ngscopeclient 0.1-dev+51fbda87c
log.h
Go to the documentation of this file.
1/***********************************************************************************************************************
2* *
3* logtools *
4* *
5* Copyright (c) 2016-2024 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
53enum class Severity
54{
56 FATAL = 1,
57
59 ERROR = 2,
60
62 WARNING = 3,
63
65 NOTICE = 4,
66
68 VERBOSE = 5,
69
71 DEBUG = 6
72};
73
74extern __thread unsigned int g_logIndentLevel;
75
81{
82public:
83 LogSink(Severity min_severity = Severity::VERBOSE)
84 : m_indentSize(4)
85 , m_termWidth(120) //default if not using ioctls to check
87 , m_min_severity(min_severity)
88 {}
89
90 virtual ~LogSink() {}
91
94 { return m_min_severity; }
95
102 std::string GetIndentString();
103
104 virtual void Log(Severity severity, const std::string &msg) = 0;
105 virtual void Log(Severity severity, const char *format, va_list va) = 0;
106
107 std::string vstrprintf(const char* format, va_list va);
108
109protected:
110
111 std::string WrapString(std::string str);
112 virtual void PreprocessLine(std::string& line);
113
115 unsigned int m_indentSize;
116
118 unsigned int m_termWidth;
119
122
125};
126
131class STDLogSink : public LogSink
132{
133public:
134 STDLogSink(Severity min_severity = Severity::VERBOSE);
135 ~STDLogSink() override;
136
137 void Log(Severity severity, const std::string &msg) override;
138 void Log(Severity severity, const char *format, va_list va) override;
139
140protected:
141 void Flush();
142};
143
149{
150public:
152 ~ColoredSTDLogSink() override;
153
154protected:
155 void PreprocessLine(std::string& line) override;
156 std::string replace(
157 const std::string& search,
158 const std::string& before,
159 const std::string& after,
160 std::string subject);
161};
162
167class FILELogSink : public LogSink
168{
169public:
170 FILELogSink(FILE *f, bool line_buffered = false, Severity min_severity = Severity::VERBOSE);
171 ~FILELogSink() override;
172
173 void Log(Severity severity, const std::string &msg) override;
174 void Log(Severity severity, const char *format, va_list va) override;
175
176protected:
177 FILE *m_file;
178};
179
180extern std::mutex g_log_mutex;
181extern std::vector<std::unique_ptr<LogSink>> g_log_sinks;
182extern std::set<std::string> g_trace_filters;
183
191{
192public:
193 LogIndenter();
194
195 ~LogIndenter();
196};
197
203 int& i,
204 int argc,
205 char* argv[],
206 Severity& console_verbosity);
207
208
209#ifdef __GNUC__
210#if defined(__MINGW32__) && !defined(__clang__)
211#define ATTR_FORMAT(n, m) __attribute__((__format__ (gnu_printf, n, m)))
212#else
213#define ATTR_FORMAT(n, m) __attribute__((__format__ (__printf__, n, m)))
214#endif
215#define ATTR_NORETURN __attribute__((noreturn))
216#else
217//FIXME on MSVC/Windows
218#define ATTR_FORMAT(n, m)
219#define ATTR_NORETURN
220#endif
221
231#ifdef __GNUC__
232#define LogTrace(...) LogDebugTrace(__PRETTY_FUNCTION__, ##__VA_ARGS__)
233#else
234#define LogTrace(...) LogDebugTrace(__func__, __VA_ARGS__)
235#endif
236
237ATTR_FORMAT(1, 2) void LogVerbose(const char *format, ...);
238ATTR_FORMAT(1, 2) void LogNotice(const char *format, ...);
239ATTR_FORMAT(1, 2) void LogWarning(const char *format, ...);
240ATTR_FORMAT(1, 2) void LogError(const char *format, ...);
241ATTR_FORMAT(1, 2) void LogDebug(const char *format, ...);
242ATTR_FORMAT(2, 3) void LogDebugTrace(const char* function, const char *format, ...);
243ATTR_FORMAT(1, 2) ATTR_NORETURN void LogFatal(const char *format, ...);
244
246ATTR_FORMAT(2, 3) void Log(Severity severity, const char *format, ...);
247
248#undef ATTR_FORMAT
249#undef ATTR_NORETURN
250
251#endif
252
A STDLogSink that colorizes "warning" or "error" keywords.
Definition: log.h:149
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:168
RAII wrapper for log indentation.
Definition: log.h:191
Base class for all log sinks.
Definition: log.h:81
unsigned int m_termWidth
Width of the console we're printing to, in characters.
Definition: log.h:118
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:93
bool m_lastMessageWasNewline
True if the last message ended in a character.
Definition: log.h:121
unsigned int m_indentSize
Number of spaces in one indentation.
Definition: log.h:115
Severity m_min_severity
Minimum severity of messages to be printed.
Definition: log.h:124
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:132
Severity
Severity of a logging message.
Definition: log.h:54
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.
@ 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