//============================================================================================================ // $Id$ // // $Description: This program to test the Boost.Log library. $ // // $Synopsis: test_logging $ // // $Author$ // // $DateTime$ //============================================================================================================ #include #include #include #include #include #include #include //#include #include #include #include #include #include #include #include #include #include //#include //#include using namespace std; namespace logging = boost::log; namespace keywords = boost::log::keywords; //namespace expr = boost::log::expressions; namespace attr = boost::log::attributes; namespace src = boost::log::sources; namespace trivial = boost::log::trivial; #define LFC1_LOG_TRACE(logger) \ BOOST_LOG_SEV(logger, trivial::trace) << "(" << __FILE__ << ", " << __LINE__ << ") " #define LFC1_LOG_DEBUG(logger) \ BOOST_LOG_SEV(logger, trivial::debug) << "(" << __FILE__ << ", " << __LINE__ << ") " #define LFC1_LOG_INFO(logger) \ BOOST_LOG_SEV(logger, trivial::info) << "(" << __FILE__ << ", " << __LINE__ << ") " #define LFC1_LOG_WARNING(logger) \ BOOST_LOG_SEV(logger, trivial::warning) << "(" << __FILE__ << ", " << __LINE__ << ") " #define LFC1_LOG_ERROR(logger) \ BOOST_LOG_SEV(logger, trivial::error) << "(" << __FILE__ << ", " << __LINE__ << ") " #define LFC1_LOG_FATAL(logger) \ BOOST_LOG_SEV(logger, trivial::fatal) << "(" << __FILE__ << ", " << __LINE__ << ") " static const auto RET_SUCCESS = 0; static const auto RET_FAIL = 1; static const auto LOGGING_ENVIRONMENT_NAME = "LOGGING_CONFIG_FILE_NAME"; static const auto DEFAULT_LOGGING_CONFIG_FILE_NAME = "config/LogSettings.txt"; BOOST_LOG_INLINE_GLOBAL_LOGGER_DEFAULT(SLogger, src::severity_logger) static void gvLoggingInit(const char* apConfigFileName = nullptr) { // Obtain the name of the logging configuration file. if (apConfigFileName == nullptr || apConfigFileName[0] == 0) { if ((apConfigFileName = getenv(LOGGING_ENVIRONMENT_NAME)) == 0 || apConfigFileName[0] == 0) { apConfigFileName = DEFAULT_LOGGING_CONFIG_FILE_NAME; } } // Open the logging configuration file. ifstream vConfigFile(apConfigFileName); if (!vConfigFile.is_open()) { // TODO: throw an exception } // Read the contents of the logging configuration file. //logging::init_from_stream(vConfigFile); logging::add_file_log ( keywords::file_name = "log/test_logging.log", keywords::format = "%TimeStamp% [%Severity%] %Process%:%ProcessID%:%ThreadID% %Message%" ); /* logging::add_file_log ( keywords::file_name = "log/test_logging.log", keywords::format = expr::stream << expr::format_date_time("TimeStamp", "%Y-%m-%d %H:%M:%S.%f") << " [" << expr::attr("Severity") << "] " << expr::attr("Process") << ":" << expr::attr("ProcessID") << ":" << expr::attr("ThreadID") << " " << expr::message ); */ // Add the logging attributes that we require. auto pCore = logging::core::get(); pCore->add_global_attribute("TimeStamp", attr::local_clock()); pCore->add_global_attribute("Process", attr::current_process_name()); pCore->add_global_attribute("ProcessID", attr::current_process_id()); pCore->add_global_attribute("ThreadID", attr::current_thread_id()); pCore->set_filter(trivial::severity >= trivial::info); } int main() { auto vRetVal = RET_SUCCESS; try { gvLoggingInit(); auto& vLogger = SLogger::get(); LFC1_LOG_TRACE(vLogger) << "A trace severity message"; LFC1_LOG_DEBUG(vLogger) << "A debug severity message"; LFC1_LOG_INFO(vLogger) << "An informational severity message"; LFC1_LOG_WARNING(vLogger) << "A warning severity message"; LFC1_LOG_ERROR(vLogger) << "An error severity message"; LFC1_LOG_FATAL(vLogger) << "A fatal severity message"; } catch (exception& crException) { cerr << crException.what() << endl; vRetVal = RET_FAIL; } catch (...) { cerr << "Unknown exception" << endl; vRetVal = RET_FAIL; } return vRetVal; } //============================================================================================================ // End of Code //============================================================================================================