CppLogging 1.0.5.0
C++ Logging Library
Loading...
Searching...
No Matches
logger.inl
Go to the documentation of this file.
1
9namespace CppLogging {
10
11inline Logger::Logger(const std::string& name, const std::shared_ptr<Processor>& sink) : _name(name), _sink(sink)
12{
13}
14
15inline Logger::~Logger()
16{
17 Flush();
18}
19
20template <typename... T>
21inline 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
55template <typename... T>
56inline 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
65template <typename... T>
66inline void Logger::Info(fmt::format_string<T...> message, T&&... args) const
67{
68 Log(Level::INFO, false, message, std::forward<T>(args)...);
69}
70
71template <typename... T>
72inline void Logger::Warn(fmt::format_string<T...> message, T&&... args) const
73{
74 Log(Level::WARN, false, message, std::forward<T>(args)...);
75}
76
77template <typename... T>
78inline void Logger::Error(fmt::format_string<T...> message, T&&... args) const
79{
80 Log(Level::ERROR, false, message, std::forward<T>(args)...);
81}
82
83template <typename... T>
84inline void Logger::Fatal(fmt::format_string<T...> message, T&&... args) const
85{
86 Log(Level::FATAL, false, message, std::forward<T>(args)...);
87}
88
89inline 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
Level level
Level of the logging record.
Definition record.h:44
uint64_t thread
Thread Id of the logging record.
Definition record.h:42
std::string logger
Logger name of the logging record.
Definition record.h:46
Record & Format(fmt::format_string< T... > pattern, T &&... args)
Format message and its arguments.
Definition record.inl:361
uint64_t timestamp
Timestamp of the logging record.
Definition record.h:40
Record & StoreFormat(fmt::format_string< T... > pattern, T &&... args)
Store format message and its arguments.
Definition record.inl:368
C++ Logging project definitions.
Definition appender.h:15
Level
Logging level.
Definition level.h:18