Loading...
Searching...
No Matches
logger.hpp
1/*---------------------------------------------------------------------------*\
2 *
3 * bitpit
4 *
5 * Copyright (C) 2015-2021 OPTIMAD engineering Srl
6 *
7 * -------------------------------------------------------------------------
8 * License
9 * This file is part of bitpit.
10 *
11 * bitpit is free software: you can redistribute it and/or modify it
12 * under the terms of the GNU Lesser General Public License v3 (LGPL)
13 * as published by the Free Software Foundation.
14 *
15 * bitpit is distributed in the hope that it will be useful, but WITHOUT
16 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
18 * License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with bitpit. If not, see <http://www.gnu.org/licenses/>.
22 *
23\*---------------------------------------------------------------------------*/
24#ifndef __BITPIT_LOGGER_HPP__
25#define __BITPIT_LOGGER_HPP__
26
27#include "compiler.hpp"
28
29#include <memory>
30#include <ostream>
31#include <unordered_map>
32#include <vector>
33
34#include "bitpit_common.hpp"
35
36#define BITPIT_DEBUG_COUT(...) BITPIT_OVERLOAD_CALL(BITPIT_DEBUG_COUT, __VA_ARGS__)
37#if BITPIT_ENABLE_DEBUG
38#define BITPIT_DEBUG_COUT_0() bitpit::log::cout()
39#define BITPIT_DEBUG_COUT_1(NAME) bitpit::log::cout(NAME)
40#else
41#define BITPIT_DEBUG_COUT_0() while(0) bitpit::log::cout()
42#define BITPIT_DEBUG_COUT_1(NAME) while(0) bitpit::log::cout(NAME)
43#endif
44
45namespace bitpit{
46
47namespace log {
48 enum Mode {
49 MODE_UNDEFINED = -1,
50 MODE_SEPARATE = 0,
51 MODE_COMBINE,
52 COMBINED = MODE_COMBINE,
53 SEPARATE = MODE_SEPARATE
54 };
55
56 enum Level {
57 LEVEL_QUIET = 60,
58 LEVEL_CRITICAL = 50,
59 LEVEL_ERROR = 40,
60 LEVEL_WARNING = 30,
61 LEVEL_INFO = 20,
62 LEVEL_DEBUG = 10,
63 LEVEL_NOTSET = 0,
64 QUIET = LEVEL_QUIET,
65 NORMAL = LEVEL_INFO,
66 DEBUG = LEVEL_DEBUG
67 };
68
69 enum Visibility {
70 VISIBILITY_NOTSET = -1,
71 VISIBILITY_MASTER = 0,
72 VISIBILITY_GLOBAL,
73 MASTER = VISIBILITY_MASTER,
74 GLOBAL = VISIBILITY_GLOBAL,
75 };
76
77 typedef Level Severity;
78 typedef Level Priority;
80}
81
82// Logger buffer
83class LoggerBuffer : public std::streambuf
84{
85
86public:
87 struct Settings {
88 int indentation;
89 std::string context;
90 bool consoleTimestampEnabled;
91 bool fileTimestampEnabled;
92 };
93
94 LoggerBuffer(int nProcesses, int rank, std::size_t bufferSize = 256);
95 LoggerBuffer(const LoggerBuffer &other) = delete;
96 LoggerBuffer(LoggerBuffer &&other) = delete;
97
99
100 int getProcessCount() const;
101 int getRank() const;
102
103 void setConsoleEnabled(bool enabled);
104 void setConsoleStream(std::ostream *console);
105 std::ostream & getConsoleStream();
106
107 void setFileEnabled(bool enabled);
108 void setFileStream(std::ofstream *file);
109 std::ofstream & getFileStream();
110
111 const Settings * getSettings() const;
112 void setSettings(const std::shared_ptr<Settings> &settings);
113
114 int flush(bool terminate);
115
116private:
117 std::vector<char> m_buffer;
118
119 int m_nProcesses;
120 int m_rank;
121 std::string m_rankPrefix;
122
123 bool m_consoleEnabled;
124 std::ostream *m_console;
125
126 bool m_fileEnabled;
127 std::ofstream *m_file;
128
129 std::shared_ptr<Settings> m_settings;
130
131 std::string getTimestamp() const;
132
133 int_type overflow(int_type ch) override;
134 int sync() override;
135
136 int flushLine(std::ostream &stream, const char *begin, const char *end,
137 const std::string &timestamp, bool terminate);
138
139};
140
141// Logger
142class Logger : public std::ostream
143{
144
145friend class LoggerManager;
146
147public:
148 void setContext(const std::string &context);
149 std::string getContext();
150
151 void setIndentation(int delta);
152 int getIndentation();
153
154 void disable(log::Level = log::Level::LEVEL_CRITICAL);
155 void disableConsole(log::Level = log::Level::LEVEL_CRITICAL);
156 void disableFile(log::Level = log::Level::LEVEL_CRITICAL);
157
158 void setDefaultSeverity(log::Level severity);
160
163
164 void setDefaultVisibility(log::Visibility visibility);
165 log::Visibility getDefaultVisibility();
166
167 BITPIT_DEPRECATED(void setVisibility(log::Visibility visibility));
168 BITPIT_DEPRECATED(log::Visibility getVisibility());
169
170 void setVerbosities(log::Level threshold);
171 void setTimestampEnabled(bool enabled);
172
173 bool isConsoleTimestampEnabled() const;
174 void setConsoleTimestampEnabled(bool enabled);
175 void setConsoleVerbosity(log::Level threshold);
177
178 bool isFileTimestampEnabled() const;
179 void setFileTimestampEnabled(bool enabled);
180 void setFileVerbosity(log::Level threshold);
182
183 std::string getName() const;
184
185 void println(const std::string &message);
186 void println(const std::string &message, log::Level severity);
187 void println(const std::string &message, log::Visibility visibility);
188 void println(const std::string &message, const log::Level severity, log::Visibility visibility);
189
190 void print(const std::string &message);
191 void print(const std::string &message, log::Level severity);
192 void print(const std::string &message, log::Visibility visibility);
193 void print(const std::string &message, log::Level severity, log::Visibility visibility);
194
195 template<typename T>
196 void print(const T &value, log::Level severity, log::Visibility visibility)
197 {
198 // Format buffer
199 m_buffer->setSettings(m_bufferSettings);
200
201 // Enable buffer streams
202 enableBufferStreams(severity, visibility);
203
204 // Print the value
205 static_cast<std::ostream &>(*this) << value;
206 };
207
208private:
209 std::string m_name;
210
211 std::shared_ptr<LoggerBuffer> m_buffer;
212 std::shared_ptr<LoggerBuffer::Settings> m_bufferSettings;
213
214 log::Level m_defaultSeverity;
215 log::Visibility m_defaultVisibility;
216
217 log::Level m_consoleDisabledThreshold;
218 log::Level m_consoleVerbosityThreshold;
219
220 log::Level m_fileDisabledThreshold;
221 log::Level m_fileVerbosityThreshold;
222
223 Logger(const std::string &name, const std::shared_ptr<LoggerBuffer> &buffer);
224 Logger(const Logger &other) = delete;
225 Logger(Logger &&other) = delete;
226
227 void formatBuffer();
228 void enableBufferStreams(log::Level severity, log::Visibility visibility);
229
230};
231
232// Logger manager
234{
235
236public:
237 BITPIT_PUBLIC_API static std::string BITPIT_LOG_NAME;
238 BITPIT_PUBLIC_API static std::string BITPIT_LOG_DIRECTORY;
239
240 static LoggerManager & manager();
241
243
244 Logger & cout(log::Level defualtSeverity = log::Level::LEVEL_NOTSET, log::Visibility defualtVisibility = log::VISIBILITY_NOTSET);
245 Logger & cout(const std::string &name, log::Level defualtSeverity = log::Level::LEVEL_NOTSET, log::Visibility defualtVisibility = log::VISIBILITY_NOTSET);
246
247 Logger & critical(log::Visibility visibility = log::VISIBILITY_NOTSET);
248 Logger & critical(const std::string &name,log::Visibility visibility = log::VISIBILITY_NOTSET);
249
250 Logger & error(log::Visibility visibility = log::VISIBILITY_NOTSET);
251 Logger & error(const std::string &name, log::Visibility visibility = log::VISIBILITY_NOTSET);
252
253 Logger & warning(log::Visibility visibility = log::VISIBILITY_NOTSET);
254 Logger & warning(const std::string &name, log::Visibility visibility = log::VISIBILITY_NOTSET);
255
256 Logger & info(log::Visibility visibility = log::VISIBILITY_NOTSET);
257 Logger & info(const std::string &name, log::Visibility visibility = log::VISIBILITY_NOTSET);
258
259 Logger & debug(log::Visibility visibility = log::VISIBILITY_NOTSET);
260 Logger & debug(const std::string &name, log::Visibility visibility = log::VISIBILITY_NOTSET);
261
262 void initialize(log::Mode mode, bool reset,
263 int nProcesses, int rank);
264
265 void initialize(log::Mode mode, bool reset = false,
266 const std::string &directory = BITPIT_LOG_DIRECTORY,
267 int nProcesses = 1, int rank = 0);
268
269 void initialize(log::Mode mode, const std::string &name,
270 bool reset = false, const std::string &directory = BITPIT_LOG_DIRECTORY,
271 int nProcesses = 1, int rank = 0);
272
273 void create(const std::string &name, bool reset = false,
274 int nProcesses = 1, int rank = 0);
275
276 void create(const std::string &name, bool reset, const std::string &directory,
277 int nProcesses = 1, int rank = 0);
278
279 bool destroy(const std::string &name, bool force = false);
280
281 bool exists(const std::string &name) const;
282
283 bool isInitialized() const;
284
285 bool setMode(log::Mode mode);
286 log::Mode getMode() const;
287
288 void setVerbosities(log::Level threshold);
289 void setConsoleVerbosity(log::Level threshold);
290 void setFileVerbosity(log::Level threshold);
291
292 std::string getDefaultName() const;
293 std::string getDefaultDirectory() const;
294
295private:
296 static std::unique_ptr<LoggerManager> m_manager;
297
298 std::string m_defaultName;
299 std::string m_defaultDirectory;
300 log::Mode m_mode;
301
302 std::unordered_map<std::string, std::unique_ptr<Logger>> m_loggers;
303 std::unordered_map<std::string, int> m_loggerUsers;
304 std::unordered_map<std::string, std::unique_ptr<std::ofstream>> m_fileStreams;
305
307
308 LoggerManager(LoggerManager const&) = delete;
309 LoggerManager& operator=(LoggerManager const&) = delete;
310
311 void _create(const std::string &name, bool reset, const std::string &directory, int nProcesses, int rank);
312 void _create(const std::string &name, std::shared_ptr<LoggerBuffer> &buffer);
313
314};
315
321namespace log {
322
323 // Generic global functions
325
326 Logger & cout(log::Level defualtSeverity = log::Level::LEVEL_NOTSET, log::Visibility defualtVisibility = log::VISIBILITY_NOTSET);
327 Logger & cout(const std::string &name, log::Level defualtSeverity = log::Level::LEVEL_NOTSET, log::Visibility defualtVisibility = log::VISIBILITY_NOTSET);
328
329 Logger & critical(log::Visibility visibility = log::VISIBILITY_NOTSET);
330 Logger & critical(const std::string &name, log::Visibility visibility = log::VISIBILITY_NOTSET);
331
332 Logger & error(log::Visibility visibility = log::VISIBILITY_NOTSET);
333 Logger & error(const std::string &name, log::Visibility visibility = log::VISIBILITY_NOTSET);
334
335 Logger & warning(log::Visibility visibility = log::VISIBILITY_NOTSET);
336 Logger & warning(const std::string &name, log::Visibility visibility = log::VISIBILITY_NOTSET);
337
338 Logger & info(log::Visibility visibility = log::VISIBILITY_NOTSET);
339 Logger & info(const std::string &name, log::Visibility visibility = log::VISIBILITY_NOTSET);
340
341 Logger & debug(log::Visibility visibility = log::VISIBILITY_NOTSET);
342 Logger & debug(const std::string &name, log::Visibility visibility = log::VISIBILITY_NOTSET);
343
344 // Manipulators global functions
345
349 template<typename T>
351 Logger & (*f) (Logger &, const T &);
352 T value;
353
357 LoggerManipulator(Logger & (*ff)(Logger &, const T &), const T & ss)
358 : f(ff), value(ss) {
359
360 }
361 };
362
366 template<typename T>
368 {
369 return m.f(logger, m.value);
370 }
371
372 Logger & setContext(Logger &logger, const std::string &context);
373 LoggerManipulator<std::string> context(const std::string &context);
374
375 Logger & setDefaultSeverity(Logger &logger, const log::Level &severity);
376 LoggerManipulator<log::Level> defaultSeverity(const log::Level &severity);
377
379 BITPIT_DEPRECATED(LoggerManipulator<log::Level> priority(const log::Priority &priority));
380
381 BITPIT_DEPRECATED(Logger & setVisibility(Logger &logger, const log::Visibility &visibility));
382 BITPIT_DEPRECATED(LoggerManipulator<log::Visibility> visibility(const log::Visibility &visibility));
383
384 Logger & setDefaultVisibility(Logger &logger, const log::Visibility &visibility);
385 LoggerManipulator<log::Visibility> defaultVisibility(const log::Visibility &visibility);
386
387 Logger & setVerbosities(Logger &logger, const log::Level &threshold);
388 LoggerManipulator<log::Level> verbosities(const log::Level &threshold);
389
390 Logger & setConsoleVerbosity(Logger &logger, const log::Level &threshold);
391 LoggerManipulator<log::Level> consoleVerbosity(const log::Level &threshold);
392
393 Logger & setFileVerbosity(Logger &logger, const log::Level &threshold);
394 LoggerManipulator<log::Level> fileVerbosity(const log::Level &threshold);
395
396 Logger & disable(Logger &logger, const log::Level &verbosity = log::Level::LEVEL_CRITICAL);
397 LoggerManipulator<log::Level> disable(const log::Level &verbosity = log::Level::LEVEL_CRITICAL);
398
399 Logger & disableConsole(Logger &logger, const log::Level &verbosity = log::Level::LEVEL_CRITICAL);
400 LoggerManipulator<log::Level> disableConsole(const log::Level &verbosity = log::Level::LEVEL_CRITICAL);
401
402 Logger & disableFile(Logger &logger, const log::Level &verbosity = log::Level::LEVEL_CRITICAL);
403 LoggerManipulator<log::Level> disableFile(const log::Level &verbosity = log::Level::LEVEL_CRITICAL);
404
405 Logger & setIndentation(Logger &logger, const int &delta);
406 LoggerManipulator<int> indent(int delta);
407
408}
409
410template<typename T>
411bitpit::Logger & operator<<(bitpit::Logger &logger, const T &value)
412{
413 logger.print(value, logger.getDefaultSeverity(), logger.getDefaultVisibility());
414
415 return logger;
416}
417
418}
419
420#endif
Stream buffer for the message logger.
Definition logger.hpp:84
std::ofstream & getFileStream()
Definition logger.cpp:375
LoggerBuffer(int nProcesses, int rank, std::size_t bufferSize=256)
Definition logger.cpp:63
void setFileEnabled(bool enabled)
Definition logger.cpp:347
void setSettings(const std::shared_ptr< Settings > &settings)
Definition logger.cpp:395
int getProcessCount() const
Definition logger.cpp:110
const Settings * getSettings() const
Definition logger.cpp:385
void setFileStream(std::ofstream *file)
Definition logger.cpp:363
void setConsoleEnabled(bool enabled)
Definition logger.cpp:305
std::ostream & getConsoleStream()
Definition logger.cpp:333
int flush(bool terminate)
Definition logger.cpp:173
int getRank() const
Definition logger.cpp:120
void setConsoleStream(std::ostream *console)
Definition logger.cpp:321
Manager for the loggers.
Definition logger.hpp:234
void create(const std::string &name, bool reset=false, int nProcesses=1, int rank=0)
Definition logger.cpp:1327
bool setMode(log::Mode mode)
Definition logger.cpp:1456
bool exists(const std::string &name) const
Definition logger.cpp:1422
Logger & debug(log::Visibility visibility=log::VISIBILITY_NOTSET)
Definition logger.cpp:1234
bool destroy(const std::string &name, bool force=false)
Definition logger.cpp:1384
void setVerbosities(log::Level threshold)
Definition logger.cpp:1549
Logger & critical(log::Visibility visibility=log::VISIBILITY_NOTSET)
Definition logger.cpp:1082
std::string getDefaultDirectory() const
Definition logger.cpp:1603
log::Mode getMode() const
Definition logger.cpp:1472
Logger & cout(log::Level defualtSeverity=log::Level::LEVEL_NOTSET, log::Visibility defualtVisibility=log::VISIBILITY_NOTSET)
Definition logger.cpp:1016
Logger & warning(log::Visibility visibility=log::VISIBILITY_NOTSET)
Definition logger.cpp:1158
static LoggerManager & manager()
Definition logger.cpp:992
Logger & error(log::Visibility visibility=log::VISIBILITY_NOTSET)
Definition logger.cpp:1120
void setFileVerbosity(log::Level threshold)
Definition logger.cpp:1581
std::string getDefaultName() const
Definition logger.cpp:1593
Logger & info(log::Visibility visibility=log::VISIBILITY_NOTSET)
Definition logger.cpp:1196
bool isInitialized() const
Definition logger.cpp:1437
void setConsoleVerbosity(log::Level threshold)
Definition logger.cpp:1565
void initialize(log::Mode mode, bool reset, int nProcesses, int rank)
Definition logger.cpp:1268
Message logger.
Definition logger.hpp:143
void setDefaultVisibility(log::Visibility visibility)
Definition logger.cpp:579
void setDefaultSeverity(log::Level severity)
Definition logger.cpp:537
void setVerbosities(log::Level threshold)
Definition logger.cpp:626
void setConsoleTimestampEnabled(bool enabled)
Definition logger.cpp:660
void setFileVerbosity(log::Level threshold)
Definition logger.cpp:767
log::Level getDefaultSeverity()
Definition logger.cpp:549
std::string getContext()
Definition logger.cpp:806
void setConsoleVerbosity(log::Level threshold)
Definition logger.cpp:680
void setPriority(log::Priority priority)
Definition logger.cpp:559
bool isConsoleTimestampEnabled() const
Definition logger.cpp:650
void disable(log::Level=log::Level::LEVEL_CRITICAL)
Definition logger.cpp:482
log::Visibility getDefaultVisibility()
Definition logger.cpp:591
log::Level getFileVerbosity()
Definition logger.cpp:780
void setIndentation(int delta)
Definition logger.cpp:816
void disableConsole(log::Level=log::Level::LEVEL_CRITICAL)
Definition logger.cpp:505
bool isFileTimestampEnabled() const
Definition logger.cpp:737
void setVisibility(log::Visibility visibility)
Definition logger.cpp:601
void setFileTimestampEnabled(bool enabled)
Definition logger.cpp:747
int getIndentation()
Definition logger.cpp:832
void println(const std::string &message)
Definition logger.cpp:852
void disableFile(log::Level=log::Level::LEVEL_CRITICAL)
Definition logger.cpp:527
std::string getName() const
Definition logger.cpp:842
void print(const std::string &message)
Definition logger.cpp:896
void setContext(const std::string &context)
Definition logger.cpp:790
void setTimestampEnabled(bool enabled)
Definition logger.cpp:638
log::Level getConsoleVerbosity()
Definition logger.cpp:726
log::Visibility getVisibility()
Definition logger.cpp:611
log::Priority getPriority()
Definition logger.cpp:569
#define BITPIT_DEPRECATED(func)
Definition compiler.hpp:87
LoggerManipulator< log::Level > defaultSeverity(const log::Level &severity)
Definition logger.cpp:1956
Level Verbosity
Definition logger.hpp:79
Logger & cout(log::Level defaultSeverity, log::Visibility defaultVisibility)
Definition logger.cpp:1705
LoggerManipulator< log::Visibility > defaultVisibility(const log::Visibility &visibility)
Definition logger.cpp:2010
Level Priority
Definition logger.hpp:78
LoggerManipulator< log::Level > verbosities(const log::Level &threshold)
Definition logger.cpp:2066
Logger & setPriority(Logger &logger, const log::Priority &priority)
Definition logger.cpp:1968
LoggerManipulator< log::Visibility > visibility(const log::Visibility &visibility)
Definition logger.cpp:2037
Logger & critical(log::Visibility defaultVisibility)
Definition logger.cpp:1742
Logger & operator<<(Logger &logger, LoggerManipulator< T > &&m)
Definition logger.hpp:367
Logger & disable(Logger &logger, const log::Level &level)
Definition logger.cpp:2134
LoggerManipulator< log::Level > consoleVerbosity(const log::Level &threshold)
Definition logger.cpp:2093
Logger & setDefaultSeverity(Logger &logger, const log::Level &severity)
Definition logger.cpp:1941
LoggerManipulator< log::Priority > priority(const log::Priority &priority)
Definition logger.cpp:1983
Logger & error(log::Visibility defaultVisibility)
Definition logger.cpp:1777
Logger & warning(log::Visibility defaultVisibility)
Definition logger.cpp:1812
LoggerManipulator< log::Level > fileVerbosity(const log::Level &threshold)
Definition logger.cpp:2120
Logger & debug(log::Visibility defaultVisibility)
Definition logger.cpp:1882
LoggerManipulator< int > indent(int delta)
Definition logger.cpp:2240
Logger & setFileVerbosity(Logger &logger, const log::Level &threshold)
Definition logger.cpp:2105
Logger & setVisibility(Logger &logger, const log::Visibility &visibility)
Definition logger.cpp:2022
Logger & info(log::Visibility defaultVisibility)
Definition logger.cpp:1847
Logger & disableConsole(Logger &logger, const log::Level &level)
Definition logger.cpp:2165
Logger & setDefaultVisibility(Logger &logger, const log::Visibility &visibility)
Definition logger.cpp:1995
LoggerManager & manager()
Definition logger.cpp:1685
Logger & setVerbosities(Logger &logger, const log::Level &threshold)
Definition logger.cpp:2050
Logger & setIndentation(Logger &logger, const int &delta)
Definition logger.cpp:2225
LoggerManipulator< std::string > context(const std::string &context)
Definition logger.cpp:1929
Logger & disableFile(Logger &logger, const log::Level &level)
Definition logger.cpp:2196
Level Severity
Definition logger.hpp:77
Logger & setConsoleVerbosity(Logger &logger, const log::Level &threshold)
Definition logger.cpp:2078
Logger & setContext(Logger &logger, const std::string &context)
Definition logger.cpp:1914
LoggerManipulator(Logger &(*ff)(Logger &, const T &), const T &ss)
Definition logger.hpp:357
--- layout: doxygen_footer ---