diff --git a/g2log/src/g2log.cpp b/g2log/src/g2log.cpp index 9bb0e6d..a2d61e7 100644 --- a/g2log/src/g2log.cpp +++ b/g2log/src/g2log.cpp @@ -62,6 +62,14 @@ namespace g2 { #endif CHECK(!internal::isLoggingInitialized()); CHECK(bgworker != nullptr); + + // Save the first uninitialized message, if any + std::call_once(g_save_first_unintialized_flag, [&bgworker] { + if (g_first_unintialized_msg) { + bgworker->save(LogMessagePtr{std::move(g_first_unintialized_msg)}); + } + }); + g_logger_instance = bgworker; } @@ -102,20 +110,13 @@ namespace g2 { err.append(g_first_unintialized_msg->message()); std::string& str = g_first_unintialized_msg->write(); str.clear(); - str.append(err); + str.append(err); // replace content std::cerr << str << std::endl; }); return; } // logger is initialized - // Save the first uninitialized message, if any - std::call_once(g_save_first_unintialized_flag, [] { - if (g_first_unintialized_msg) { - g_logger_instance->save(LogMessagePtr{std::move(g_first_unintialized_msg)}); - } - }); - g_logger_instance->save(incoming); } diff --git a/g2log/src/g2logmessage.cpp b/g2log/src/g2logmessage.cpp index 5f76fa0..a7cb67d 100644 --- a/g2log/src/g2logmessage.cpp +++ b/g2log/src/g2logmessage.cpp @@ -36,86 +36,47 @@ namespace { namespace g2 { -// std::string LogMessage::toString() const { -// std::ostringstream oss; -// oss << "\n" << timestamp() << "." << microseconds() << "\t"; -// oss << level() << " [" << file(); -// oss << " L: " << line() << "]\t"; -// -// // Non-fatal Log Message -// if (false == wasFatal()) { -// oss << '"' << message() << '"'; -// return oss.str(); -// } -// -// if (internal::FATAL_SIGNAL.value == _level.value) { -// oss.str(""); // clear any previous text and formatting -// oss << "\n" << timestamp() << "." << microseconds(); -// oss << "\n\n***** FATAL SIGNAL RECEIVED ******* " << std::endl; -// oss << '"' << message() << '"'; -// return oss.str(); -// } -// -// // Not crash scenario but LOG or CONTRACT -// auto level_value = _level.value; -// if (FATAL.value == level_value) { -// oss << "\n\t*******\tEXIT trigger caused by LOG(FATAL) entry: \n\t"; -// oss << '"' << message() << '"'; -// oss << "\n*******\tSTACKDUMP *******\n" << internal::stackdump(); -// } else if (internal::CONTRACT.value == level_value) { -// oss << "\n\t *******\tEXIT trigger caused by broken Contract: CHECK(" << _expression << ")\n\t"; -// oss << '"' << message() << '"'; -// oss << "\n*******\tSTACKDUMP *******\n" << internal::stackdump() << '"'; -// } else { -// oss << "\n\t*******\tUNKNOWN Log Message Type\n" << '"' << message() << '"'; -// } -// -// return oss.str(); -// } + std::string LogMessage::toString() const { + std::ostringstream oss; + oss << "\n" << timestamp() << "." << microseconds() << "\t"; + oss << level() << " [" << file(); + oss << " L: " << line() << "]\t"; - std::string LogMessage::timestamp(const std::string & time_look) const { - return localtime_formatted(_timestamp, time_look); - } - - - - std::string LogMessage::toString() { - std::string out; - out.append("\n").append(timestamp()).append(".") - .append(microseconds()).append("\t") - .append(level()).append(" [").append(file()) - .append(" L: ").append(line()).append("]\t"); - // Non-fatal Log Message if (false == wasFatal()) { - out.append("\"").append(message()).append("\""); - return out; + oss << '"' << message() << '"'; + return oss.str(); } if (internal::FATAL_SIGNAL.value == _level.value) { - out = {"\n"}; - out.append(timestamp()).append(".").append(microseconds()) - .append("\n\n***** FATAL SIGNAL RECEIVED ******* \n""\"").append(message()).append("\""); - return out; + oss.str(""); // clear any previous text and formatting + oss << "\n" << timestamp() << "." << microseconds(); + oss << "\n\n***** FATAL SIGNAL RECEIVED ******* " << std::endl; + oss << '"' << message() << '"'; + return oss.str(); } // Not crash scenario but LOG or CONTRACT auto level_value = _level.value; if (FATAL.value == level_value) { - out.append("\n\t*******\tEXIT trigger caused by LOG(FATAL) entry: \n\t") - .append("\"").append(message()).append("\"") - .append("\n*******\tSTACKDUMP *******\n") - .append(internal::stackdump()); + oss << "\n\t*******\tEXIT trigger caused by LOG(FATAL) entry: \n\t"; + oss << '"' << message() << '"'; + oss << "\n*******\tSTACKDUMP *******\n" << internal::stackdump(); } else if (internal::CONTRACT.value == level_value) { - out.append("\n\t *******\tEXIT trigger caused by broken Contract: CHECK(").append(_expression) - .append(")\n\t").append("\"").append(message()).append("\"") - .append("\n*******\tSTACKDUMP *******\n").append(internal::stackdump()).append("\""); + oss << "\n\t *******\tEXIT trigger caused by broken Contract: CHECK(" << _expression << ")\n\t"; + oss << '"' << message() << '"'; + oss << "\n*******\tSTACKDUMP *******\n" << internal::stackdump() << '"'; } else { - out.append("\n\t*******\tUNKNOWN Log Message Type\n").append("\"").append(message()).append("\""); + oss << "\n\t*******\tUNKNOWN Log Message Type\n" << '"' << message() << '"'; } - return out; - } + return oss.str(); + } + + std::string LogMessage::timestamp(const std::string & time_look) const { + return localtime_formatted(_timestamp, time_look); + } + LogMessage::LogMessage(const std::string &file, const int line, const std::string& function, const LEVELS& level) : _timestamp(g2::systemtime_now()) diff --git a/g2log/src/g2logmessage.hpp b/g2log/src/g2logmessage.hpp index ce1bd71..689dc3c 100644 --- a/g2log/src/g2logmessage.hpp +++ b/g2log/src/g2logmessage.hpp @@ -34,10 +34,10 @@ namespace g2 { std::string message() const { return _message; } std::string& write() const { return _message; } - std::string expression() { return _expression; } - bool wasFatal() { return internal::wasFatal(_level); } + std::string expression() const { return _expression; } + bool wasFatal() const { return internal::wasFatal(_level); } - std::string toString(); + std::string toString() const; void setExpression(const std::string expression) { _expression = expression; } @@ -46,8 +46,6 @@ namespace g2 { LogMessage(const LogMessage&); LogMessage(LogMessage&& other); - //LogMessage& operator=(const LogMessage& other); - //LogMessage& operator=(LogMessage&& other); virtual ~LogMessage() = default;