Toolbox snapshot
The Reactive C++ Toolbox
Loading...
Searching...
No Matches
Logger.hpp
Go to the documentation of this file.
1// The Reactive C++ Toolbox.
2// Copyright (C) 2013-2019 Swirly Cloud Limited
3// Copyright (C) 2022 Reactive Markets Limited
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16
17#ifndef TOOLBOX_SYS_LOGGER_HPP
18#define TOOLBOX_SYS_LOGGER_HPP
19
20#include <boost/lockfree/queue.hpp>
21#include <thread>
22
24#include <toolbox/sys/Time.hpp>
26
27namespace toolbox {
28inline namespace sys {
29
30class Logger;
31
32enum class LogLevel : int {
34 None,
36 Crit,
38 Error,
40 Warn,
42 Metric,
44 Notice,
46 Info,
48 Debug
49};
50
52
55
59
62
65
68
71{
72 return level <= get_log_level();
73}
74
77
80
82TOOLBOX_API Logger& set_logger(Logger& logger) noexcept;
83
88
92TOOLBOX_API void write_log(WallTime ts, LogLevel level, LogMsgPtr&& msg, std::size_t size) noexcept;
93
97 public:
98 Logger() noexcept = default;
99 virtual ~Logger();
100
101 // Copy.
102 Logger(const Logger&) noexcept = default;
103 Logger& operator=(const Logger&) noexcept = default;
104
105 // Move.
106 Logger(Logger&&) noexcept = default;
107 Logger& operator=(Logger&&) noexcept = default;
108
109 void write_log(WallTime ts, LogLevel level, int tid, LogMsgPtr&& msg, std::size_t size) noexcept
110 {
111 do_write_log(ts, level, tid, std::move(msg), size);
112 }
113
114 protected:
115 virtual void do_write_log(WallTime ts, LogLevel level, int tid, LogMsgPtr&& msg,
116 std::size_t size) noexcept
117 = 0;
118};
119
121 struct Task {
122 WallTime ts;
123 LogLevel level;
124 int tid;
125 void* msg;
126 std::size_t size;
127 };
128
129 public:
130 explicit AsyncLogger(Logger& logger);
131 ~AsyncLogger() override;
132
133 // Copy.
134 AsyncLogger(const AsyncLogger&) = delete;
136
137 // Move.
140
143 bool run();
144
146 void stop();
147
148 private:
149 void write_all_messages();
150 void do_write_log(WallTime ts, LogLevel level, int tid, LogMsgPtr&& msg,
151 std::size_t size) noexcept override;
152
153 Logger& logger_;
154 boost::lockfree::queue<Task, boost::lockfree::fixed_sized<true>> tq_{512};
155 std::atomic<bool> stop_{false};
156};
157
161 public:
162 explicit ScopedLogLevel(LogLevel level) noexcept
163 : prev_{set_log_level(level)}
164 {
165 }
167
168 // Copy.
171
172 // Move.
175
176 private:
177 const LogLevel prev_;
178};
179
183 public:
184 explicit ScopedLogger(Logger& logger) noexcept
185 : prev_{set_logger(logger)}
186 {
187 }
189
190 // Copy.
191 ScopedLogger(const ScopedLogger&) = delete;
193
194 // Move.
197
198 private:
199 Logger& prev_;
200};
201
202inline std::ostream& operator<<(std::ostream& os, LogLevel level)
203{
204 return os << log_label(level);
205}
206
207} // namespace sys
208} // namespace toolbox
209
210#endif // TOOLBOX_SYS_LOGGER_HPP
#define TOOLBOX_API
Definition Config.h:39
AsyncLogger & operator=(AsyncLogger &&)=delete
AsyncLogger(const AsyncLogger &)=delete
AsyncLogger & operator=(const AsyncLogger &)=delete
AsyncLogger(AsyncLogger &&)=delete
Logger() noexcept=default
virtual void do_write_log(WallTime ts, LogLevel level, int tid, LogMsgPtr &&msg, std::size_t size) noexcept=0
ScopedLogLevel(LogLevel level) noexcept
Definition Logger.hpp:162
ScopedLogLevel & operator=(ScopedLogLevel &&)=delete
ScopedLogLevel & operator=(const ScopedLogLevel &)=delete
ScopedLogLevel(const ScopedLogLevel &)=delete
ScopedLogLevel(ScopedLogLevel &&)=delete
ScopedLogger & operator=(const ScopedLogger &)=delete
ScopedLogger & operator=(ScopedLogger &&)=delete
ScopedLogger(const ScopedLogger &)=delete
ScopedLogger(ScopedLogger &&)=delete
ScopedLogger(Logger &logger) noexcept
Definition Logger.hpp:184
STL namespace.
ostream & operator<<(ostream &os, const pair< T, U > &p)
Definition Parser.ut.cpp:29
bool is_log_level(LogLevel level) noexcept
Return true if level is less than or equal to current log level.
Definition Logger.hpp:70
const char * log_label(LogLevel level) noexcept
Return log label for given log level.
Definition Logger.cpp:155
Logger & set_logger(Logger &logger) noexcept
Set logger globally for all threads.
Definition Logger.cpp:175
Logger & get_logger() noexcept
Return current logger.
Definition Logger.cpp:170
void write_log(WallTime ts, LogLevel level, LogMsgPtr &&msg, std::size_t size) noexcept
Definition Logger.cpp:180
Logger & null_logger() noexcept
Null logger. This logger does nothing and is effectively /dev/null.
Definition Logger.cpp:140
WallClock::time_point WallTime
Definition Time.hpp:111
StoragePtr< MaxLogLine > LogMsgPtr
Definition Logger.hpp:51
LogLevel get_log_level() noexcept
Return current log level.
Definition Logger.cpp:160
Logger & std_logger() noexcept
Definition Logger.cpp:145
LogLevel set_log_level(LogLevel level) noexcept
Set log level globally for all threads.
Definition Logger.cpp:165
Logger & sys_logger() noexcept
System logger. This logger calls syslog().
Definition Logger.cpp:150
constexpr std::size_t size(const detail::Struct< detail::Member< TagsT, ValuesT >... > &s)
Definition Struct.hpp:98
std::unique_ptr< void, detail::StorageDeleter< SizeN > > StoragePtr
Definition Storage.hpp:34
constexpr auto bind() noexcept
Definition Slot.hpp:92