LibCarna Version 3.4.0
Loading...
Searching...
No Matches
Log.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2010 - 2016 Leonid Kostrykin
3 *
4 * Chair of Medical Engineering (mediTEC)
5 * RWTH Aachen University
6 * Pauwelsstr. 20
7 * 52074 Aachen
8 * Germany
9 *
10 *
11 * Copyright (C) 2021 - 2025 Leonid Kostrykin
12 *
13 */
14
15#ifndef LOG_H_6014714286
16#define LOG_H_6014714286
17
18#include <LibCarna/LibCarna.hpp>
21#include <functional>
22#include <string>
23#include <memory>
24
30namespace LibCarna
31{
32
33namespace base
34{
35
36
37
38// ----------------------------------------------------------------------------------
39// Log
40// ----------------------------------------------------------------------------------
41
49class LIBCARNA Log : public Singleton< Log >
50{
51
52 struct Details;
53 const std::unique_ptr< Details > pimpl;
54
56
57 void pushTag( const std::string& tag );
58
59 void popTag();
60
61protected:
62
63 friend class Singleton< Log >;
64
68 Log();
69
70public:
71
76 virtual ~Log();
77
81 const std::string& tag() const;
82
87 {
92 verbose
93 };
94
95 // ------------------------------------------------------------------------------
96 // Log :: TagScope
97 // ------------------------------------------------------------------------------
98
106 struct LIBCARNA TagScope
107 {
112 explicit TagScope( const std::string& tag );
113
118
119 private:
120
121 const bool valid;
122 static void* operator new( std::size_t );
123 static void* operator new[]( std::size_t );
124 };
125
126 // ------------------------------------------------------------------------------
127 // Log :: Writer
128 // ------------------------------------------------------------------------------
129
135 class LIBCARNA Writer
136 {
137
139
140 public:
141
145 virtual ~Writer();
146
150 virtual void write( Severity severity, const std::string& entry ) const = 0;
151
152 }; // Log :: Writer
153
154 // ------------------------------------------------------------------------------
155 // Log :: TextWriter
156 // ------------------------------------------------------------------------------
157
164 class LIBCARNA TextWriter : public Writer
165 {
166
167 public:
168
169 virtual void write( Severity, const std::string& ) const override;
170
171 protected:
172
176 virtual void writeLine( Severity severity, const std::string& formattedEntry ) const = 0;
177
178 }; // Log :: TextWriter
179
180 // ------------------------------------------------------------------------------
181 // Log :: StdWriter
182 // ------------------------------------------------------------------------------
183
190 class LIBCARNA StdWriter : public TextWriter
191 {
192
193 protected:
194
195 virtual void writeLine( Severity, const std::string& ) const override;
196
197 }; // Log :: StdWriter
198
203
207 void record( Severity severity, const std::string& entry ) const;
208
209 // ------------------------------------------------------------------------------
210 // Log :: OnShutdownListener
211 // ------------------------------------------------------------------------------
212
219 class LIBCARNA OnShutdownListener
220 {
221
222 public:
223
228
232 virtual void onLogShutdown() = 0;
233
234 }; // Log :: OnShutdownListener
235
240
246
254 void shutdown();
255
256}; // Log
257
258
259
260// ----------------------------------------------------------------------------------
261// LIBCARNA_LOG_TAG_SCOPE
262// ----------------------------------------------------------------------------------
263
267#define LIBCARNA_LOG_TAG_SCOPE( tag ) \
268 const ::LibCarna::base::Log::TagScope _tagScope##__COUNTER__( tag )
269
270
271
272} // namespace LibCarna :: base
273
274} // namespace LibCarna
275
276#endif // LOG_H_6014714286
Contains forward-declarations.
Defines LibCarna::base::Singleton.
Represents an association.
Defines callback interface that is invoked when the Log system is about to shut down.
Definition Log.hpp:220
virtual ~OnShutdownListener()
Does nothing.
virtual void onLogShutdown()=0
Indicates that Log::instance is about to shut down.
Writes fatal and error classified log messages to std::cerr and such classified as warning or debug t...
Definition Log.hpp:191
virtual void writeLine(Severity, const std::string &) const override
Logs formattedEntry with severity.
Abstract implementation of the Writer interface, that formats log messages along with their severity ...
Definition Log.hpp:165
virtual void writeLine(Severity severity, const std::string &formattedEntry) const =0
Logs formattedEntry with severity.
virtual void write(Severity, const std::string &) const override
Logs entry with severity.
Writes log entries.
Definition Log.hpp:136
virtual void write(Severity severity, const std::string &entry) const =0
Logs entry with severity.
virtual ~Writer()
Deletes.
Records log messages. The log writing is delegated to implementations of the Log::Writer interface....
Definition Log.hpp:50
void record(Severity severity, const std::string &entry) const
Instructs current writer to write entry with severity.
Log()
Instantiates.
void addOnShutdownListener(OnShutdownListener &listener)
Makes listener be notified when the log system is about to shut down.
virtual ~Log()
Deletes. Notifies its shutdown listeners if they haven't been notified yet.
void setWriter(Writer *)
Sets object that log writing will be delegated to.
const std::string & tag() const
Tells current log tag.
Severity
Describes the severity of a log entry.
Definition Log.hpp:87
@ debug
Indicates messages that might be of interest when searching bugs.
Definition Log.hpp:91
@ error
Indicates errors like memory leaks.
Definition Log.hpp:89
@ fatal
Indicates errors that might lead to misbehaving program logic.
Definition Log.hpp:88
@ warning
Indicates warnings.
Definition Log.hpp:90
void removeOnShutdownListener(const OnShutdownListener &listener)
Makes listener no longer be notified when the log system is about to shut down.
void shutdown()
Notifies the shutdown listeners that the log system is about to shut down, if they haven't been notif...
Singleton base class
Definition Singleton.hpp:63
Defines LibCarna::base::noncopyable and NON_COPYABLE.
#define NON_COPYABLE
Marks the class that it is placed in as non-copyable.
Enforces the log to use a specific tag for the duration of the existence of the object from this clas...
Definition Log.hpp:107
TagScope(const std::string &tag)
Enforces the log to use tag for the duration of the existence of the created object.
~TagScope()
Removes the tag from the log.