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#ifndef DOXYGEN_SHOULD_SKIP_THIS
21
22#include "log/Logger.h"
23
24#include <string>
25
26#endif /* DOXYGEN_SHOULD_SKIP_THIS */
27
28INITIALIZE_EASYLOGGINGPP
29
30namespace logger {
31
32 void Logger::init() {
33 el::Configurations conf = *el::Loggers::defaultConfigurations();
34
35 conf.setGlobally(el::ConfigurationType::Enabled, "true");
36 conf.setGlobally(el::ConfigurationType::Format, "%datetime{%Y-%M-%d %H:%m:%s} %tick %level %msg");
37 conf.setGlobally(el::ConfigurationType::ToFile, "false");
38 conf.setGlobally(el::ConfigurationType::ToStandardOutput, "true");
39 conf.setGlobally(el::ConfigurationType::SubsecondPrecision, "2");
40 conf.setGlobally(el::ConfigurationType::PerformanceTracking, "false");
41 conf.setGlobally(el::ConfigurationType::MaxLogFileSize, "2097152");
42 conf.setGlobally(el::ConfigurationType::LogFlushThreshold, "0");
43 conf.set(el::Level::Verbose, el::ConfigurationType::Format, "%datetime{%Y-%M-%d %H:%m:%s} %tick %msg");
44
45 el::Loggers::addFlag(el::LoggingFlag::DisableApplicationAbortOnFatalLog);
46 el::Loggers::addFlag(el::LoggingFlag::DisablePerformanceTrackingCheckpointComparison);
47 el::Loggers::addFlag(el::LoggingFlag::ColoredTerminalOutput);
48 el::Loggers::removeFlag(el::LoggingFlag::AllowVerboseIfModuleNotSpecified);
49
50 el::Loggers::setDefaultConfigurations(conf, true);
51
54 }
55
56 void Logger::setCustomFormatSpec(const char* format, const el::FormatSpecifierValueResolver& resolver) {
57 el::Helpers::installCustomFormatSpecifier(el::CustomFormatSpecifier(format, resolver));
58 }
59
60 // Application logging should be done with VLOG(loglevel)
61 // Framework logging should use one of the following levels
62 void Logger::setLogLevel(int level) {
63 el::Configurations conf = *el::Loggers::defaultConfigurations();
64
65 conf.set(el::Level::Trace, el::ConfigurationType::Enabled, "false"); // trace method/function calling
66 conf.set(el::Level::Debug, el::ConfigurationType::Enabled, "false"); // typical assert messages - but we can go on
67 conf.set(el::Level::Info, el::ConfigurationType::Enabled, "false"); // additional infos - what 's going on
68 conf.set(el::Level::Warning, el::ConfigurationType::Enabled, "false"); // not that serious but mentioning
69 conf.set(el::Level::Error, el::ConfigurationType::Enabled, "false"); // serious errors - but we can go on
70 conf.set(el::Level::Fatal, el::ConfigurationType::Enabled, "false"); // asserts - stop - we can not go on
71
72 switch (level) {
73 case 6:
74 conf.set(el::Level::Trace, el::ConfigurationType::Enabled, "true");
75 [[fallthrough]];
76 case 5:
77 conf.set(el::Level::Debug, el::ConfigurationType::Enabled, "true");
78 [[fallthrough]];
79 case 4:
80 conf.set(el::Level::Info, el::ConfigurationType::Enabled, "true");
81 [[fallthrough]];
82 case 3:
83 conf.set(el::Level::Warning, el::ConfigurationType::Enabled, "true");
84 [[fallthrough]];
85 case 2:
86 conf.set(el::Level::Error, el::ConfigurationType::Enabled, "true");
87 [[fallthrough]];
88 case 1:
89 conf.set(el::Level::Fatal, el::ConfigurationType::Enabled, "true");
90 [[fallthrough]];
91 case 0:
92 [[fallthrough]];
93 default:;
94 }
95
96 el::Loggers::setDefaultConfigurations(conf, true);
97 }
98
99 void Logger::setVerboseLevel(int level) {
100 if (level >= 0) {
101 el::Loggers::setVerboseLevel(static_cast<el::base::type::VerboseLevel>(level));
102 }
103 }
104
105 void Logger::logToFile(const std::string& logFile) {
106 el::Configurations conf = *el::Loggers::defaultConfigurations();
107
108 conf.setGlobally(el::ConfigurationType::Filename, logFile);
109 conf.setGlobally(el::ConfigurationType::ToFile, "true");
110
111 el::Loggers::setDefaultConfigurations(conf, true);
112 }
113
114 void Logger::setQuiet(bool quiet) {
115 el::Configurations conf = *el::Loggers::defaultConfigurations();
116
117 conf.setGlobally(el::ConfigurationType::ToStandardOutput, quiet ? "false" : "true");
118
119 el::Loggers::setDefaultConfigurations(conf, true);
120 }
121
122} // namespace logger
123
125 return os << "\033[" << static_cast<int>(code) << "m";
126}
static void setVerboseLevel(int level)
Definition Logger.cpp:99
static void setCustomFormatSpec(const char *format, const el::FormatSpecifierValueResolver &resolver)
Definition Logger.cpp:56
static void setLogLevel(int level)
Definition Logger.cpp:62
static void init()
Definition Logger.cpp:32
static void setQuiet(bool quiet=true)
Definition Logger.cpp:114
Definition Logger.h:42
std::ostream & operator<<(std::ostream &os, Code code)
Definition Logger.cpp:124