CppLogging  1.0.4.0
C++ Logging Library
logger.inl
Go to the documentation of this file.
1 
9 namespace CppLogging {
10 
11 inline Logger::Logger(const std::string& name, const std::shared_ptr<Processor>& sink) : _name(name), _sink(sink)
12 {
13 }
14 
15 inline Logger::~Logger()
16 {
17  Flush();
18 }
19 
20 template <typename... T>
21 inline void Logger::Log(Level level, bool format, fmt::format_string<T...> message, T&&... args) const
22 {
23  // Thread local thread Id
24  thread_local uint64_t thread = CppCommon::Thread::CurrentThreadId();
25  // Thread local instance of the logging record
26  thread_local Record record;
27 
28  // Clear the logging record
29  record.Clear();
30 
31  // Fill necessary fields of the logging record
32  record.timestamp = CppCommon::Timestamp::utc();
33  record.thread = thread;
34  record.level = level;
35  record.logger = _name;
36 
37  // Check for valid and started logging sink
38  if (_sink && _sink->IsStarted())
39  {
40  // Filter the logging record
41  if (!_sink->FilterRecord(record))
42  return;
43 
44  // Format or serialize arguments list
45  if (format)
46  record.Format(message, std::forward<T>(args)...);
47  else
48  record.StoreFormat(message, std::forward<T>(args)...);
49 
50  // Process the logging record
51  _sink->ProcessRecord(record);
52  }
53 }
54 
55 template <typename... T>
56 inline void Logger::Debug(fmt::format_string<T...> message, T&&... args) const
57 {
58 #if defined(NDEBUG)
59  // Log nothing in release mode...
60 #else
61  Log(Level::DEBUG, false, message, std::forward<T>(args)...);
62 #endif
63 }
64 
65 template <typename... T>
66 inline void Logger::Info(fmt::format_string<T...> message, T&&... args) const
67 {
68  Log(Level::INFO, false, message, std::forward<T>(args)...);
69 }
70 
71 template <typename... T>
72 inline void Logger::Warn(fmt::format_string<T...> message, T&&... args) const
73 {
74  Log(Level::WARN, false, message, std::forward<T>(args)...);
75 }
76 
77 template <typename... T>
78 inline void Logger::Error(fmt::format_string<T...> message, T&&... args) const
79 {
80  Log(Level::ERROR, false, message, std::forward<T>(args)...);
81 }
82 
83 template <typename... T>
84 inline void Logger::Fatal(fmt::format_string<T...> message, T&&... args) const
85 {
86  Log(Level::FATAL, false, message, std::forward<T>(args)...);
87 }
88 
89 inline void Logger::Flush()
90 {
91  if (_sink && _sink->IsStarted())
92  _sink->Flush();
93 }
94 
95 } // namespace CppLogging
Logger()
Initialize default logger.
Definition: logger.cpp:15
Logging record.
Definition: record.h:37
void Clear()
Clear logging record.
Definition: record.inl:449
C++ Logging project definitions.
Definition: appender.h:15
Level
Logging level.
Definition: level.h:18