ngscopeclient v0.2.2
Loading...
Searching...
No Matches
log.h
Go to the documentation of this file.
1/***********************************************************************************************************************
2* *
3* logtools *
4* *
5* Copyright (c) 2016-2026 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
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(const 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 const 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 //not copyable or assignable
188 FILELogSink(const FILELogSink& rhs) =delete;
189 FILELogSink& operator=(const FILELogSink& rhs) =delete;
190
191 void Log(Severity severity, const std::string &msg) override;
192 void Log(Severity severity, const char *format, va_list va) override;
193
194protected:
195 FILE *m_file;
196};
197
198extern std::mutex g_log_mutex;
199extern std::vector<std::unique_ptr<LogSink>> g_log_sinks;
200extern std::set<std::string> g_trace_filters;
201
209{
210public:
211 LogIndenter();
212
213 ~LogIndenter();
214};
215
221 int& i,
222 int argc,
223 char* argv[],
224 Severity& console_verbosity);
225
226
227#ifdef __GNUC__
228#if defined(__MINGW32__) && !defined(__clang__)
229#define ATTR_FORMAT(n, m) __attribute__((__format__ (gnu_printf, n, m)))
230#else
231#define ATTR_FORMAT(n, m) __attribute__((__format__ (__printf__, n, m)))
232#endif
233#define ATTR_NORETURN __attribute__((noreturn))
234#else
235//FIXME on MSVC/Windows
236#define ATTR_FORMAT(n, m)
237#define ATTR_NORETURN
238#endif
239
249#ifdef __GNUC__
250#define LogTrace(...) LogDebugTrace(__PRETTY_FUNCTION__, ##__VA_ARGS__)
251#else
252#define LogTrace(...) LogDebugTrace(__func__, __VA_ARGS__)
253#endif
254
255ATTR_FORMAT(1, 2) void LogVerbose(const char *format, ...);
256ATTR_FORMAT(1, 2) void LogNotice(const char *format, ...);
257ATTR_FORMAT(1, 2) void LogWarning(const char *format, ...);
258ATTR_FORMAT(1, 2) void LogError(const char *format, ...);
259ATTR_FORMAT(1, 2) void LogDebug(const char *format, ...);
260ATTR_FORMAT(2, 3) void LogDebugTrace(const char* function, const char *format, ...);
261ATTR_FORMAT(1, 2) ATTR_NORETURN void LogFatal(const char *format, ...);
262
264ATTR_FORMAT(2, 3) void Log(Severity severity, const char *format, ...);
265
266#undef ATTR_FORMAT
267#undef ATTR_NORETURN
268
269std::string LogHexDump(const unsigned char* data, size_t len);
270
271#endif
272
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, const 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:209
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 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
std::string WrapString(const std::string &str)
Wraps long lines and adds indentation as needed.
Definition log.cpp:110
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
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
const char const char std::string LogHexDump(const unsigned char *data, size_t len)
Convert a sequence of bytes to its textual representation ("hex dump").
Definition log.cpp:456
std::set< std::string > g_trace_filters
Set of classes or class::function for high verbosity trace messages.
Definition log.cpp:76