SNode.C
Loading...
Searching...
No Matches
Logger.cpp
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 DOXYGEN_SHOULD_SKIP_THIS
43
44#include "log/Logger.h"
45
46#include <string>
47
48#endif /* DOXYGEN_SHOULD_SKIP_THIS */
49
50INITIALIZE_EASYLOGGINGPP
51
52namespace logger {
53
54 void Logger::init() {
55 el::Configurations conf = *el::Loggers::defaultConfigurations();
56
57 conf.setGlobally(el::ConfigurationType::Enabled, "true");
58 conf.setGlobally(el::ConfigurationType::Format, "%datetime{%Y-%M-%d %H:%m:%s} %tick %level %msg");
59 conf.setGlobally(el::ConfigurationType::ToFile, "false");
60 conf.setGlobally(el::ConfigurationType::ToStandardOutput, "true");
61 conf.setGlobally(el::ConfigurationType::SubsecondPrecision, "2");
62 conf.setGlobally(el::ConfigurationType::PerformanceTracking, "false");
63 conf.setGlobally(el::ConfigurationType::MaxLogFileSize, "2097152");
64 conf.setGlobally(el::ConfigurationType::LogFlushThreshold, "0");
65 conf.set(el::Level::Verbose, el::ConfigurationType::Format, "%datetime{%Y-%M-%d %H:%m:%s} %tick %msg");
66
67 el::Loggers::addFlag(el::LoggingFlag::DisableApplicationAbortOnFatalLog);
68 el::Loggers::addFlag(el::LoggingFlag::DisablePerformanceTrackingCheckpointComparison);
69 el::Loggers::addFlag(el::LoggingFlag::ColoredTerminalOutput);
70 el::Loggers::removeFlag(el::LoggingFlag::AllowVerboseIfModuleNotSpecified);
71
72 el::Loggers::setDefaultConfigurations(conf, true);
73
74 setDisableColor(::isatty(::fileno(stdout)) == 0);
75
78 }
79
80 void Logger::setCustomFormatSpec(const char* format, const el::FormatSpecifierValueResolver& resolver) {
81 el::Helpers::installCustomFormatSpecifier(el::CustomFormatSpecifier(format, resolver));
82 }
83
84 void Logger::setDisableColor(bool disableColorLog) {
85 if (disableColorLog) {
86 el::Loggers::removeFlag(el::LoggingFlag::ColoredTerminalOutput);
87 } else {
88 el::Loggers::addFlag(el::LoggingFlag::ColoredTerminalOutput);
89 }
90
91 Logger::disableColorLog = disableColorLog;
92 }
93
95 return disableColorLog;
96 }
97
98 // Application logging should be done with VLOG(loglevel)
99 // Framework logging should use one of the following levels
100 void Logger::setLogLevel(int level) {
101 el::Configurations conf = *el::Loggers::defaultConfigurations();
102
103 conf.set(el::Level::Trace, el::ConfigurationType::Enabled, "false"); // trace method/function calling
104 conf.set(el::Level::Debug, el::ConfigurationType::Enabled, "false"); // typical assert messages - but we can go on
105 conf.set(el::Level::Info, el::ConfigurationType::Enabled, "false"); // additional infos - what 's going on
106 conf.set(el::Level::Warning, el::ConfigurationType::Enabled, "false"); // not that serious but mentioning
107 conf.set(el::Level::Error, el::ConfigurationType::Enabled, "false"); // serious errors - but we can go on
108 conf.set(el::Level::Fatal, el::ConfigurationType::Enabled, "false"); // asserts - stop - we can not go on
109
110 switch (level) {
111 case 6:
112 conf.set(el::Level::Trace, el::ConfigurationType::Enabled, "true");
113 [[fallthrough]];
114 case 5:
115 conf.set(el::Level::Debug, el::ConfigurationType::Enabled, "true");
116 [[fallthrough]];
117 case 4:
118 conf.set(el::Level::Info, el::ConfigurationType::Enabled, "true");
119 [[fallthrough]];
120 case 3:
121 conf.set(el::Level::Warning, el::ConfigurationType::Enabled, "true");
122 [[fallthrough]];
123 case 2:
124 conf.set(el::Level::Error, el::ConfigurationType::Enabled, "true");
125 [[fallthrough]];
126 case 1:
127 conf.set(el::Level::Fatal, el::ConfigurationType::Enabled, "true");
128 [[fallthrough]];
129 case 0:
130 [[fallthrough]];
131 default:;
132 }
133
134 el::Loggers::setDefaultConfigurations(conf, true);
135 }
136
137 void Logger::setVerboseLevel(int level) {
138 if (level >= 0) {
139 el::Loggers::setVerboseLevel(static_cast<el::base::type::VerboseLevel>(level));
140 }
141 }
142
143 void Logger::logToFile(const std::string& logFile) {
144 el::Configurations conf = *el::Loggers::defaultConfigurations();
145
146 conf.setGlobally(el::ConfigurationType::Filename, logFile);
147 conf.setGlobally(el::ConfigurationType::ToFile, "true");
148
149 el::Loggers::setDefaultConfigurations(conf, true);
150 }
151
152 void Logger::setQuiet(bool quiet) {
153 el::Configurations conf = *el::Loggers::defaultConfigurations();
154
155 conf.setGlobally(el::ConfigurationType::ToStandardOutput, quiet ? "false" : "true");
156
157 el::Loggers::setDefaultConfigurations(conf, true);
158 }
159
160 bool Logger::disableColorLog = false;
161
162} // namespace logger
163
164std::ostream& Color::operator<<(std::ostream& os, const Code& code) {
165 return os << (!logger::Logger::disableColorLog ? ("\033[" + std::to_string(static_cast<int>(code)) + "m") : "");
166}
167
168std::string Color::operator+(const std::string& string, const Code& code) {
169 return string + (!logger::Logger::disableColorLog ? ("\033[" + std::to_string(static_cast<int>(code)) + "m") : "");
170}
171
172std::string Color::operator+(const Code& code, const std::string& string) {
173 return (!logger::Logger::disableColorLog ? ("\033[" + std::to_string(static_cast<int>(code)) + "m") : "") + string;
174}
static bool disableColorLog
Definition Logger.h:124
static void setDisableColor(bool disableColorLog=true)
Definition Logger.cpp:84
static void logToFile(const std::string &logFile)
Definition Logger.cpp:143
static void setVerboseLevel(int level)
Definition Logger.cpp:137
static void setCustomFormatSpec(const char *format, const el::FormatSpecifierValueResolver &resolver)
Definition Logger.cpp:80
static void setLogLevel(int level)
Definition Logger.cpp:100
static void init()
Definition Logger.cpp:54
static void setQuiet(bool quiet=true)
Definition Logger.cpp:152
static bool getDisableColor()
Definition Logger.cpp:94
Definition Logger.h:66
Code
Definition Logger.h:68
std::string operator+(const std::string &string, const Code &code)
Definition Logger.cpp:168
std::string operator+(const Code &code, const std::string &string)
Definition Logger.cpp:172