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 _file_name = TheBESKeys::TheKeys()->get_key( "BES.LogName", found ) ;
00077 if( _file_name == "" )
00078 {
00079 string err = (string)"BES Fatal: unable to determine log file name."
00080 + " Please set BES.LogName in your initialization file" ;
00081 cerr << err << endl ;
00082 throw BESInternalFatalError( err, __FILE__, __LINE__ ) ;
00083 }
00084 else
00085 {
00086 _file_buffer = new ofstream( _file_name.c_str(), ios::out | ios::app ) ;
00087 if( !(*_file_buffer) )
00088 {
00089 string err = (string)"BES Fatal; can not open log file "
00090 + _file_name + "." ;
00091 cerr << err << endl ;
00092 throw BESInternalFatalError( err, __FILE__, __LINE__ ) ;
00093 }
00094
00095
00096
00097
00098
00099
00100
00101 }
00102 string verbose = TheBESKeys::TheKeys()->get_key( "BES.LogVerbose", found ) ;
00103 if( verbose == "YES" || verbose == "Yes" || verbose == "yes" )
00104 {
00105 _verbose = true ;
00106 }
00107 }
00108
00113 BESLog:: ~BESLog()
00114 {
00115 _file_buffer->close();
00116 delete _file_buffer;
00117 _file_buffer = 0 ;
00118 }
00119
00126 void
00127 BESLog::dump_time()
00128 {
00129 const time_t sctime=time(NULL);
00130 const struct tm *sttime=localtime(&sctime);
00131 char zone_name[10];
00132 strftime(zone_name, sizeof(zone_name), "%Z", sttime);
00133 char *b=asctime(sttime);
00134 (*_file_buffer)<<"["<<zone_name<<" ";
00135 for (register int j=0; b[j]!='\n'; j++)
00136 (*_file_buffer)<<b[j];
00137 pid_t thepid = getpid() ;
00138 (*_file_buffer)<<" id: "<<thepid<<"] ";
00139 _flushed = 0 ;
00140 }
00141
00146 BESLog& BESLog::operator<<(string &s)
00147 {
00148 if (!_suspended)
00149 {
00150 if (_flushed)
00151 dump_time();
00152 (*_file_buffer) << s;
00153 }
00154 return *this;
00155 }
00156
00161 BESLog& BESLog::operator<<(const string &s)
00162 {
00163 if (!_suspended)
00164 {
00165 if (_flushed)
00166 dump_time();
00167 (*_file_buffer) << s;
00168 }
00169 return *this;
00170 }
00171
00176 BESLog& BESLog::operator<<(char *val)
00177 {
00178 if (!_suspended)
00179 {
00180 if (_flushed)
00181 dump_time();
00182 if( val )
00183 (*_file_buffer) << val;
00184 else
00185 (*_file_buffer) << "NULL" ;
00186 }
00187 return *this;
00188 }
00189
00194 BESLog& BESLog::operator<<(const char *val)
00195 {
00196 if (!_suspended)
00197 {
00198 if (_flushed)
00199 {
00200 dump_time();
00201 }
00202 if( val )
00203 (*_file_buffer) << val;
00204 else
00205 (*_file_buffer) << "NULL" ;
00206 }
00207 return *this;
00208 }
00209
00214 BESLog& BESLog::operator<<(int val)
00215 {
00216 if (!_suspended)
00217 {
00218 if (_flushed)
00219 dump_time();
00220 (*_file_buffer) << val;
00221 }
00222 return *this;
00223 }
00224
00229 BESLog& BESLog::operator<<(char val)
00230 {
00231 if (!_suspended)
00232 {
00233 if (_flushed)
00234 dump_time();
00235 (*_file_buffer) << val;
00236 }
00237 return *this;
00238 }
00239
00244 BESLog& BESLog::operator<<(long val)
00245 {
00246 if (!_suspended)
00247 {
00248 if (_flushed)
00249 dump_time();
00250 (*_file_buffer) << val;
00251 }
00252 return *this;
00253 }
00254
00259 BESLog& BESLog::operator<<(unsigned long val)
00260 {
00261 if (!_suspended)
00262 {
00263 if (_flushed)
00264 dump_time();
00265 (*_file_buffer) << val;
00266 }
00267 return *this;
00268 }
00269
00274 BESLog& BESLog::operator<<(double val)
00275 {
00276 if (!_suspended)
00277 {
00278 if (_flushed)
00279 dump_time();
00280 (*_file_buffer) << val;
00281 }
00282 return *this;
00283 }
00284
00292 BESLog& BESLog::operator<<(p_ostream_manipulator val)
00293 {
00294 if (!_suspended)
00295 {
00296 (*_file_buffer) << val ;
00297 if ((val==(p_ostream_manipulator)endl) || (val==(p_ostream_manipulator)flush))
00298 _flushed=1;
00299 }
00300 return *this;
00301 }
00302
00309 BESLog& BESLog::operator<<(p_ios_manipulator val)
00310 {
00311 if (!_suspended)
00312 (*_file_buffer)<<val;
00313 return *this;
00314 }
00315
00323 void
00324 BESLog::dump( ostream &strm ) const
00325 {
00326 strm << BESIndent::LMarg << "BESLog::dump - ("
00327 << (void *)this << ")" << endl ;
00328 BESIndent::Indent() ;
00329 strm << BESIndent::LMarg << "log file: " << _file_name << endl ;
00330 if( _file_buffer && *_file_buffer )
00331 {
00332 strm << BESIndent::LMarg << "log is valid" << endl ;
00333 }
00334 else
00335 {
00336 strm << BESIndent::LMarg << "log is NOT valid" << endl ;
00337 }
00338 strm << BESIndent::LMarg << "is verbose: " << _verbose << endl ;
00339 strm << BESIndent::LMarg << "is flushed: " << _flushed << endl ;
00340 strm << BESIndent::LMarg << "is suspended: " << _suspended << endl ;
00341 BESIndent::UnIndent() ;
00342 }
00343
00344 BESLog *
00345 BESLog::TheLog()
00346 {
00347 if( _instance == 0 )
00348 {
00349 _instance = new BESLog ;
00350 }
00351 return _instance ;
00352 }
00353