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
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 setVerboseLevel(0);
75 setLogLevel(0);
76 }
77
78 void Logger::setCustomFormatSpec(const char* format, const el::FormatSpecifierValueResolver& resolver) {
79 el::Helpers::installCustomFormatSpecifier(el::CustomFormatSpecifier(format, resolver));
80 }
81
82 // Application logging should be done with VLOG(loglevel)
83 // Framework logging should use one of the following levels
84 void Logger::setLogLevel(int level) {
85 el::Configurations conf = *el::Loggers::defaultConfigurations();
86
87 conf.set(el::Level::Trace, el::ConfigurationType::Enabled, "false"); // trace method/function calling
88 conf.set(el::Level::Debug, el::ConfigurationType::Enabled, "false"); // typical assert messages - but we can go on
89 conf.set(el::Level::Info, el::ConfigurationType::Enabled, "false"); // additional infos - what 's going on
90 conf.set(el::Level::Warning, el::ConfigurationType::Enabled, "false"); // not that serious but mentioning
91 conf.set(el::Level::Error, el::ConfigurationType::Enabled, "false"); // serious errors - but we can go on
92 conf.set(el::Level::Fatal, el::ConfigurationType::Enabled, "false"); // asserts - stop - we can not go on
93
94 switch (level) {
95 case 6:
96 conf.set(el::Level::Trace, el::ConfigurationType::Enabled, "true");
97 [[fallthrough]];
98 case 5:
99 conf.set(el::Level::Debug, el::ConfigurationType::Enabled, "true");
100 [[fallthrough]];
101 case 4:
102 conf.set(el::Level::Info, el::ConfigurationType::Enabled, "true");
103 [[fallthrough]];
104 case 3:
105 conf.set(el::Level::Warning, el::ConfigurationType::Enabled, "true");
106 [[fallthrough]];
107 case 2:
108 conf.set(el::Level::Error, el::ConfigurationType::Enabled, "true");
109 [[fallthrough]];
110 case 1:
111 conf.set(el::Level::Fatal, el::ConfigurationType::Enabled, "true");
112 [[fallthrough]];
113 case 0:
114 [[fallthrough]];
115 default:;
116 }
117
118 el::Loggers::setDefaultConfigurations(conf, true);
119 }
120
121 void Logger::setVerboseLevel(int level) {
122 if (level >= 0) {
123 el::Loggers::setVerboseLevel(static_cast<el::base::type::VerboseLevel>(level));
124 }
125 }
126
127 void Logger::logToFile(const std::string& logFile) {
128 el::Configurations conf = *el::Loggers::defaultConfigurations();
129
130 conf.setGlobally(el::ConfigurationType::Filename, logFile);
131 conf.setGlobally(el::ConfigurationType::ToFile, "true");
132
133 el::Loggers::setDefaultConfigurations(conf, true);
134 }
135
136 void Logger::setQuiet(bool quiet) {
137 el::Configurations conf = *el::Loggers::defaultConfigurations();
138
139 conf.setGlobally(el::ConfigurationType::ToStandardOutput, quiet ? "false" : "true");
140
141 el::Loggers::setDefaultConfigurations(conf, true);
142 }
143
144} // namespace logger
145
146std::ostream& Color::operator<<(std::ostream& os, Code code) {
147 return os << "\033[" << static_cast<int>(code) << "m";
148}