ngscopeclient v0.1
log.h
Go to the documentation of this file.
1/***********************************************************************************************************************
2* *
3* logtools *
4* *
5* Copyright (c) 2016-2025 Andrew D. Zonenberg and contributors *
6* All rights reserved. *
7* *
8* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the *
9* following conditions are met: *
10* *
11* * Redistributions of source code must retain the above copyright notice, this list of conditions, and the *
12* following disclaimer. *
13* *
14* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the *
15* following disclaimer in the documentation and/or other materials provided with the distribution. *
16* *
17* * Neither the name of the author nor the names of any contributors may be used to endorse or promote products *
18* derived from this software without specific prior written permission. *
19* *
20* THIS SOFTWARE IS PROVIDED BY THE AUTHORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
21* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL *
22* THE AUTHORS BE HELD LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES *
23* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR *
24* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT *
25* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE *
26* POSSIBILITY OF SUCH DAMAGE. *
27* *
28***********************************************************************************************************************/
29
30#ifndef log_h
31#define log_h
32
39#include <memory>
40#include <string>
41#include <vector>
42#include <set>
43#include <mutex>
44
45#if defined(__MINGW32__)
46#undef ERROR
47#endif
48
49#if defined(_MSC_VER)
50// MSVC
51#include <thread>
52#undef ERROR
53#define __thread __declspec(thread)
54#endif
55
60enum class Severity
61{
63 FATAL = 1,
64
66 ERROR = 2,
67
69 WARNING = 3,
70
72 NOTICE = 4,
73
75 VERBOSE = 5,
76
78 DEBUG = 6,
79
81 TRACE = 7
82};
83
84extern __thread unsigned int g_logIndentLevel;
85
91{
92public:
93 LogSink(Severity min_severity = Severity::VERBOSE)
94 : m_indentSize(4)
95 , m_termWidth(120) //default if not using ioctls to check
97 , m_min_severity(min_severity)
98 {}
99
100 virtual ~LogSink() {}
101
104 { return m_min_severity; }
105
108 { m_min_severity = sev; }
109
116 std::string GetIndentString();
117
118 virtual void Log(Severity severity, const std::string &msg) = 0;
119 virtual void Log(Severity severity, const char *format, va_list va) = 0;
120
121 std::string vstrprintf(const char* format, va_list va);
122
123protected:
124
125 std::string WrapString(std::string str);
126 virtual void PreprocessLine(std::string& line);
127
129 unsigned int m_indentSize;
130
132 unsigned int m_termWidth;
133
136
139};
140
145class STDLogSink : public LogSink
146{
147public:
148 STDLogSink(Severity min_severity = Severity::VERBOSE);
149 ~STDLogSink() override;
150
151 void Log(Severity severity, const std::string &msg) override;
152 void Log(Severity severity, const char *format, va_list va) override;
153
154protected:
155 void Flush();
156};
157
163{
164public:
166 ~ColoredSTDLogSink() override;
167
168protected:
169 void PreprocessLine(std::string& line) override;
170 std::string replace(
171 const std::string& search,
172 const std::string& before,
173 const std::string& after,
174 std::string subject);
175};
176
181class FILELogSink : public LogSink
182{
183public:
184 FILELogSink(FILE *f, bool line_buffered = false, Severity min_severity = Severity::VERBOSE);
185 ~FILELogSink() override;
186
187 void Log(Severity severity, const std::string &msg) override;
188 void Log(Severity severity, const char *format, va_list va) override;
189
190protected:
191 FILE *m_file;
192};
193
194extern std::mutex g_log_mutex;
195extern std::vector<std::unique_ptr<LogSink>> g_log_sinks;
196extern std::set<std::string> g_trace_filters;
197
205{
206public:
207 LogIndenter();
208
209 ~LogIndenter();
210};
211
217 int& i,
218 int argc,
219 char* argv[],
220 Severity& console_verbosity);
221
222
223#ifdef __GNUC__
224#if defined(__MINGW32__) && !defined(__clang__)
225#define ATTR_FORMAT(n, m) __attribute__((__format__ (gnu_printf, n, m)))
226#else
227#define ATTR_FORMAT(n, m) __attribute__((__format__ (__printf__, n, m)))
228#endif
229#define ATTR_NORETURN __attribute__((noreturn))
230#else
231//FIXME on MSVC/Windows
232#define ATTR_FORMAT(n, m)
233#define ATTR_NORETURN
234#endif
235
245#ifdef __GNUC__
246#define LogTrace(...) LogDebugTrace(__PRETTY_FUNCTION__, ##__VA_ARGS__)
247#else
248#define LogTrace(...) LogDebugTrace(__func__, __VA_ARGS__)
249#endif
250
251ATTR_FORMAT(1, 2) void LogVerbose(const char *format, ...);
252ATTR_FORMAT(1, 2) void LogNotice(const char *format, ...);
253ATTR_FORMAT(1, 2) void LogWarning(const char *format, ...);
254ATTR_FORMAT(1, 2) void LogError(const char *format, ...);
255ATTR_FORMAT(1, 2) void LogDebug(const char *format, ...);
256ATTR_FORMAT(2, 3) void LogDebugTrace(const char* function, const char *format, ...);
257ATTR_FORMAT(1, 2) ATTR_NORETURN void LogFatal(const char *format, ...);
258
260ATTR_FORMAT(2, 3) void Log(Severity severity, const char *format, ...);
261
262#undef ATTR_FORMAT
263#undef ATTR_NORETURN
264
265#endif
266
A STDLogSink that colorizes "warning" or "error" keywords.
Definition: log.h:163
std::string replace(const std::string &search, const std::string &before, const std::string &after, std::string subject)
Replaces warning keywords with ANSI escape sequences.
Definition: ColoredSTDLogSink.cpp:108
void PreprocessLine(std::string &line) override
Do any processing required to a line before printing it. Nothing in the base class.
Definition: ColoredSTDLogSink.cpp:59
A log sink writing to a FILE* file handle.
Definition: log.h:182
RAII wrapper for log indentation.
Definition: log.h:205
Base class for all log sinks.
Definition: log.h:91
unsigned int m_termWidth
Width of the console we're printing to, in characters.
Definition: log.h:132
virtual void PreprocessLine(std::string &line)
Do any processing required to a line before printing it. Nothing in the base class.
Definition: log.cpp:159
std::string WrapString(std::string str)
Wraps long lines and adds indentation as needed.
Definition: log.cpp:110
std::string GetIndentString()
Gets the indent string (for now, only used by STDLogSink)
Definition: log.cpp:104
Severity GetSeverity()
Returns the current severity / verbosity level.
Definition: log.h:103
bool m_lastMessageWasNewline
True if the last message ended in a character.
Definition: log.h:135
void SetSeverity(Severity sev)
Update the current severity level.
Definition: log.h:107
unsigned int m_indentSize
Number of spaces in one indentation.
Definition: log.h:129
Severity m_min_severity
Minimum severity of messages to be printed.
Definition: log.h:138
std::string vstrprintf(const char *format, va_list va)
Like sprintf, but self-managing a buffer with a std::string.
Definition: log.cpp:84
A log sink writing to stdout/stderr depending on severity.
Definition: log.h:146
Severity
Severity of a logging message.
Definition: log.h:61
bool ParseLoggerArguments(int &i, int argc, char *argv[], Severity &console_verbosity)
Helper function for parsing arguments that use common syntax.
Definition: log.cpp:178
@ WARNING
Something went wrong, but we'll attempt to proceed.
@ FATAL
State is totally unusable, must exit right now. Aborts the program after printing.
@ TRACE
Debug information that can be switched on/off on a per function basis.
@ NOTICE
Useful information about progress printed by default.
@ ERROR
Something went very wrong, an operation may be aborted or state may be confused.
@ DEBUG
Extremely detailed information only useful to people working on application internals.
@ VERBOSE
Detailed information end users may sometimes need, but not often.
__thread unsigned int g_logIndentLevel
The current indentation level.
Definition: log.cpp:53
ATTR_FORMAT(2, 3) void LogDebugTrace(const char *function
Just print the message at given log level, don't do anything special for warnings or errors.
std::vector< std::unique_ptr< LogSink > > g_log_sinks
The set of log sink objects logtools knows about.
Definition: log.cpp:62
std::mutex g_log_mutex
Mutex for serializing access to global logging state.
Definition: log.cpp:47
std::set< std::string > g_trace_filters
Set of classes or class::function for high verbosity trace messages.
Definition: log.cpp:76