mCRL2
Loading...
Searching...
No Matches
logger.h
Go to the documentation of this file.
1// Author(s): Jeroen Keiren
2// Copyright: see the accompanying file COPYING or copy at
3// https://github.com/mCRL2org/mCRL2/blob/master/COPYING
4//
5// Distributed under the Boost Software License, Version 1.0.
6// (See accompanying file LICENSE_1_0.txt or copy at
7// http://www.boost.org/LICENSE_1_0.txt)
8//
10
11#ifndef MCRL2_UTILITIES_LOGGER_H
12#define MCRL2_UTILITIES_LOGGER_H
13
14#include <cassert>
15#include <atomic>
16#include <cstdio>
17#include <ctime>
18#include <map>
19#include <memory>
20#include <set>
21#include <stdexcept>
22
25
26namespace mcrl2::log {
27
31{
32 quiet, // No log message should ever be printed to this log level!
36 status, // This is overwritten in the output when printed consecutively, and the highest level that is shown by default.
40};
41
44inline
45std::string log_level_to_string(const log_level_t level)
46{
47 static const char* const buffer[] = {"quiet", "error", "warning", "info", "status", "verbose", "debug", "trace"};
48 if ((unsigned) level >= sizeof(buffer) / sizeof(buffer[0])) return "unknown level";
49 return buffer[level];
50}
51
53inline
55{
56 if (s == "quiet")
57 {
58 return quiet;
59 }
60 else if (s == "error")
61 {
62 return error;
63 }
64 else if (s == "warning")
65 {
66 return warning;
67 }
68 else if (s == "info")
69 {
70 return info;
71 }
72 else if (s == "status")
73 {
74 return status;
75 }
76 else if (s == "verbose")
77 {
78 return verbose;
79 }
80 else if (s == "debug")
81 {
82 return debug;
83 }
84 else if (s == "trace")
85 {
86 return trace;
87 }
88 else
89 {
90 throw std::runtime_error("Unknown log-level " + s + " provided.");
91 }
92}
93
94std::string format_time(const time_t* t);
95
100{
101 public:
104 {}
105
108 {}
109
117 virtual void output(const log_level_t level, const time_t timestamp, const std::string& msg, const bool print_time_information) = 0;
118};
119
120std::set<output_policy*> initialise_output_policies();
121
129{
130 protected:
133 std::ostringstream m_os;
134
137
140
141 static std::atomic<log_level_t>& log_level()
142 {
143 static std::atomic<log_level_t> g_log_level(log_level_t::info);
144 return g_log_level;
145 }
146
149 {
150 thread_local bool print_timing_info=false;
151 return print_timing_info;
152 }
153
155 static
156 std::set<output_policy*>& output_policies()
157 {
158 static std::set<output_policy*> m_output_policies = initialise_output_policies();
159 return m_output_policies;
160 }
161
162 public:
165 {
166 assert(quiet != l);
167 m_level = l;
168 std::time(&m_timestamp);
169 }
170
175 {
176 for(output_policy* policy: output_policies())
177 {
178 policy->output(m_level, m_timestamp, m_os.str(), m_print_time_information());
179 }
180 }
181
183 static
185 {
186 output_policies().insert(&policy);
187 }
188
190 static
192 {
193 std::set<output_policy*>::iterator i = output_policies().find(&policy);
194 if(i != output_policies().end())
195 {
196 output_policies().erase(i);
197 }
198 }
199
201 static
203 {
204 output_policies().clear();
205 }
206
209 static
211 {
212 log_level() = level;
213 }
214
216 static
218 {
219 return log_level();
220 }
221
224 {
226 }
227
230 {
231 m_print_time_information() = false;
232 }
233
236 static bool get_report_time_info()
237 {
239 }
240
243 std::ostringstream& get()
244 {
245 return m_os;
246 }
247};
248
250{
251public:
259 static std::string format(const log_level_t level, const time_t timestamp, const std::string& msg, const bool print_time_information)
260 {
262 (void)level; (void)timestamp; (void)print_time_information;
263
264 assert(quiet != level);
265 return msg;
266 }
267};
268
273{
274protected:
277 static
279 {
280 static std::atomic<bool> m_last_message_ended_with_newline = true;
281 return m_last_message_ended_with_newline;
282 }
283
284 static
285 std::atomic<bool>& last_message_was_status()
286 {
287 static std::atomic<bool> m_last_message_was_status = false;
288 return m_last_message_was_status;
289 }
290
291 static
292 std::atomic<std::size_t>& caret_pos()
293 {
294 static std::atomic<std::size_t> m_caret_pos = 0;
295 return m_caret_pos;
296 }
297
298 static
299 std::atomic<std::size_t>& last_caret_pos()
300 {
301 static std::atomic<std::size_t> m_last_caret_pos = 0;
302 return m_last_caret_pos;
303 }
304
305public:
311 static std::string format(const log_level_t level, const time_t timestamp, const std::string& msg, const bool print_time_information);
312};
313
318{
319 protected:
321 static std::atomic<FILE*>& get_stream()
322 {
323 static std::atomic<FILE*> g_stream(stderr);
324 return g_stream;
325 }
326
327 public:
329 {}
330
331 virtual ~file_output()
332 {}
333
335 static
336 void set_stream(FILE* stream)
337 {
338 get_stream() = stream;
339 }
340
350 virtual void output(const log_level_t level, const time_t timestamp, const std::string& msg, const bool print_time_information) override
351 {
352 assert(quiet != level);
353 FILE* p_stream = get_stream();
354 if (!p_stream)
355 {
356 return;
357 }
358
359 fprintf(p_stream, "%s", formatter::format(level, timestamp, msg, print_time_information).c_str());
360 fflush(p_stream);
361 }
362};
363
365inline
367{
368 static file_output m_default = file_output();
369 return m_default;
370}
371
374inline
375std::set<output_policy*> initialise_output_policies()
376{
377 std::set<output_policy*> result;
378 result.insert(&default_output_policy());
379 return result;
380}
381
383inline bool mCRL2logEnabled(const log_level_t level)
384{
386}
387
388} // namespace mcrl2::log
389
391#define mCRL2log(LEVEL) if (mcrl2::log::mCRL2logEnabled(LEVEL)) mcrl2::log::logger(LEVEL).get()
392
393#endif // MCRL2_UTILITIES_LOGGER_H
File output class.
Definition logger.h:318
static void set_stream(FILE *stream)
Definition logger.h:336
virtual ~file_output()
Definition logger.h:331
virtual void output(const log_level_t level, const time_t timestamp, const std::string &msg, const bool print_time_information) override
Definition logger.h:350
static std::atomic< FILE * > & get_stream()
Obtain the underlying stream used to print to a file.
Definition logger.h:321
static std::string format(const log_level_t level, const time_t timestamp, const std::string &msg, const bool print_time_information)
Format msg,.
Definition logger.h:259
Mixin that takes care of formatting of a message.
Definition logger.h:273
static std::atomic< std::size_t > & caret_pos()
Definition logger.h:292
static std::atomic< bool > & last_message_ended_with_newline()
Records whether the last message that was printed ended with a new line.
Definition logger.h:278
static std::atomic< std::size_t > & last_caret_pos()
Definition logger.h:299
static std::atomic< bool > & last_message_was_status()
Definition logger.h:285
static std::string format(const log_level_t level, const time_t timestamp, const std::string &msg, const bool print_time_information)
Prefix each line in s with some extra information. The things that are added are:
Definition logger.cpp:32
Class for logging messages.
Definition logger.h:129
static bool & m_print_time_information()
An indication whether time information should be printed.
Definition logger.h:148
static void clear_report_time_info()
Indicate that timing information should not be printed.
Definition logger.h:229
static void unregister_output_policy(output_policy &policy)
Unregister output policy.
Definition logger.h:191
static void set_report_time_info()
Indicate that timing information should be printed.
Definition logger.h:223
log_level_t m_level
The loglevel of the current message.
Definition logger.h:136
static std::set< output_policy * > & output_policies()
Output policies.
Definition logger.h:156
static log_level_t get_reporting_level()
Get reporting level.
Definition logger.h:217
static void set_reporting_level(const log_level_t level)
Set reporting level.
Definition logger.h:210
std::ostringstream & get()
Definition logger.h:243
logger(const log_level_t l)
Default constructor.
Definition logger.h:164
static void clear_output_policies()
Clear all output policies.
Definition logger.h:202
time_t m_timestamp
Timestamp of the current message.
Definition logger.h:139
static void register_output_policy(output_policy &policy)
Register output policy.
Definition logger.h:184
~logger()
Destructor; flushes output. Flushing during destruction is important to confer thread safety to the l...
Definition logger.h:174
static bool get_report_time_info()
Get whether timing information is printed.
Definition logger.h:236
static std::atomic< log_level_t > & log_level()
Definition logger.h:141
std::ostringstream m_os
Stream that is printed to internally Collects the full debug message that we are currently printing.
Definition logger.h:133
Interface class for output policy.
Definition logger.h:100
output_policy()
Constructor.
Definition logger.h:103
virtual void output(const log_level_t level, const time_t timestamp, const std::string &msg, const bool print_time_information)=0
Output message.
virtual ~output_policy()
Destructor.
Definition logger.h:107
Inherit from this class to prevent it from being copyable.
Definition noncopyable.h:21
output_policy & default_output_policy()
The default output policy used by the logger.
Definition logger.h:366
std::string log_level_to_string(const log_level_t level)
Convert log level to string This string is used to prefix messages in the logging output.
Definition logger.h:45
std::string format_time(const time_t *t)
Definition logger.cpp:17
std::set< output_policy * > initialise_output_policies()
Initialise the output policies. This returns the singleton set containing the default output policy.
Definition logger.h:375
log_level_t
Log levels that are supported.
Definition logger.h:31
@ warning
Definition logger.h:34
@ verbose
Definition logger.h:37
log_level_t log_level_from_string(const std::string &s)
Convert string to log level.
Definition logger.h:54
bool mCRL2logEnabled(const log_level_t level)
Definition logger.h:383
String manipulation functions.