43#include "TheBESKeys.h"
44#include "BESInternalFatalError.h"
51#define prolog std::string("BESLog::").append(__func__).append("() - ")
55BESLog *BESLog::d_instance =
nullptr;
56const string BESLog::mark = string(
"|&|");
85 msg << prolog <<
"ERROR - Caught BESInternalFatalError! Will re-throw. Message: " << bife.
get_message() <<
" File: " << bife.
get_file() <<
" Line: " << bife.
get_line() << endl;
86 BESDEBUG(MODULE,msg.str());
92 msg << prolog <<
"FATAL ERROR: Caught unknown exception! Unable to determine log file name." << endl;
93 BESDEBUG(MODULE,msg.str());
104 msg << prolog <<
"ERROR - Caught BESInternalFatalError! Will re-throw. Message: " << bife.
get_message() <<
" File: " << bife.
get_file() <<
" Line: " << bife.
get_line() << endl;
105 BESDEBUG(MODULE,msg.str());
111 msg << prolog <<
"FATAL ERROR: Caught unknown exception! Unable to determine log file name." << endl;
112 BESDEBUG(MODULE,msg.str());
124 BESDEBUG(MODULE, prolog <<
"d_use_local_time: " << (d_use_local_time?
"true":
"false") << endl);
128 err << prolog <<
"FATAL ERROR: Caught unknown exception. Failed to read the value of BES.LogTimeLocal" << endl;
129 BESDEBUG(MODULE,err.str());
130 cerr << err.str() << endl;
134 if (d_file_name.empty()) {
136 err << prolog <<
"FATAL ERROR: unable to determine log file name. ";
137 err <<
"Please set BES.LogName in your initialization file" << endl;
138 BESDEBUG(MODULE,err.str());
139 cerr << err.str() << endl;
143 d_file_buffer =
new ofstream(d_file_name.c_str(), ios::out | ios::app);
144 if (!(*d_file_buffer)) {
146 err << prolog <<
"BES Fatal; cannot open log file " + d_file_name +
"." << endl;
147 BESDEBUG(MODULE,err.str());
148 cerr << err.str() << endl;
156 BESDEBUG(MODULE, prolog <<
"d_verbose: " << (d_verbose?
"true":
"false") << endl);
162 BESDEBUG(MODULE, prolog <<
"d_use_unix_time: " << (d_use_unix_time?
"true":
"false") << endl);
174 d_file_buffer->close();
175 delete d_file_buffer;
176 d_file_buffer =
nullptr;
185 d_pid = to_string(pid);
186 d_log_record_prolog_base = mark + d_instance_id + mark + d_pid + mark;
201 string log_record_prolog;
206 log_record_prolog = std::to_string(now);
209 char buf[
sizeof "YYYY-MM-DDTHH:MM:SS zones"];
211 if (!d_use_local_time){
212 gmtime_r(&now, &date_time);
215 localtime_r(&now, &date_time);
217 (void)strftime(buf,
sizeof buf,
"%FT%T %Z", &date_time);
218 log_record_prolog = buf;
221 log_record_prolog += d_log_record_prolog_base + get_request_id() + mark;
222 return log_record_prolog;
234 if(!msg.empty() && msg.back() !=
'\n')
235 *d_file_buffer <<
"\n";
237 *d_file_buffer << flush;
248 *d_file_buffer << file << mark << line << mark << msg ;
249 if(!msg.empty() && msg.back() !=
'\n')
250 *d_file_buffer <<
"\n";
252 *d_file_buffer << flush;
264 strm << BESIndent::LMarg <<
"BESLog::dump - (" << (
void *)
this <<
")\n";
266 strm << BESIndent::LMarg <<
" log file: " << d_file_name;
267 if (d_file_buffer && *d_file_buffer) {
268 strm << BESIndent::LMarg <<
" (log is valid)\n";
271 strm << BESIndent::LMarg <<
" (log is NOT valid)\n";
273 strm << BESIndent::LMarg <<
" d_verbose: " << (d_verbose?
"enabled":
"disable") <<
"\n";
274 strm << BESIndent::LMarg <<
"d_instance_id: " << d_instance_id <<
"\n";
275 strm << BESIndent::LMarg <<
" d_pid: " << d_pid <<
"\n";
276 BESIndent::UnIndent();
284 BESDEBUG(MODULE,
"request_id: " << request_id << endl);
292 if (d_instance ==
nullptr) {
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.
void set_request_id(const std::string &id)
Sets the current request id (cached in BESLog) to id.
pid_t update_pid()
Update the d_pid and the d_log_record_prolog_base values.
void log_record(const std::string &record_type, const std::string &msg) const
Writes msg to a log record with type lrt.
void trace_log_record(const std::string &record_type, const std::string &msg, const std::string &file, int line) const
Writes msg, file, and line to a trace log record with type lrt.
std::string log_record_begin() const
Protected method that returns a string with the first fields of a log record.
void dump(std::ostream &strm) const override
dumps information about this object
~BESLog() override
Cleans up the logging mechanism.
BESLog()
constructor that sets up logging for the application.
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()
Access to the singleton.
static std::string read_string_key(const std::string &key, const std::string &default_value)
Read a string-valued key from the bes.conf file.