ngscopeclient v0.1-rc1
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
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};
80
81extern __thread unsigned int g_logIndentLevel;
82
88{
89public:
90 LogSink(Severity min_severity = Severity::VERBOSE)
91 : m_indentSize(4)
92 , m_termWidth(120) //default if not using ioctls to check
94 , m_min_severity(min_severity)
95 {}
96
97 virtual ~LogSink() {}
98
101 { return m_min_severity; }
102
109 std::string GetIndentString();
110
111 virtual void Log(Severity severity, const std::string &msg) = 0;
112 virtual void Log(Severity severity, const char *format, va_list va) = 0;
113
114 std::string vstrprintf(const char* format, va_list va);
115
116protected:
117
118 std::string WrapString(std::string str);
119 virtual void PreprocessLine(std::string& line);
120
122 unsigned int m_indentSize;
123
125 unsigned int m_termWidth;
126
129
132};
133
138class STDLogSink : public LogSink
139{
140public:
141 STDLogSink(Severity min_severity = Severity::VERBOSE);
142 ~STDLogSink() override;
143
144 void Log(Severity severity, const std::string &msg) override;
145 void Log(Severity severity, const char *format, va_list va) override;
146
147protected:
148 void Flush();
149};
150
156{
157public:
159 ~ColoredSTDLogSink() override;
160
161protected:
162 void PreprocessLine(std::string& line) override;
163 std::string replace(
164 const std::string& search,
165 const std::string& before,
166 const std::string& after,
167 std::string subject);
168};
169
174class FILELogSink : public LogSink
175{
176public:
177 FILELogSink(FILE *f, bool line_buffered = false, Severity min_severity = Severity::VERBOSE);
178 ~FILELogSink() override;
179
180 void Log(Severity severity, const std::string &msg) override;
181 void Log(Severity severity, const char *format, va_list va) override;
182
183protected:
184 FILE *m_file;
185};
186
187extern std::mutex g_log_mutex;
188extern std::vector<std::unique_ptr<LogSink>> g_log_sinks;
189extern std::set<std::string> g_trace_filters;
190
198{
199public:
200 LogIndenter();
201
202 ~LogIndenter();
203};
204
210 int& i,
211 int argc,
212 char* argv[],
213 Severity& console_verbosity);
214
215
216#ifdef __GNUC__
217#if defined(__MINGW32__) && !defined(__clang__)
218#define ATTR_FORMAT(n, m) __attribute__((__format__ (gnu_printf, n, m)))
219#else
220#define ATTR_FORMAT(n, m) __attribute__((__format__ (__printf__, n, m)))
221#endif
222#define ATTR_NORETURN __attribute__((noreturn))
223#else
224//FIXME on MSVC/Windows
225#define ATTR_FORMAT(n, m)
226#define ATTR_NORETURN
227#endif
228
238#ifdef __GNUC__
239#define LogTrace(...) LogDebugTrace(__PRETTY_FUNCTION__, ##__VA_ARGS__)
240#else
241#define LogTrace(...) LogDebugTrace(__func__, __VA_ARGS__)
242#endif
243
244ATTR_FORMAT(1, 2) void LogVerbose(const char *format, ...);
245ATTR_FORMAT(1, 2) void LogNotice(const char *format, ...);
246ATTR_FORMAT(1, 2) void LogWarning(const char *format, ...);
247ATTR_FORMAT(1, 2) void LogError(const char *format, ...);
248ATTR_FORMAT(1, 2) void LogDebug(const char *format, ...);
249ATTR_FORMAT(2, 3) void LogDebugTrace(const char* function, const char *format, ...);
250ATTR_FORMAT(1, 2) ATTR_NORETURN void LogFatal(const char *format, ...);
251
253ATTR_FORMAT(2, 3) void Log(Severity severity, const char *format, ...);
254
255#undef ATTR_FORMAT
256#undef ATTR_NORETURN
257
258#endif
259
A STDLogSink that colorizes "warning" or "error" keywords.
Definition: log.h:156
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:175
RAII wrapper for log indentation.
Definition: log.h:198
Base class for all log sinks.
Definition: log.h:88
unsigned int m_termWidth
Width of the console we're printing to, in characters.
Definition: log.h:125
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:100
bool m_lastMessageWasNewline
True if the last message ended in a character.
Definition: log.h:128
unsigned int m_indentSize
Number of spaces in one indentation.
Definition: log.h:122
Severity m_min_severity
Minimum severity of messages to be printed.
Definition: log.h:131
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:139
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.
@ 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