cppcore
A small, portable C++ core library
Loading...
Searching...
No Matches
Logger.h
Go to the documentation of this file.
1/*-----------------------------------------------------------------------------------------------
2The MIT License (MIT)
3
4Copyright (c) 2014-2026 Kim Kulling
5
6Permission is hereby granted, free of charge, to any person obtaining a copy of
7this software and associated documentation files (the "Software"), to deal in
8the Software without restriction, including without limitation the rights to
9use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
10the Software, and to permit persons to whom the Software is furnished to do so,
11subject to the following conditions:
12
13The above copyright notice and this permission notice shall be included in all
14copies or substantial portions of the Software.
15
16THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
18FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
19COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22-----------------------------------------------------------------------------------------------*/
23#pragma once
24
28
29namespace cppcore {
30
32#define DECL_LOG_MODULE(name) static constexpr char Tag[] = #name;
33
35
36//-------------------------------------------------------------------------------------------------
45//-------------------------------------------------------------------------------------------------
47public:
49 virtual ~AbstractLogStream() = default;
50
53 virtual void write(const String &message) = 0;
54
56 virtual void activate();
57
59 virtual void deactivate();
60
63 virtual bool isActive() const;
64
65 // No copying
67 AbstractLogStream &operator = (const AbstractLogStream &) = delete;
68
69protected:
71 AbstractLogStream() = default;
72
73private:
74 bool mIsActive{true};
75};
76
77//-------------------------------------------------------------------------------------------------
87//-------------------------------------------------------------------------------------------------
88class DLL_CPPCORE_EXPORT Logger final {
89public:
91 enum class VerboseMode {
92 Invalid = -1,
93 Normal,
94 Verbose,
95 Debug,
96 Trace,
97 Count
98 };
99
101 enum class PrintMode {
102 Invalid = -1,
103 WithDateTime,
104 WithoutDateTime,
105 Count
106 };
107
110 static Logger *create();
111
114 static void set(Logger *logger);
115
118 static Logger *getInstance();
119
121 static void kill();
122
126 void setVerboseMode(VerboseMode sev);
127
130 VerboseMode getVerboseMode() const;
131
135 void trace(const String &domain, const String &msg);
136
140 void debug(const String &domain, const String &msg);
141
145 void info(const String &domain, const String &msg);
146
150 void print(const String &message, PrintMode mode = PrintMode::WithoutDateTime);
151
155 void warn( const String &domain, const String &msg );
156
160 void error(const String &domain, const String &msg);
161
165 void fatal(const String &domain, const String &msg);
166
169 void registerLogStream(AbstractLogStream *pLogStream);
170
173 void unregisterLogStream(const AbstractLogStream *pLogStream);
174
175private:
176 Logger();
177 ~Logger();
178 String getDateTime() const;
179
180private:
181 // @brief The Standard log stream.
182 class StdLogStream : public AbstractLogStream {
183 public:
185 StdLogStream() = default;
186
188 ~StdLogStream() override = default;
189
192 void write(const String &msg) override;
193 };
194
195 static Logger *sLogger;
196
197 using LogStreamArray = cppcore::TArray<AbstractLogStream*>;
198 LogStreamArray mLogStreams;
199 VerboseMode mVerboseMode{VerboseMode::Normal};
200 uint32_t mIntention{0};
201};
202
203// Logger helper function prototypes
204void tracePrint(const String &domain, const String &file, int line, const String &msg);
205void debugPrint(const String &domain, const String &file, int line, const String &msg);
206void infoPrint( const String &domain, const String &file, int line, const String &msg );
207void warnPrint( const String &domain, const String &file, int line, const String &msg );
208void errorPrint( const String &domain, const String &file, int line, const String &msg );
209void fatalPrint( const String &domain, const String &file, int line, const String &msg );
210
211} // namespace cppcore
212
213//-------------------------------------------------------------------------------------------------
218//-------------------------------------------------------------------------------------------------
219#define log_trace(domain, msg) ::cppcore::tracePrint(domain, __FILE__, __LINE__, msg)
220
221//-------------------------------------------------------------------------------------------------
226//-------------------------------------------------------------------------------------------------
227#define log_debug(domain, msg) ::cppcore::debugPrint(domain, __FILE__, __LINE__, msg)
228
229//-------------------------------------------------------------------------------------------------
234//-------------------------------------------------------------------------------------------------
235#define log_info(domain, msg) ::cppcore::infoPrint(domain, __FILE__, __LINE__, msg)
236
237//-------------------------------------------------------------------------------------------------
242//-------------------------------------------------------------------------------------------------
243#define log_warn(domain, message) ::cppcore::warnPrint(domain, __FILE__, __LINE__, message)
244
245//-------------------------------------------------------------------------------------------------
250//-------------------------------------------------------------------------------------------------
251#define log_error(domain, message) ::cppcore::errorPrint(domain, __FILE__, __LINE__, message)
252
253//-------------------------------------------------------------------------------------------------
258//-------------------------------------------------------------------------------------------------
259#define log_fatal(domain, message) ::cppcore::fatalPrint(domain, __FILE__, __LINE__, message)
#define DLL_CPPCORE_EXPORT
This pure abstract interface class declares the protocol for log-stream implementation.
Definition Logger.h:46
AbstractLogStream(const AbstractLogStream &)=delete
virtual void deactivate()
The stream will be deactivated.
Definition Logger.cpp:75
virtual bool isActive() const
Returns true, if the stream is active.
Definition Logger.cpp:79
AbstractLogStream()=default
The default class constructor.
virtual void write(const String &message)=0
Will write a message into the attached log stream.
virtual void activate()
The stream will be activated.
Definition Logger.cpp:71
virtual ~AbstractLogStream()=default
The default class destructor, virtual.
This class implements a simple logger.
Definition Logger.h:88
VerboseMode
Describes the verbose mode.
Definition Logger.h:91
PrintMode
Describes the mode for prints into the active log stream.
Definition Logger.h:101
This template class implements a simple array with dynamic boundaries.
Definition TArray.h:55
This class is used to describe the inner data of a string.
Definition TStringBase.h:38
TStringBase< char > String
Definition Logger.h:34
void tracePrint(const String &domain, const String &file, int line, const String &msg)
Definition Logger.cpp:276
void errorPrint(const String &domain, const String &file, int line, const String &msg)
Definition Logger.cpp:304
void warnPrint(const String &domain, const String &file, int line, const String &msg)
Definition Logger.cpp:297
void debugPrint(const String &domain, const String &file, int line, const String &msg)
Definition Logger.cpp:283
void fatalPrint(const String &domain, const String &file, int line, const String &msg)
Definition Logger.cpp:311
void infoPrint(const String &domain, const String &file, int line, const String &msg)
Definition Logger.cpp:290