CatapultServer  v0.5.0.1 (Elephant)
Logging.h
Go to the documentation of this file.
1 
21 #pragma once
22 #include "PathUtils.h"
23 #include <boost/log/attributes/constant.hpp>
24 #include <boost/log/core.hpp>
25 #include <boost/log/detail/light_rw_mutex.hpp>
26 #include <boost/log/sources/global_logger_storage.hpp>
27 #include <boost/log/sources/threading_models.hpp>
28 #include <boost/log/trivial.hpp>
29 #include <boost/thread/lock_guard.hpp>
30 
31 namespace catapult { namespace utils {
32 
33  // region LogLevel
34 
36  enum class LogLevel {
38  Trace = boost::log::trivial::trace,
39 
42 
45 
48 
50  Error = boost::log::trivial::error,
51 
53  Fatal = boost::log::trivial::fatal,
54 
56  Min = Trace,
57 
59  Max = Fatal,
60  };
61 
62  // endregion
63 
64  // region LogSinkType
65 
67  enum class LogSinkType {
69  Sync,
70 
72  Async
73  };
74 
75  // endregion
76 
77  // region LogColorMode
78 
80  enum class LogColorMode {
82  Ansi,
83 
85  AnsiBold,
86 
88  None
89  };
90 
91  // endregion
92 
93  // region LogFilter
94 
96  struct LogFilter {
97  public:
99  explicit LogFilter(LogLevel level);
100 
102  ~LogFilter();
103 
104  public:
106  boost::log::filter toBoostFilter() const;
107 
108  public:
110  void setLevel(const char* name, LogLevel level);
111 
112  private:
113  class Impl;
114  std::unique_ptr<Impl> m_pImpl;
115  };
116 
117  // endregion
118 
119  // region [Basic|File]LoggerOptions
120 
123  public:
128  {}
129 
132 
135  };
136 
140  explicit FileLoggerOptions(const std::string& directory, const std::string& filePattern)
141  : Directory(directory)
142  , FilePattern(filePattern)
143  {}
144 
146  std::string Directory;
147 
149  std::string FilePattern;
150 
152  uint64_t RotationSize = 25u * 1024 * 1024;
153 
155  uint64_t MaxTotalSize = 100u * 25 * 1024 * 1024;
156 
158  uint64_t MinFreeSpace = 100u * 1024 * 1024;
159  };
160 
161  // endregion
162 
163  // region LoggingBootstrapper
164 
166  class LoggingBootstrapper final {
167  public:
170 
173 
174  public:
176  void addConsoleLogger(const BasicLoggerOptions& options, const LogFilter& filter);
177 
179  void addFileLogger(const FileLoggerOptions& options, const LogFilter& filter);
180 
181  private:
182  class Impl;
183  std::unique_ptr<Impl> m_pImpl;
184  };
185 
186  // endregion
187 
190  void CatapultLogFlush();
191 
192  // region boost logging configuration and utils
193 
194  namespace log {
196  template<typename TAttributes>
197  class EraseOnExit {
198  private:
199  using IteratorType = typename std::remove_reference_t<TAttributes>::iterator;
200 
201  public:
202  EraseOnExit(TAttributes& attrs, IteratorType& iter) : m_attrs(attrs), m_iter(iter)
203  {}
204 
206  if (m_iter != m_attrs.end())
207  m_attrs.erase(m_iter);
208  }
209 
210  private:
211  TAttributes& m_attrs;
213  };
214 
216  template<typename TBase, typename TTraits>
217  class custom_info_tagger_feature : public TBase {
218  public:
219  using char_type = typename TBase::char_type;
220  using threading_model = typename TBase::threading_model;
221 
222  public:
224  {}
225 
226  custom_info_tagger_feature(const custom_info_tagger_feature& rhs) : TBase(static_cast<const TBase&>(rhs))
227  {}
228 
229  template<typename TArgs>
230  custom_info_tagger_feature(const TArgs& args) : TBase(args)
231  {}
232 
233  public:
234  // choose the most restrictive lock
235  using open_record_lock = typename boost::log::strictest_lock<
236  boost::lock_guard<threading_model>,
237  typename TBase::open_record_lock,
238  typename TBase::add_attribute_lock,
239  typename TBase::remove_attribute_lock
240  >::type;
241 
242  protected:
243  template<typename TArgs>
244  boost::log::record open_record_unlocked(const TArgs& args) {
245  // extract value from parameter pack
246  const auto& keyword = boost::parameter::keyword<typename TTraits::TagType>::instance;
247  const auto& value = args[keyword];
248 
249  // add as a new attribute
250  auto& attrs = TBase::attributes();
251  auto result = TBase::add_attribute_unlocked(
253  boost::log::attributes::constant<typename TTraits::Type>(value));
254  auto iter = result.second ? result.first : attrs.end();
255 
256  // remove attribute upon scope exit
257  EraseOnExit<decltype(attrs)> eraseGuard(attrs, iter);
258 
259  // forward to the base
260  return TBase::open_record_unlocked(args);
261  }
262  };
263 
265  template<typename TCustomFeatureTraits>
267  template<typename TBase>
268  struct apply {
270  };
271  };
272 
274  namespace keywords {
275  BOOST_PARAMETER_KEYWORD(tag, line)
276  BOOST_PARAMETER_KEYWORD(tag, file)
277  BOOST_PARAMETER_KEYWORD(tag, subcomponent)
278  }
279 
282  using Type = unsigned int;
283  using TagType = keywords::tag::line;
284  static constexpr auto Name = "Line";
285  };
286 
288  struct FilenameTraits {
289  using Type = const char*;
290  using TagType = keywords::tag::file;
291  static constexpr auto Name = "File";
292  };
293 
296  using Type = RawString;
297  using TagType = keywords::tag::subcomponent;
298  static constexpr auto Name = "Subcomponent";
299  };
300 
303  public boost::log::sources::basic_composite_logger<
304  char,
305  catapult_logger,
306  boost::log::sources::multi_thread_model<boost::log::aux::light_rw_mutex>,
307  boost::log::sources::features<
308  boost::log::sources::severity<boost::log::trivial::severity_level>,
309  custom_info_tagger<LineNumberTraits>,
310  custom_info_tagger<FilenameTraits>,
311  custom_info_tagger<SubcomponentTraits>>> {
312  // generate forwarding constructors
313  BOOST_LOG_FORWARD_LOGGER_MEMBERS_TEMPLATE(catapult_logger)
314  };
315 
317  BOOST_LOG_INLINE_GLOBAL_LOGGER_DEFAULT(global_logger, catapult::utils::log::catapult_logger)
318  }
319 
320  // endregion
321 }}
322 
323 #define CATAPULT_LOG_WITH_LOGGER_LEVEL_TAG(LOGGER, LEVEL, TAG) \
324  BOOST_LOG_STREAM_WITH_PARAMS( \
325  (LOGGER), \
326  (::catapult::utils::log::keywords::file = (::catapult::utils::ExtractFilename(__FILE__))) \
327  (::catapult::utils::log::keywords::line = (static_cast<unsigned int>(__LINE__))) \
328  (::catapult::utils::log::keywords::subcomponent = (TAG)) \
329  (::boost::log::keywords::severity = (static_cast<boost::log::trivial::severity_level>(LEVEL))))
330 
332 #define CATAPULT_LOG_WITH_LOGGER_LEVEL(LOGGER, LEVEL) \
333  CATAPULT_LOG_WITH_LOGGER_LEVEL_TAG(LOGGER, LEVEL, (::catapult::utils::ExtractDirectoryName(__FILE__)))
334 
336 #define CATAPULT_LOG_LEVEL(LEVEL) \
337  CATAPULT_LOG_WITH_LOGGER_LEVEL(::catapult::utils::log::global_logger::get(), LEVEL)
338 
340 #define CATAPULT_LOG(SEV) \
341  CATAPULT_LOG_WITH_LOGGER_LEVEL( \
342  ::catapult::utils::log::global_logger::get(), \
343  (static_cast<::catapult::utils::LogLevel>(boost::log::trivial::SEV)))
ColorMode
static constexpr auto ColorMode
Definition: Logging.cpp:90
catapult::utils::@1428::Colors
Colors
Definition: Logging.cpp:65
Parser.debug
def debug(*args)
Definition: Parser.py:46
catapult::utils::log::FilenameTraits::Type
const char * Type
Definition: Logging.h:289
catapult::utils::LoggingBootstrapper::addConsoleLogger
void addConsoleLogger(const BasicLoggerOptions &options, const LogFilter &filter)
Adds a console logger with the specified options and filter.
Definition: Logging.cpp:253
catapult::utils::log::custom_info_tagger_feature::custom_info_tagger_feature
custom_info_tagger_feature(const custom_info_tagger_feature &rhs)
Definition: Logging.h:226
catapult::utils::log::catapult_logger
Catapult logger type.
Definition: Logging.h:302
catapult::utils::LogFilter::Impl::m_overrideLevels
OverrideLevelsMap m_overrideLevels
Definition: Logging.cpp:181
catapult::utils::log::LineNumberTraits
Traits for attaching a line number to a log record.
Definition: Logging.h:281
catapult::utils::BasicLoggerOptions::BasicLoggerOptions
BasicLoggerOptions()
Creates default options.
Definition: Logging.h:125
catapult::utils::log::FilenameTraits::TagType
keywords::tag::file TagType
Definition: Logging.h:290
catapult::utils::FileLoggerOptions::FilePattern
std::string FilePattern
Log filename pattern.
Definition: Logging.h:149
catapult::utils::LogFilter::~LogFilter
~LogFilter()
Destroys the log filter.
catapult::utils::LogColorMode::Ansi
Ansi codes.
catapult::CountOf
constexpr size_t CountOf(T const (&)[N]) noexcept
Returns the size of the specified array.
Definition: src/catapult/types.h:113
colorPrint.warning
def warning(*args)
Definition: colorPrint.py:10
catapult::utils::log::EraseOnExit::EraseOnExit
EraseOnExit(TAttributes &attrs, IteratorType &iter)
Definition: Logging.h:202
catapult::utils::LogColorMode
LogColorMode
Catapult (console) log color modes.
Definition: Logging.h:80
catapult::utils::LogFilter::Impl
Definition: Logging.cpp:164
catapult::utils::LogFilter
Filter used for filtering logs by level and/or component.
Definition: Logging.h:96
catapult::utils::LoggingBootstrapper::Impl::m_sinks
std::vector< SinkPointer > m_sinks
Definition: Logging.cpp:240
catapult::utils::log::EraseOnExit::IteratorType
typename std::remove_reference_t< TAttributes >::iterator IteratorType
Definition: Logging.h:199
catapult::utils::LogFilter::m_pImpl
std::unique_ptr< Impl > m_pImpl
Definition: Logging.h:113
catapult::utils::LogLevel::Fatal
Level for logging fatal events.
catapult::utils::LogSinkType::Sync
A synchronous sink.
catapult::utils::CatapultLogFlush
void CatapultLogFlush()
Definition: Logging.cpp:283
catapult::utils::LogFilter::setLevel
void setLevel(const char *name, LogLevel level)
Sets the log level for the component specified by name.
Definition: Logging.cpp:198
catapult::utils::FileLoggerOptions::MinFreeSpace
uint64_t MinFreeSpace
Minimum size of free disk space in order to create log files.
Definition: Logging.h:158
catapult::utils::to_underlying_type
constexpr std::underlying_type_t< TEnum > to_underlying_type(TEnum value)
Converts a strongly typed enumeration value to its underlying integral value.
Definition: Casting.h:37
catapult::utils::LoggingBootstrapper::~LoggingBootstrapper
~LoggingBootstrapper()
Destroys the bootstrapper.
catapult::utils::FileLoggerOptions::Directory
std::string Directory
Log directory.
Definition: Logging.h:146
catapult::utils::BasicLoggerOptions
Basic logger options.
Definition: Logging.h:122
catapult::utils::log::custom_info_tagger::apply
Definition: Logging.h:268
catapult::utils::log::SubcomponentTraits::TagType
keywords::tag::subcomponent TagType
Definition: Logging.h:297
catapult::utils::LoggingBootstrapper::Impl::addBackend
void addBackend(const boost::shared_ptr< TBackend > &pBackend, const BasicLoggerOptions &options, const LogFilter &filter)
Definition: Logging.cpp:218
catapult::utils::LogLevel::Error
Level for logging error events.
catapult::utils::LogColorMode::AnsiBold
Bold ansi codes.
catapult::utils::LogFilter::Impl::setLevel
void setLevel(const char *name, LogLevel level)
Definition: Logging.cpp:175
catapult::utils::log::SubcomponentTraits::Name
static constexpr auto Name
Definition: Logging.h:298
BitwiseEnum.h
catapult::utils::log::EraseOnExit::m_attrs
TAttributes & m_attrs
Definition: Logging.h:211
catapult::utils::LogLevel::Debug
Level for logging debug events.
catapult::utils::LogLevel::Info
Level for logging informational events.
catapult::utils::LogLevel::Trace
Level for logging trace events.
catapult::utils::log::LineNumberTraits::Name
static constexpr auto Name
Definition: Logging.h:284
catapult::utils::FileLoggerOptions::FileLoggerOptions
FileLoggerOptions(const std::string &directory, const std::string &filePattern)
Creates options that specify the creation of log files with the pattern filePattern in the directory.
Definition: Logging.h:140
catapult::utils::LogSinkType
LogSinkType
Catapult log sink types.
Definition: Logging.h:67
catapult::utils::LoggingBootstrapper::Impl::addSink
void addSink(const TSinkPointer &pSink, LogColorMode colorMode, const LogFilter &filter)
Definition: Logging.cpp:232
catapult::utils::LogLevel
LogLevel
Catapult log levels.
Definition: Logging.h:36
catapult::utils::log::SubcomponentTraits
Traits for attaching a subcomponent tag to a log record.
Definition: Logging.h:295
catapult::utils::log::custom_info_tagger_feature::threading_model
typename TBase::threading_model threading_model
Definition: Logging.h:220
forwardsValidation.info
def info(*args)
Definition: forwardsValidation.py:12
catapult::utils::LoggingBootstrapper::addFileLogger
void addFileLogger(const FileLoggerOptions &options, const LogFilter &filter)
Adds a file logger with the specified options and filter.
Definition: Logging.cpp:263
catapult::utils::log::custom_info_tagger_feature::custom_info_tagger_feature
custom_info_tagger_feature()
Definition: Logging.h:223
catapult::utils::FileLoggerOptions
File logger options.
Definition: Logging.h:138
catapult::utils::BasicLoggerOptions::ColorMode
LogColorMode ColorMode
Log color mode.
Definition: Logging.h:134
catapult::utils::LogColorMode::None
No coloring.
Name
static constexpr auto Name
Definition: PtSynchronizer.cpp:31
catapult::utils::LoggingBootstrapper
Bootstraps boost logging.
Definition: Logging.h:166
catapult::utils::LogFilter::LogFilter
LogFilter(LogLevel level)
Creates a log filter that sets the default log level across all components.
Definition: Logging.cpp:188
catapult::utils::RawString
A const string buffer.
Definition: RawBuffer.h:68
catapult::utils::operator<<
std::ostream & operator<<(std::ostream &out, const BlockSpan &blockSpan)
Insertion operator for outputting blockSpan to out.
Definition: BlockSpan.cpp:27
catapult::utils::LogFilter::toBoostFilter
boost::log::filter toBoostFilter() const
Creates an equivalent boost log filter.
Definition: Logging.cpp:194
catapult::utils::LoggingBootstrapper::Impl
Definition: Logging.cpp:206
catapult::utils::log::SubcomponentTraits::Type
RawString Type
Definition: Logging.h:296
catapult::utils::log::custom_info_tagger_feature
A custom logging feature that allows tagging a log record with custom information.
Definition: Logging.h:217
catapult::utils::log::FilenameTraits
Traits for attaching a filename to a log record.
Definition: Logging.h:288
catapult::utils::LogLevel::Max
Maximum log level.
catapult::utils::log::custom_info_tagger_feature::char_type
typename TBase::char_type char_type
Definition: Logging.h:219
catapult::utils::log::EraseOnExit::m_iter
IteratorType & m_iter
Definition: Logging.h:212
types.h
catapult::utils::log::custom_info_tagger_feature::custom_info_tagger_feature
custom_info_tagger_feature(const TArgs &args)
Definition: Logging.h:230
catapult::utils::LoggingBootstrapper::m_pImpl
std::unique_ptr< Impl > m_pImpl
Definition: Logging.h:182
catapult::utils::log::LineNumberTraits::Type
unsigned int Type
Definition: Logging.h:282
catapult::utils::LogFilter::Impl::m_defaultLevel
LogLevel m_defaultLevel
Definition: Logging.cpp:180
catapult::utils::LogSinkType::Async
An asynchronous sink.
catapult
Definition: AddressExtractionExtension.cpp:28
catapult::utils::log::FilenameTraits::Name
static constexpr auto Name
Definition: Logging.h:291
catapult::utils::log::custom_info_tagger_feature::open_record_lock
typename boost::log::strictest_lock< boost::lock_guard< threading_model >, typename TBase::open_record_lock, typename TBase::add_attribute_lock, typename TBase::remove_attribute_lock >::type open_record_lock
Definition: Logging.h:240
catapult::utils::LogLevel::Min
Minimum log level.
Logging.h
catapult::utils::LoggingBootstrapper::LoggingBootstrapper
LoggingBootstrapper()
Creates a bootstrapper.
Definition: Logging.cpp:247
catapult::utils::BasicLoggerOptions::SinkType
LogSinkType SinkType
Log sink type.
Definition: Logging.h:131
catapult::utils::LoggingBootstrapper::Impl::~Impl
~Impl()
Definition: Logging.cpp:208
PathUtils.h
catapult::utils::log::LineNumberTraits::TagType
keywords::tag::line TagType
Definition: Logging.h:283
catapult::utils::LogFilter::Impl::setLevel
void setLevel(LogLevel level)
Definition: Logging.cpp:171
catapult::utils::LogLevel::Warning
Level for logging warning events.
catapult::utils::log::EraseOnExit::~EraseOnExit
~EraseOnExit()
Definition: Logging.h:205
catapult::utils::log::custom_info_tagger
Allows custom_info_tagger_feature to be used as a source feature.
Definition: Logging.h:266
catapult::utils::log::EraseOnExit
Removes an item from an attributes collection on destruction.
Definition: Logging.h:197
catapult::utils::LogFilter::Impl::toBoostFilter
boost::log::filter toBoostFilter() const
Definition: Logging.cpp:166
catapult::utils::FileLoggerOptions::RotationSize
uint64_t RotationSize
File rotation size.
Definition: Logging.h:152
catapult::utils::FileLoggerOptions::MaxTotalSize
uint64_t MaxTotalSize
Maximum size of all log files.
Definition: Logging.h:155
catapult::utils::log::custom_info_tagger_feature::open_record_unlocked
boost::log::record open_record_unlocked(const TArgs &args)
Definition: Logging.h:244