SNode.C
Loading...
Searching...
No Matches
Logger.h
Go to the documentation of this file.
1/*
2 * SNode.C - A Slim Toolkit for Network Communication
3 * Copyright (C) Volker Christian <me@vchrist.at>
4 * 2020, 2021, 2022, 2023, 2024, 2025, 2026
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published
8 * by the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20/*
21 * MIT License
22 *
23 * Permission is hereby granted, free of charge, to any person obtaining a copy
24 * of this software and associated documentation files (the "Software"), to deal
25 * in the Software without restriction, including without limitation the rights
26 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
27 * copies of the Software, and to permit persons to whom the Software is
28 * furnished to do so, subject to the following conditions:
29 *
30 * The above copyright notice and this permission notice shall be included in
31 * all copies or substantial portions of the Software.
32 *
33 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
39 * THE SOFTWARE.
40 */
41
42#ifndef LOGGER_LOGGER_H
43#define LOGGER_LOGGER_H
44
45#ifndef DOXYGEN_SHOULD_SKIP_THIS
46
47#include <functional>
48#include <memory>
49#include <ostream> // IWYU pragma: export
50#include <sstream>
51#include <string>
52
53#endif /* DOXYGEN_SHOULD_SKIP_THIS */
54
55namespace Color {
56
80
81 std::ostream& operator<<(std::ostream& os, const Code& code);
82
83 std::string operator+(const std::string& string, const Code& code);
84 std::string operator+(const Code& code, const std::string& string);
85
86} // namespace Color
87
88namespace logger {
89
91
92 class Logger {
93 public:
94 using TickResolver = std::function<std::string()>;
95
96 Logger() = delete;
97 ~Logger() = delete;
98
99 static void init();
100 static void setLogLevel(int level);
101 static void setVerboseLevel(int level);
102 static void logToFile(const std::string& logFile);
103 static void disableLogToFile();
104 static void setQuiet(bool quiet = true);
105 static void setTickResolver(TickResolver resolver);
106 static void setDisableColor(bool disableColorLog = true);
107 static bool getDisableColor();
108
109 static bool shouldLog(Level level);
110 static bool shouldVerbose(int verboseLevel);
111
112 protected:
113 static bool disableColorLog;
114
115 friend std::ostream& Color::operator<<(std::ostream& os, const Color::Code& code);
116 friend std::string Color::operator+(const std::string& string, const Color::Code& code);
117 friend std::string Color::operator+(const Color::Code& code, const std::string& string);
118 };
119
121 public:
122 LogMessage(Level level, int verboseLevel = -1, bool withErrno = false);
123 ~LogMessage();
124
125 std::ostringstream& stream();
126
127 private:
133 std::ostringstream message;
134 };
135
136} // namespace logger
137
138#ifdef SNODEC_DISABLE_LOGLEVEL_LOGGING
139#define LOG(level)
140 if (true) {
141 } else
142 ::logger::LogMessage(::logger::Level::level).stream()
143#define PLOG(level)
144 if (true) {
145 } else
146 ::logger::LogMessage(::logger::Level::level, -1, true).stream()
147#else
148#define LOG(level)
149 if (!::logger::Logger::shouldLog(::logger::Level::level)) {
150 } else
151 ::logger::LogMessage(::logger::Level::level).stream()
152#define PLOG(level)
153 if (!::logger::Logger::shouldLog(::logger::Level::level)) {
154 } else
155 ::logger::LogMessage(::logger::Level::level, -1, true).stream()
156#endif
157
158#ifdef SNODEC_DISABLE_VERBOSE_LOGGING
159#define VLOG(level)
160 if (true) {
161 } else
162 ::logger::LogMessage(::logger::Level::VERBOSE, level).stream()
163#else
164#define VLOG(level)
165 if (!::logger::Logger::shouldVerbose(level)) {
166 } else
167 ::logger::LogMessage(::logger::Level::VERBOSE, level).stream()
168#endif
169
170#endif // LOGGER_LOGGER_H
#define VLOG(level)
Definition Logger.h:164
LogMessage(Level level, int verboseLevel=-1, bool withErrno=false)
Definition Logger.cpp:280
std::ostringstream message
Definition Logger.h:133
std::ostringstream & stream()
Definition Logger.cpp:294
static bool disableColorLog
Definition Logger.h:113
static void setTickResolver(TickResolver resolver)
Definition Logger.cpp:209
static void setDisableColor(bool disableColorLog=true)
Definition Logger.cpp:240
static void logToFile(const std::string &logFile)
Definition Logger.cpp:221
static void setVerboseLevel(int level)
Definition Logger.cpp:217
~Logger()=delete
static void setLogLevel(int level)
Definition Logger.cpp:213
std::function< std::string()> TickResolver
Definition Logger.h:94
static void init()
Definition Logger.cpp:193
static void setQuiet(bool quiet=true)
Definition Logger.cpp:236
static bool shouldVerbose(int verboseLevel)
Definition Logger.cpp:252
Logger()=delete
static bool getDisableColor()
Definition Logger.cpp:244
static bool shouldLog(Level level)
Definition Logger.cpp:248
static void disableLogToFile()
Definition Logger.cpp:231
int main(int argc, char *argv[])
Definition Logger.h:55
Code
Definition Logger.h:57
@ FG_LIGHT_CYAN
Definition Logger.h:73
@ BG_DEFAULT
Definition Logger.h:78
@ FG_LIGHT_YELLOW
Definition Logger.h:70
@ FG_LIGHT_GREEN
Definition Logger.h:69
@ FG_DARK_GRAY
Definition Logger.h:67
@ FG_LIGHT_RED
Definition Logger.h:68
@ FG_LIGHT_GRAY
Definition Logger.h:66
@ FG_LIGHT_MAGENTA
Definition Logger.h:72
@ FG_LIGHT_BLUE
Definition Logger.h:71
@ FG_YELLOW
Definition Logger.h:62
@ FG_DEFAULT
Definition Logger.h:58
@ FG_MAGENTA
Definition Logger.h:64
std::string operator+(const std::string &string, const Code &code)
Definition Logger.cpp:304
std::string operator+(const Code &code, const std::string &string)
Definition Logger.cpp:308
Level
Definition Logger.h:90