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 <atomic>
15#include <cstdio>
16#include <ctime>
17#include <map>
18#include <memory>
19#include <set>
20#include <stdexcept>
21
23
24namespace mcrl2::log {
25
29{
30 quiet, // No log message should ever be printed to this log level!
34 status, // This is overwritten in the output when printed consecutively, and the lowest level that is shown by default.
38};
39
42inline
43std::string log_level_to_string(const log_level_t level)
44{
45 static const char* const buffer[] = {"quiet", "error", "warning", "info", "status", "verbose", "debug", "trace"};
46 return buffer[level];
47}
48
50inline
52{
53 if (s == "quiet")
54 {
55 return quiet;
56 }
57 else if (s == "error")
58 {
59 return error;
60 }
61 else if (s == "warning")
62 {
63 return warning;
64 }
65 else if (s == "info")
66 {
67 return info;
68 }
69 else if (s == "status")
70 {
71 return status;
72 }
73 else if (s == "verbose")
74 {
75 return verbose;
76 }
77 else if (s == "debug")
78 {
79 return debug;
80 }
81 else if (s == "trace")
82 {
83 return trace;
84 }
85 else
86 {
87 throw std::runtime_error("Unknown log-level " + s + " provided.");
88 }
89}
90
91std::string format_time(const time_t* t);
92
97{
98 public:
101 {}
102
105 {}
106
114 virtual void output(const log_level_t level, const time_t timestamp, const std::string& msg, const bool print_time_information) = 0;
115};
116
117std::set<output_policy*> initialise_output_policies();
118
126{
127 public:
128 // Prevent copying loggers
129 private:
131 {}
132
134 {
135 return *this;
136 }
137
138 protected:
141 std::ostringstream m_os;
142
145
148
149 static std::atomic<log_level_t>& log_level()
150 {
151 static std::atomic<log_level_t> g_log_level(log_level_t::info);
152 return g_log_level;
153 }
154
157 {
158 thread_local bool print_timing_info=false;
159 return print_timing_info;
160 }
161
163 static
164 std::set<output_policy*>& output_policies()
165 {
166 static std::set<output_policy*> m_output_policies = initialise_output_policies();
167 return m_output_policies;
168 }
169
170 public:
173 {}
174
179 {
180 for(output_policy* policy: output_policies())
181 {
182 policy->output(m_level, m_timestamp, m_os.str(), m_print_time_information());
183 }
184 }
185
187 static
189 {
190 output_policies().insert(&policy);
191 }
192
194 static
196 {
197 std::set<output_policy*>::iterator i = output_policies().find(&policy);
198 if(i != output_policies().end())
199 {
200 output_policies().erase(i);
201 }
202 }
203
205 static
207 {
208 output_policies().clear();
209 }
210
213 static
215 {
216 log_level() = level;
217 }
218
220 static
222 {
223 return log_level();
224 }
225
228 {
230 }
231
234 {
235 m_print_time_information() = false;
236 }
237
240 static bool get_report_time_info()
241 {
243 }
244
247 std::ostringstream& get(const log_level_t l)
248 {
249 m_level = l;
250 std::time(&m_timestamp);
251 return m_os;
252 }
253};
254
256{
257public:
265 static std::string format(const log_level_t level, const time_t timestamp, const std::string& msg, const bool print_time_information)
266 {
268 (void)level; (void)timestamp; (void)print_time_information;
269
270 return msg;
271 }
272};
273
278{
279protected:
282 static
284 {
285 static std::atomic<bool> m_last_message_ended_with_newline = true;
286 return m_last_message_ended_with_newline;
287 }
288
289 static
290 std::atomic<bool>& last_message_was_status()
291 {
292 static std::atomic<bool> m_last_message_was_status = false;
293 return m_last_message_was_status;
294 }
295
296 static
297 std::atomic<std::size_t>& caret_pos()
298 {
299 static std::atomic<std::size_t> m_caret_pos = 0;
300 return m_caret_pos;
301 }
302
303 static
304 std::atomic<std::size_t>& last_caret_pos()
305 {
306 static std::atomic<std::size_t> m_last_caret_pos = 0;
307 return m_last_caret_pos;
308 }
309
310public:
316 static std::string format(const log_level_t level, const time_t timestamp, const std::string& msg, const bool print_time_information);
317};
318
323{
324 protected:
326 static std::atomic<FILE*>& get_stream()
327 {
328 static std::atomic<FILE*> g_stream(stderr);
329 return g_stream;
330 }
331
332 public:
334 {}
335
336 virtual ~file_output()
337 {}
338
340 static
341 void set_stream(FILE* stream)
342 {
343 get_stream() = stream;
344 }
345
355 virtual void output(const log_level_t level, const time_t timestamp, const std::string& msg, const bool print_time_information) override
356 {
357 FILE* p_stream = get_stream();
358 if (!p_stream)
359 {
360 return;
361 }
362
363 fprintf(p_stream, "%s", formatter::format(level, timestamp, msg, print_time_information).c_str());
364 fflush(p_stream);
365 }
366};
367
369inline
371{
372 static file_output m_default = file_output();
373 return m_default;
374}
375
378inline
379std::set<output_policy*> initialise_output_policies()
380{
381 std::set<output_policy*> result;
382 result.insert(&default_output_policy());
383 return result;
384}
385
387inline bool mCRL2logEnabled(const log_level_t level)
388{
390}
391
392} // namespace mcrl2::log
393
395#define mCRL2log(LEVEL) if (mcrl2::log::mCRL2logEnabled(LEVEL)) mcrl2::log::logger().get(LEVEL)
396
397#endif // MCRL2_UTILITIES_LOGGER_H
File output class.
Definition logger.h:323
static void set_stream(FILE *stream)
Definition logger.h:341
virtual ~file_output()
Definition logger.h:336
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:355
static std::atomic< FILE * > & get_stream()
Obtain the underlying stream used to print to a file.
Definition logger.h:326
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:265
Mixin that takes care of formatting of a message.
Definition logger.h:278
static std::atomic< std::size_t > & caret_pos()
Definition logger.h:297
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:283
static std::atomic< std::size_t > & last_caret_pos()
Definition logger.h:304
static std::atomic< bool > & last_message_was_status()
Definition logger.h:290
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:126
static bool & m_print_time_information()
An indication whether time information should be printed.
Definition logger.h:156
static void clear_report_time_info()
Indicate that timing information should not be printed.
Definition logger.h:233
static void unregister_output_policy(output_policy &policy)
Unregister output policy.
Definition logger.h:195
static void set_report_time_info()
Indicate that timing information should be printed.
Definition logger.h:227
log_level_t m_level
The loglevel of the current message.
Definition logger.h:144
static std::set< output_policy * > & output_policies()
Output policies.
Definition logger.h:164
static log_level_t get_reporting_level()
Get reporting level.
Definition logger.h:221
logger & operator=(const logger &)
Definition logger.h:133
static void set_reporting_level(const log_level_t level)
Set reporting level.
Definition logger.h:214
static void clear_output_policies()
Clear all output policies.
Definition logger.h:206
time_t m_timestamp
Timestamp of the current message.
Definition logger.h:147
logger(const logger &)
Definition logger.h:130
static void register_output_policy(output_policy &policy)
Register output policy.
Definition logger.h:188
~logger()
Destructor; flushes output. Flushing during destruction is important to confer thread safety to the l...
Definition logger.h:178
static bool get_report_time_info()
Get whether timing information is printed.
Definition logger.h:240
static std::atomic< log_level_t > & log_level()
Definition logger.h:149
logger()
Default constructor.
Definition logger.h:172
std::ostringstream & get(const log_level_t l)
Definition logger.h:247
std::ostringstream m_os
Stream that is printed to internally Collects the full debug message that we are currently printing.
Definition logger.h:141
Interface class for output policy.
Definition logger.h:97
output_policy()
Constructor.
Definition logger.h:100
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:104
output_policy & default_output_policy()
The default output policy used by the logger.
Definition logger.h:370
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:43
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:379
log_level_t
Log levels that are supported.
Definition logger.h:29
@ warning
Definition logger.h:32
@ verbose
Definition logger.h:35
log_level_t log_level_from_string(const std::string &s)
Convert string to log level.
Definition logger.h:51
bool mCRL2logEnabled(const log_level_t level)
Definition logger.h:387
String manipulation functions.