Carna Version 3.3.3
Loading...
Searching...
No Matches
Log.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2010 - 2015 Leonid Kostrykin
3 *
4 * Chair of Medical Engineering (mediTEC)
5 * RWTH Aachen University
6 * Pauwelsstr. 20
7 * 52074 Aachen
8 * Germany
9 *
10 */
11
12#ifndef LOG_H_6014714286
13#define LOG_H_6014714286
14
15#include <Carna/Carna.h>
18#include <functional>
19#include <string>
20#include <memory>
21
26namespace Carna
27{
28
29namespace base
30{
31
32
33
34// ----------------------------------------------------------------------------------
35// Log
36// ----------------------------------------------------------------------------------
37
46class CARNA_LIB Log : public Singleton< Log >
47{
48
49 struct Details;
50 const std::unique_ptr< Details > pimpl;
51
53
54 void pushTag( const std::string& tag );
55
56 void popTag();
57
58protected:
59
60 friend class Singleton< Log >;
61
65 Log();
66
67public:
68
73 virtual ~Log();
74
78 const std::string& tag() const;
79
84 {
89 verbose
90 };
91
92 // ------------------------------------------------------------------------------
93 // Log :: TagScope
94 // ------------------------------------------------------------------------------
95
104 struct CARNA_LIB TagScope
105 {
110 explicit TagScope( const std::string& tag );
111
116
117 private:
118
119 const bool valid;
120 static void* operator new( std::size_t );
121 static void* operator new[]( std::size_t );
122 };
123
124 // ------------------------------------------------------------------------------
125 // Log :: Writer
126 // ------------------------------------------------------------------------------
127
134 class CARNA_LIB Writer
135 {
136
138
139 public:
140
144 virtual ~Writer();
145
149 virtual void write( Severity severity, const std::string& entry ) const = 0;
150
151 }; // Log :: Writer
152
153 // ------------------------------------------------------------------------------
154 // Log :: TextWriter
155 // ------------------------------------------------------------------------------
156
164 class CARNA_LIB 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
191 class CARNA_LIB StdWriter : public TextWriter
192 {
193
194 protected:
195
196 virtual void writeLine( Severity, const std::string& ) const override;
197
198 }; // Log :: StdWriter
199
204
208 void record( Severity severity, const std::string& entry ) const;
209
210 // ------------------------------------------------------------------------------
211 // Log :: OnShutdownListener
212 // ------------------------------------------------------------------------------
213
221 class CARNA_LIB OnShutdownListener
222 {
223
224 public:
225
230
234 virtual void onLogShutdown() = 0;
235
236 }; // Log :: OnShutdownListener
237
242
248
256 void shutdown();
257
258}; // Log
259
260
261
262// ----------------------------------------------------------------------------------
263// CARNA_LOG_TAG_SCOPE
264// ----------------------------------------------------------------------------------
265
269#define CARNA_LOG_TAG_SCOPE( tag ) \
270 const ::Carna::base::Log::TagScope _tagScope##__COUNTER__( tag )
271
272
273
274} // namespace Carna :: base
275
276} // namespace Carna
277
278#endif // LOG_H_6014714286
Defines Carna::base::Singleton.
Represents an association.
Definition Association.h:45
Defines callback interface that is invoked when the Log system is about to shut down.
Definition Log.h:222
virtual void onLogShutdown()=0
Indicates that Log::instance is about to shut down.
virtual ~OnShutdownListener()
Does nothing.
Writes fatal and error classified log messages to std::cerr and such classified as warning or debug t...
Definition Log.h:192
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.h:165
virtual void write(Severity, const std::string &) const override
Logs entry with severity.
virtual void writeLine(Severity severity, const std::string &formattedEntry) const =0
Logs formattedEntry with severity.
Writes log entries.
Definition Log.h:135
virtual ~Writer()
Deletes.
virtual void write(Severity severity, const std::string &entry) const =0
Logs entry with severity.
Records log messages. The log writing is delegated to implementations of the Log::Writer interface....
Definition Log.h:47
void setWriter(Writer *)
Sets object that log writing will be delegated to.
const std::string & tag() const
Tells current log tag.
Log()
Instantiates.
virtual ~Log()
Deletes. Notifies its shutdown listeners if they haven't been notified yet.
Severity
Describes the severity of a log entry.
Definition Log.h:84
@ fatal
Indicates errors that might lead to misbehaving program logic.
Definition Log.h:85
@ debug
Indicates messages that might be of interest when searching bugs.
Definition Log.h:88
@ error
Indicates errors like memory leaks.
Definition Log.h:86
@ warning
Indicates warnings.
Definition Log.h:87
void addOnShutdownListener(OnShutdownListener &listener)
Makes listener be notified when the log system is about to shut down.
void record(Severity severity, const std::string &entry) const
Instructs current writer to write entry with severity.
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.h:60
#define NON_COPYABLE
Features class 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.h:105
~TagScope()
Removes the tag from the log.
TagScope(const std::string &tag)
Enforces the log to use tag for the duration of the existence of the created object.