9 #ifndef BOOST_LOG_DYN_LINK 10 #define BOOST_LOG_DYN_LINK 12 #include <boost/date_time/posix_time/posix_time_types.hpp> 13 #include <boost/log/attributes/current_process_id.hpp> 14 #include <boost/log/attributes/current_process_name.hpp> 15 #include <boost/log/attributes/current_thread_id.hpp> 16 #include <boost/log/expressions.hpp> 17 #include <boost/log/sources/global_logger_storage.hpp> 18 #include <boost/log/sources/logger.hpp> 19 #include <boost/log/sources/record_ostream.hpp> 20 #include <boost/log/sources/severity_logger.hpp> 21 #include <boost/log/support/date_time.hpp> 22 #include <boost/log/utility/setup/common_attributes.hpp> 23 #include <boost/log/utility/setup/console.hpp> 24 #include <boost/log/utility/setup/file.hpp> 34 #define LOG(severity) BOOST_LOG_SEV(dds::misc::Logger::instance().logger(), severity) 37 #define P_H BOOST_LOG_SEV(dds::misc::Logger::instance().logger(), dds::misc::proto_high) 38 #define P_M BOOST_LOG_SEV(dds::misc::Logger::instance().logger(), dds::misc::proto_mid) 39 #define P_L BOOST_LOG_SEV(dds::misc::Logger::instance().logger(), dds::misc::proto_low) 40 #define DBG BOOST_LOG_SEV(dds::misc::Logger::instance().logger(), dds::misc::debug) 41 #define INF BOOST_LOG_SEV(dds::misc::Logger::instance().logger(), dds::misc::info) 42 #define WRN BOOST_LOG_SEV(dds::misc::Logger::instance().logger(), dds::misc::warning) 43 #define ERR BOOST_LOG_SEV(dds::misc::Logger::instance().logger(), dds::misc::error) 44 #define FAT BOOST_LOG_SEV(dds::misc::Logger::instance().logger(), dds::misc::fatal) 45 #define STDOUT BOOST_LOG_SEV(dds::misc::Logger::instance().logger(), dds::misc::log_stdout) 46 #define STDOUT_CLEAN BOOST_LOG_SEV(dds::misc::Logger::instance().logger(), dds::misc::log_stdout_clean) 47 #define STDERR BOOST_LOG_SEV(dds::misc::Logger::instance().logger(), dds::misc::log_stderr) 56 typedef boost::shared_ptr<boost::log::sinks::synchronous_sink<boost::log::sinks::text_file_backend>> fileSink_t;
59 typedef boost::log::sources::severity_logger_mt<ELogSeverityLevel> logger_t;
62 static Logger& instance()
64 static Logger instance;
76 static bool bStarted =
false;
82 using namespace boost::log;
89 m_fileSink = createFileSink();
92 typedef boost::shared_ptr<sinks::synchronous_sink<sinks::text_ostream_backend>> ostreamSink_t;
93 ostreamSink_t stdoutSink = add_console_log(std::cout, keywords::format =
"%Process%: %Message%");
94 ostreamSink_t stdoutCleanSink = add_console_log(std::cout, keywords::format =
"%Message%");
95 ostreamSink_t stderrSink = add_console_log(std::cerr, keywords::format =
"%Process%: error: %Message%");
97 stdoutSink->set_filter(severity ==
log_stdout && hasConsoleOutput);
98 stdoutCleanSink->set_filter(severity ==
log_stdout_clean && hasConsoleOutput);
99 stderrSink->set_filter(severity ==
log_stderr && hasConsoleOutput);
101 add_common_attributes();
102 core::get()->add_global_attribute(
"Process", attributes::current_process_name());
104 LOG(
info) <<
"Log engine is initialized with severety \"" 110 boost::log::core::get()->remove_sink(m_fileSink);
112 m_fileSink = createFileSink();
116 fileSink_t createFileSink()
const 118 using namespace boost::log;
124 std::string sLogFile = userDefaults.
getLogFile();
127 formatter formatter =
129 expressions::stream << std::left
130 << expressions::format_date_time<boost::posix_time::ptime>(
"TimeStamp",
131 "%Y-%m-%d %H:%M:%S.%f")
132 <<
" " << std::setw(7) << expressions::attr<ELogSeverityLevel>(
"Severity")
133 << std::setw(20) << expressions::attr<std::string>(
"Process") <<
" <" 134 << expressions::attr<attributes::current_process_id::value_type>(
"ProcessID") <<
":" 135 << expressions::attr<attributes::current_thread_id::value_type>(
"ThreadID")
136 <<
"> " << expressions::smessage;
138 fileSink_t fileSink =
139 add_file_log(keywords::file_name = sLogFile,
140 keywords::open_mode = (std::ios::out | std::ios::app),
141 keywords::rotation_size = rotationSize * 1024 * 1024,
143 keywords::time_based_rotation = sinks::file::rotation_at_time_point(0, 0, 0),
147 keywords::max_size = 1000 * 1024 * 1024,
148 keywords::min_free_space = 2000 * 1024 * 1024,
149 keywords::auto_flush =
true);
151 fileSink->set_formatter(formatter);
152 fileSink->set_filter(severity >= severityLevel);
160 fileSink_t m_fileSink;
163 #endif //_DDS_LOGGER_H_
std::string getLogFile() const
Definition: UserDefaults.cpp:420
dds::misc::ELogSeverityLevel m_logSeverityLevel
Log rotation size in MB.
Definition: dds-user-defaults/src/Options.h:29
const SDDSUserDefaultsOptions_t getOptions() const
Definition: UserDefaults.cpp:317
#define LOG(severity)
Definition: Logger.h:34
unsigned int m_logRotationSize
True if output log also to console.
Definition: dds-user-defaults/src/Options.h:31
ELogSeverityLevel
Log Severity levels.
Definition: def.h:141
bool m_logHasConsoleOutput
Idle time in [s] after which process will be killed by monitoring thread.
Definition: dds-user-defaults/src/Options.h:33
Definition: UserDefaults.h:21
static CUserDefaults & instance(const boost::uuids::uuid &_sid=CUserDefaults::getInitialSID())
Return singleton instance.
Definition: UserDefaults.cpp:37
BOOST_LOG_ATTRIBUTE_KEYWORD(severity, "Severity", ELogSeverityLevel) class Logger
Definition: Logger.h:51
SDDSServerOptions m_server
Definition: dds-user-defaults/src/Options.h:54
Definition: BoostHelper.h:14