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
173 char buf[
sizeof "YYYY-MM-DDTHH:MM:SS zones"];
175 (*d_file_buffer) << now;
179 if (!d_use_local_time){
180 gmtime_r(&now, &date_time);
183 localtime_r(&now, &date_time);
185 (void)strftime(buf,
sizeof buf,
"%FT%T %Z", &date_time);
186 (*d_file_buffer) << buf;
189 const time_t sctime = time(NULL);
190 const struct tm *sttime = localtime(&sctime);
192 strftime(zone_name,
sizeof(zone_name),
"%Z", sttime);
193 char *b = asctime(sttime);
195 (*d_file_buffer) << zone_name <<
" ";
196 for (
register int j = 0; b[j] !=
'\n'; j++)
197 (*d_file_buffer) << b[j];
200 (*d_file_buffer) << mark << getpid() << mark;
215 (*d_file_buffer) << s;
228 (*d_file_buffer) << s;
242 (*d_file_buffer) << val;
244 (*d_file_buffer) <<
"NULL";
260 (*d_file_buffer) << val;
262 (*d_file_buffer) <<
"NULL";
275 (*d_file_buffer) << val;
284 (*d_file_buffer) << val;
298 (*d_file_buffer) << val;
311 (*d_file_buffer) << val;
324 (*d_file_buffer) << val;
337 (*d_file_buffer) << val;
352 (*d_file_buffer) << val;
358void BESLog::flush_me(){
359 (*d_file_buffer) << flush;
371 if (!d_suspended) (*d_file_buffer) << val;
384 strm << BESIndent::LMarg <<
"BESLog::dump - (" << (
void *)
this <<
")" << endl;
386 strm << BESIndent::LMarg <<
"log file: " << d_file_name << endl;
387 if (d_file_buffer && *d_file_buffer) {
388 strm << BESIndent::LMarg <<
"log is valid" << endl;
391 strm << BESIndent::LMarg <<
"log is NOT valid" << endl;
393 strm << BESIndent::LMarg <<
"is verbose: " << d_verbose << endl;
394 strm << BESIndent::LMarg <<
"is flushed: " << d_flushed << endl;
395 strm << BESIndent::LMarg <<
"is suspended: " << d_suspended << endl;
396 BESIndent::UnIndent();
402 if (d_instance == 0) {
unsigned int get_line() const
get the line number where the exception was thrown
std::string get_file() const
get the file name where the exception was thrown
std::string get_message() const
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()