43#include "TheBESKeys.h"
44#include "BESInternalFatalError.h"
50#define ISO8601_TIME_IN_LOGS
52#define prolog std::string("BESLog::").append(__func__).append("() - ")
56BESLog *BESLog::d_instance = 0;
57const string BESLog::mark = string(
"|&|");
79 d_flushed(1), d_file_buffer(0), d_suspended(0), d_verbose(false), d_use_local_time(false), d_use_unix_time(false)
88 msg << prolog <<
"ERROR - Caught BESInternalFatalError! Will re-throw. Message: " << bife.
get_message() <<
" File: " << bife.
get_file() <<
" Line: " << bife.
get_line() << endl;
89 BESDEBUG(MODULE,msg.str());
95 msg << prolog <<
"FATAL ERROR: Caught unknown exception! Unable to determine log file name." << endl;
96 BESDEBUG(MODULE,msg.str());
107 BESDEBUG(MODULE, prolog <<
"d_use_local_time: " << (d_use_local_time?
"true":
"false") << endl);
111 err << prolog <<
"FATAL ERROR: Caught unknown exception. Failed to read the value of BES.LogTimeLocal" << endl;
112 BESDEBUG(MODULE,err.str());
113 cerr << err.str() << endl;
117 if (d_file_name.empty()) {
119 err << prolog <<
"FATAL ERROR: unable to determine log file name. ";
120 err <<
"Please set BES.LogName in your initialization file" << endl;
121 BESDEBUG(MODULE,err.str());
122 cerr << err.str() << endl;
126 d_file_buffer =
new ofstream(d_file_name.c_str(), ios::out | ios::app);
127 if (!(*d_file_buffer)) {
129 err << prolog <<
"BES Fatal; cannot open log file " + d_file_name +
"." << endl;
130 BESDEBUG(MODULE,err.str());
131 cerr << err.str() << endl;
139 BESDEBUG(MODULE, prolog <<
"d_verbose: " << (d_verbose?
"true":
"false") << endl);
145 BESDEBUG(MODULE, prolog <<
"d_use_unix_time: " << (d_use_unix_time?
"true":
"false") << endl);
155 d_file_buffer->close();
156 delete d_file_buffer;
169#ifdef ISO8601_TIME_IN_LOGS
174 char buf[
sizeof "YYYY-MM-DDTHH:MM:SSzone"];
183 if (!d_use_local_time)
184 status = strftime(buf,
sizeof buf,
"%FT%T%Z", gmtime(&now));
186 status = strftime(buf,
sizeof buf,
"%FT%T%Z", localtime(&now));
189 char buf[
sizeof "YYYY-MM-DDTHH:MM:SS zones"];
192 (*d_file_buffer) << now;
196 if (!d_use_local_time){
197 gmtime_r(&now, &dat_time);
200 localtime_r(&now, &dat_time);
202 status = strftime(buf,
sizeof buf,
"%FT%T %Z", &dat_time);
203 (*d_file_buffer) << buf;
208 const time_t sctime = time(NULL);
209 const struct tm *sttime = localtime(&sctime);
211 strftime(zone_name,
sizeof(zone_name),
"%Z", sttime);
212 char *b = asctime(sttime);
214 (*d_file_buffer) << zone_name <<
" ";
215 for (
register int j = 0; b[j] !=
'\n'; j++)
216 (*d_file_buffer) << b[j];
219 (*d_file_buffer) << mark << getpid() << mark;
234 (*d_file_buffer) << s;
247 (*d_file_buffer) << s;
261 (*d_file_buffer) << val;
263 (*d_file_buffer) <<
"NULL";
279 (*d_file_buffer) << val;
281 (*d_file_buffer) <<
"NULL";
294 (*d_file_buffer) << val;
303 (*d_file_buffer) << val;
317 (*d_file_buffer) << val;
330 (*d_file_buffer) << val;
343 (*d_file_buffer) << val;
356 (*d_file_buffer) << val;
371 (*d_file_buffer) << val;
377void BESLog::flush_me(){
378 (*d_file_buffer) << flush;
390 if (!d_suspended) (*d_file_buffer) << val;
403 strm << BESIndent::LMarg <<
"BESLog::dump - (" << (
void *)
this <<
")" << endl;
405 strm << BESIndent::LMarg <<
"log file: " << d_file_name << endl;
406 if (d_file_buffer && *d_file_buffer) {
407 strm << BESIndent::LMarg <<
"log is valid" << endl;
410 strm << BESIndent::LMarg <<
"log is NOT valid" << endl;
412 strm << BESIndent::LMarg <<
"is verbose: " << d_verbose << endl;
413 strm << BESIndent::LMarg <<
"is flushed: " << d_flushed << endl;
414 strm << BESIndent::LMarg <<
"is suspended: " << d_suspended << endl;
415 BESIndent::UnIndent();
421 if (d_instance == 0) {
virtual int get_line()
get the line number where the exception was thrown
virtual std::string get_file()
get the file name where the exception was thrown
virtual std::string get_message()
get the error message for this exception
exception thrown if an internal error is found and is fatal to the BES
Provides a mechanism for applications to log information to an external file.
std::ostream &(* p_ostream_manipulator)(std::ostream &)
Defines a data type p_std::ostream_manipulator "pointer to function that takes std::ostream& and retu...
void dump_time()
Protected method that dumps the date/time to the log file.
BESLog & operator<<(std::string &)
Overloaded inserter that writes the specified string.
~BESLog()
Cleans up the logging mechanism.
BESLog()
constructor that sets up logging for the application.
virtual void dump(std::ostream &strm) const
dumps information about this object
static std::string lowercase(const std::string &s)
void get_value(const std::string &s, std::string &val, bool &found)
Retrieve the value of a given key, if set.
static TheBESKeys * TheKeys()