DDS  ver. 1.6
Logger.h
Go to the documentation of this file.
1 // Copyright 2014 GSI, Inc. All rights reserved.
2 //
3 // Log engine core.
4 //
5 #ifndef LOGGER_H
6 #define LOGGER_H
7 
8 // BOOST
9 #define BOOST_LOG_DYN_LINK
10 #include <boost/date_time/posix_time/posix_time_types.hpp>
11 
12 #if BOOST_VERSION < 105700
13 #pragma clang diagnostic push
14 #pragma clang diagnostic ignored "-Wdeprecated-register"
15 #endif
16 
17 #pragma clang diagnostic push
18 #pragma clang diagnostic ignored "-Wunused-local-typedef"
19 #include <boost/log/expressions.hpp>
20 #include <boost/log/support/date_time.hpp>
21 #pragma clang diagnostic pop
22 
23 #include <boost/log/attributes/current_process_id.hpp>
24 #include <boost/log/attributes/current_process_name.hpp>
25 #include <boost/log/attributes/current_thread_id.hpp>
26 #include <boost/log/sources/global_logger_storage.hpp>
27 #include <boost/log/sources/logger.hpp>
28 #include <boost/log/sources/record_ostream.hpp>
29 #include <boost/log/sources/severity_logger.hpp>
30 #include <boost/log/utility/setup/common_attributes.hpp>
31 #include <boost/log/utility/setup/console.hpp>
32 #include <boost/log/utility/setup/file.hpp>
33 #if BOOST_VERSION < 105700
34 #pragma clang diagnostic pop
35 #endif
36 // STD
37 #include <fstream>
38 #include <ostream>
39 
40 // DDS
41 #include "SysHelper.h"
42 #include "UserDefaults.h"
43 #include "def.h"
44 
45 // Main macro to be used for logging in DDS
46 // Example: LOG(info) << "My message";
47 //
48 // WORKAROUND: a bug in 1.59 version
49 // https://svn.boost.org/trac/boost/ticket/11549
50 //
51 #if BOOST_VERSION == 105900
52 #define LOG(severity) BOOST_LOG_SEV(MiscCommon::Logger::instance().logger(), severity) << ""
53 #else
54 #define LOG(severity) BOOST_LOG_SEV(MiscCommon::Logger::instance().logger(), severity)
55 #endif
56 
57 // Convenience functions
58 #define P_H BOOST_LOG_SEV(MiscCommon::Logger::instance().logger(), MiscCommon::proto_high)
59 #define P_M BOOST_LOG_SEV(MiscCommon::Logger::instance().logger(), MiscCommon::proto_mid)
60 #define P_L BOOST_LOG_SEV(MiscCommon::Logger::instance().logger(), MiscCommon::proto_low)
61 #define DBG BOOST_LOG_SEV(MiscCommon::Logger::instance().logger(), MiscCommon::debug)
62 #define INF BOOST_LOG_SEV(MiscCommon::Logger::instance().logger(), MiscCommon::info)
63 #define WRN BOOST_LOG_SEV(MiscCommon::Logger::instance().logger(), MiscCommon::warning)
64 #define ERR BOOST_LOG_SEV(MiscCommon::Logger::instance().logger(), MiscCommon::error)
65 #define FAT BOOST_LOG_SEV(MiscCommon::Logger::instance().logger(), MiscCommon::fatal)
66 #define STDOUT BOOST_LOG_SEV(MiscCommon::Logger::instance().logger(), MiscCommon::log_stdout)
67 #define STDOUT_CLEAN BOOST_LOG_SEV(MiscCommon::Logger::instance().logger(), MiscCommon::log_stdout_clean)
68 #define STDERR BOOST_LOG_SEV(MiscCommon::Logger::instance().logger(), MiscCommon::log_stderr)
69 
70 namespace MiscCommon
71 {
73 
74  class Logger
75  {
76  public:
77  typedef boost::log::sources::severity_logger_mt<ELogSeverityLevel> logger_t;
78 
80  static Logger& instance()
81  {
82  static Logger instance;
83  return instance;
84  }
85 
86  logger_t& logger()
87  {
88  return fLogger;
89  }
90 
92  void init()
93  {
94  static bool bStarted = false;
95  if (bStarted)
96  return;
97 
98  bStarted = true;
99 
100  using namespace boost::log;
101 
102  const dds::user_defaults_api::CUserDefaults& userDefaults =
104 
105  std::string sLogFile = userDefaults.getLogFile();
106 
107  unsigned int rotationSize = userDefaults.getOptions().m_server.m_logRotationSize;
108  unsigned int severityLevel = userDefaults.getOptions().m_server.m_logSeverityLevel;
109  unsigned int hasConsoleOutput = userDefaults.getOptions().m_server.m_logHasConsoleOutput;
110 
111  // Default format for logger
112  boost::log::formatter formatter =
113  // TODO: std::setw doesn't work for the first collumn of the log (TimeStamp). Investigate!
114  expressions::stream << std::left << expressions::format_date_time<boost::posix_time::ptime>(
115  "TimeStamp", "%Y-%m-%d %H:%M:%S.%f")
116  << " " << std::setw(7) << expressions::attr<ELogSeverityLevel>("Severity")
117  << std::setw(20) << expressions::attr<std::string>("Process") << " <"
118  << expressions::attr<attributes::current_process_id::value_type>("ProcessID") << ":"
119  << expressions::attr<attributes::current_thread_id::value_type>("ThreadID")
120  << "> " << expressions::smessage;
121 
122  // Logging to file
123  boost::shared_ptr<sinks::synchronous_sink<sinks::text_file_backend>> fileSink =
124  add_file_log(keywords::file_name = sLogFile,
125  keywords::open_mode = (std::ios::out | std::ios::app),
126  keywords::rotation_size = rotationSize * 1024 * 1024,
127  // rotate at midnight every day
128  keywords::time_based_rotation = sinks::file::rotation_at_time_point(0, 0, 0),
129  // log collector,
130  // -- maximum total size of the stored log files is 1GB.
131  // -- minimum free space on the drive is 2GB
132  keywords::max_size = 1000 * 1024 * 1024,
133  keywords::min_free_space = 2000 * 1024 * 1024,
134  keywords::auto_flush = true);
135 
136  fileSink->set_formatter(formatter);
137  fileSink->set_filter(severity >= severityLevel);
138 
139  // Logging to console
140  boost::shared_ptr<sinks::synchronous_sink<sinks::text_ostream_backend>> consoleSTDOUTSink =
141  add_console_log(std::cout, boost::log::keywords::format = "%Process%: %Message%");
142  boost::shared_ptr<sinks::synchronous_sink<sinks::text_ostream_backend>> consoleSTDOUTCleanSink =
143  add_console_log(std::cout, boost::log::keywords::format = "%Message%");
144  boost::shared_ptr<sinks::synchronous_sink<sinks::text_ostream_backend>> consoleSTDERRSink =
145  add_console_log(std::cerr, boost::log::keywords::format = "%Process%: error: %Message%");
146 
147  consoleSTDOUTSink->set_filter(severity == log_stdout && hasConsoleOutput);
148  consoleSTDOUTCleanSink->set_filter(severity == log_stdout_clean && hasConsoleOutput);
149  consoleSTDERRSink->set_filter(severity == log_stderr && hasConsoleOutput);
150 
151  add_common_attributes();
152  core::get()->add_global_attribute("Process", attributes::current_process_name());
153 
154  LOG(info) << "Log engine is initialized with severety \""
155  << userDefaults.getOptions().m_server.m_logSeverityLevel << "\"";
156  }
157 
158  private:
159  logger_t fLogger;
160  };
161 };
162 #endif
Definition: def.h:156
Definition: def.h:154
ELogSeverityLevel
Log Severity levels.
Definition: def.h:144
std::string getLogFile() const
Definition: UserDefaults.cpp:287
Definition: def.h:155
const SDDSUserDefaultsOptions_t getOptions() const
Definition: UserDefaults.cpp:190
#define LOG(severity)
Definition: Logger.h:54
unsigned int m_logRotationSize
True if output log also to console.
Definition: dds-user-defaults/src/Options.h:30
bool m_logHasConsoleOutput
Idle time in [s] after which process will be killed by monitoring thread.
Definition: dds-user-defaults/src/Options.h:32
Definition: UserDefaults.h:18
static CUserDefaults & instance()
Return singleton instance.
Definition: UserDefaults.cpp:40
MiscCommon::ELogSeverityLevel m_logSeverityLevel
Log rotation size in MB.
Definition: dds-user-defaults/src/Options.h:28
BOOST_LOG_ATTRIBUTE_KEYWORD(severity, "Severity", ELogSeverityLevel) class Logger
Definition: Logger.h:72
Definition: def.h:150
SDDSServerOptions m_server
Definition: dds-user-defaults/src/Options.h:40
Miscellaneous functions and helpers are located here.
Definition: BOOST_FILESYSTEM.h:21