Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #include "config.h"
00034
00035 #include <iostream>
00036 #include <time.h>
00037 #include <string>
00038
00039 #include "BESLog.h"
00040 #include "TheBESKeys.h"
00041 #include "BESInternalFatalError.h"
00042
00043 #if HAVE_UNISTD_H
00044 #include <unistd.h>
00045 #endif
00046
00047 using std::cerr ;
00048 using std::endl ;
00049 using std::flush ;
00050
00051 BESLog *BESLog::_instance = 0 ;
00052
00068 BESLog::BESLog()
00069 : _flushed( 1 ),
00070 _file_buffer( 0 ),
00071 _suspended( 0 ),
00072 _verbose( false )
00073 {
00074 _suspended = 0 ;
00075 bool found = false ;
00076 try
00077 {
00078 TheBESKeys::TheKeys()->get_value( "BES.LogName", _file_name, found ) ;
00079 }
00080 catch( ... )
00081 {
00082 string err = (string)"BES Fatal: unable to determine log file name."
00083 + " The key BES.LogName has multiple values" ;
00084 cerr << err << endl ;
00085 throw BESInternalFatalError( err, __FILE__, __LINE__ ) ;
00086 }
00087 if( _file_name == "" )
00088 {
00089 string err = (string)"BES Fatal: unable to determine log file name."
00090 + " Please set BES.LogName in your initialization file" ;
00091 cerr << err << endl ;
00092 throw BESInternalFatalError( err, __FILE__, __LINE__ ) ;
00093 }
00094 _file_buffer = new ofstream( _file_name.c_str(), ios::out | ios::app ) ;
00095 if( !(*_file_buffer) )
00096 {
00097 string err = (string)"BES Fatal; cannot open log file "
00098 + _file_name + "." ;
00099 cerr << err << endl ;
00100 throw BESInternalFatalError( err, __FILE__, __LINE__ ) ;
00101 }
00102 found = false ;
00103 string verbose ;
00104 TheBESKeys::TheKeys()->get_value( "BES.LogVerbose", verbose, found ) ;
00105 if( verbose == "YES" || verbose == "Yes" || verbose == "yes" )
00106 {
00107 _verbose = true ;
00108 }
00109 }
00110
00115 BESLog:: ~BESLog()
00116 {
00117 _file_buffer->close();
00118 delete _file_buffer;
00119 _file_buffer = 0 ;
00120 }
00121
00128 void
00129 BESLog::dump_time()
00130 {
00131 const time_t sctime=time(NULL);
00132 const struct tm *sttime=localtime(&sctime);
00133 char zone_name[10];
00134 strftime(zone_name, sizeof(zone_name), "%Z", sttime);
00135 char *b=asctime(sttime);
00136 (*_file_buffer)<<"["<<zone_name<<" ";
00137 for (register int j=0; b[j]!='\n'; j++)
00138 (*_file_buffer)<<b[j];
00139 pid_t thepid = getpid() ;
00140 (*_file_buffer)<<" id: "<<thepid<<"] ";
00141 _flushed = 0 ;
00142 }
00143
00148 BESLog& BESLog::operator<<(string &s)
00149 {
00150 if (!_suspended)
00151 {
00152 if (_flushed)
00153 dump_time();
00154 (*_file_buffer) << s;
00155 }
00156 return *this;
00157 }
00158
00163 BESLog& BESLog::operator<<(const string &s)
00164 {
00165 if (!_suspended)
00166 {
00167 if (_flushed)
00168 dump_time();
00169 (*_file_buffer) << s;
00170 }
00171 return *this;
00172 }
00173
00178 BESLog& BESLog::operator<<(char *val)
00179 {
00180 if (!_suspended)
00181 {
00182 if (_flushed)
00183 dump_time();
00184 if( val )
00185 (*_file_buffer) << val;
00186 else
00187 (*_file_buffer) << "NULL" ;
00188 }
00189 return *this;
00190 }
00191
00196 BESLog& BESLog::operator<<(const char *val)
00197 {
00198 if (!_suspended)
00199 {
00200 if (_flushed)
00201 {
00202 dump_time();
00203 }
00204 if( val )
00205 (*_file_buffer) << val;
00206 else
00207 (*_file_buffer) << "NULL" ;
00208 }
00209 return *this;
00210 }
00211
00216 BESLog& BESLog::operator<<(int val)
00217 {
00218 if (!_suspended)
00219 {
00220 if (_flushed)
00221 dump_time();
00222 (*_file_buffer) << val;
00223 }
00224 return *this;
00225 }
00226
00231 BESLog& BESLog::operator<<(char val)
00232 {
00233 if (!_suspended)
00234 {
00235 if (_flushed)
00236 dump_time();
00237 (*_file_buffer) << val;
00238 }
00239 return *this;
00240 }
00241
00246 BESLog& BESLog::operator<<(long val)
00247 {
00248 if (!_suspended)
00249 {
00250 if (_flushed)
00251 dump_time();
00252 (*_file_buffer) << val;
00253 }
00254 return *this;
00255 }
00256
00261 BESLog& BESLog::operator<<(unsigned long val)
00262 {
00263 if (!_suspended)
00264 {
00265 if (_flushed)
00266 dump_time();
00267 (*_file_buffer) << val;
00268 }
00269 return *this;
00270 }
00271
00276 BESLog& BESLog::operator<<(double val)
00277 {
00278 if (!_suspended)
00279 {
00280 if (_flushed)
00281 dump_time();
00282 (*_file_buffer) << val;
00283 }
00284 return *this;
00285 }
00286
00294 BESLog& BESLog::operator<<(p_ostream_manipulator val)
00295 {
00296 if (!_suspended)
00297 {
00298 (*_file_buffer) << val ;
00299 if ((val==(p_ostream_manipulator)endl) || (val==(p_ostream_manipulator)flush))
00300 _flushed=1;
00301 }
00302 return *this;
00303 }
00304
00311 BESLog& BESLog::operator<<(p_ios_manipulator val)
00312 {
00313 if (!_suspended)
00314 (*_file_buffer)<<val;
00315 return *this;
00316 }
00317
00325 void
00326 BESLog::dump( ostream &strm ) const
00327 {
00328 strm << BESIndent::LMarg << "BESLog::dump - ("
00329 << (void *)this << ")" << endl ;
00330 BESIndent::Indent() ;
00331 strm << BESIndent::LMarg << "log file: " << _file_name << endl ;
00332 if( _file_buffer && *_file_buffer )
00333 {
00334 strm << BESIndent::LMarg << "log is valid" << endl ;
00335 }
00336 else
00337 {
00338 strm << BESIndent::LMarg << "log is NOT valid" << endl ;
00339 }
00340 strm << BESIndent::LMarg << "is verbose: " << _verbose << endl ;
00341 strm << BESIndent::LMarg << "is flushed: " << _flushed << endl ;
00342 strm << BESIndent::LMarg << "is suspended: " << _suspended << endl ;
00343 BESIndent::UnIndent() ;
00344 }
00345
00346 BESLog *
00347 BESLog::TheLog()
00348 {
00349 if( _instance == 0 )
00350 {
00351 _instance = new BESLog ;
00352 }
00353 return _instance ;
00354 }
00355