2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
42#ifndef DOXYGEN_SHOULD_SKIP_THIS
44#include "log/Logger.h"
50#include <spdlog/logger.h>
51#include <spdlog/pattern_formatter.h>
52#include <spdlog/sinks/basic_file_sink.h>
53#include <spdlog/sinks/rotating_file_sink.h>
54#include <spdlog/sinks/stdout_color_sinks.h>
70 using Clock = std::chrono::steady_clock;
75 class TickFlagFormatter
final :
public spdlog::custom_flag_formatter {
77 void format(
const spdlog::details::log_msg&,
const std::tm&, spdlog::memory_buf_t& dest)
override {
78 std::string tick = tickResolver ? tickResolver() :
defaultTick();
79 dest.append(tick.data(), tick.data() +
static_cast<std::ptrdiff_t>(tick.size()));
83 return spdlog::details::make_unique<TickFlagFormatter>();
88 const auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(
Clock::now() - startTime).count();
89 std::string tick = std::to_string(elapsed);
90 if (tick.size() < 13) {
91 tick.insert(0, 13 - tick.size(),
'0');
195 stdoutSink = std::make_shared<spdlog::sinks::stdout_color_sink_st>();
196 stdoutLogger = std::make_shared<spdlog::logger>(
"snodec-stdout", stdoutSink);
197 auto stdoutPattern = std::make_unique<spdlog::pattern_formatter>();
198 stdoutPattern->add_flag<TickFlagFormatter>(
'*').set_pattern(
"%Y-%m-%d %H:%M:%S %* %v");
204 disableColorLog = ::isatty(::fileno(stdout)) == 0;
222 constexpr std::size_t maxSize = 2 * 1024 * 1024;
223 constexpr std::size_t maxFiles = 3;
224 fileSink = std::make_shared<spdlog::sinks::rotating_file_sink_st>(logFile, maxSize, maxFiles);
225 fileLogger = std::make_shared<spdlog::logger>(
"snodec-file", fileSink);
226 auto filePattern = std::make_unique<spdlog::pattern_formatter>();
227 filePattern->add_flag<TickFlagFormatter>(
'*').set_pattern(
"%Y-%m-%d %H:%M:%S %* %v");
228 fileLogger->set_formatter(std::move(filePattern));
256 static void emitLine(
Level level, std::string message,
const bool withErrno,
const int errnoValue) {
263 message += std::strerror(errnoValue);
274 fileLogger->log(spdlog::level::info, message);
300std::ostream&
Color::operator<<(std::ostream& os,
const Code& code) {
LogMessage(Level level, int verboseLevel=-1, bool withErrno=false)
std::ostringstream message
std::ostringstream & stream()
static bool disableColorLog
static void setTickResolver(TickResolver resolver)
static void setDisableColor(bool disableColorLog=true)
static void logToFile(const std::string &logFile)
static void setVerboseLevel(int level)
static void setLogLevel(int level)
std::function< std::string()> TickResolver
static void setQuiet(bool quiet=true)
static bool shouldVerbose(int verboseLevel)
static bool getDisableColor()
static bool shouldLog(Level level)
static void disableLogToFile()
std::string operator+(const std::string &string, const Code &code)
std::string operator+(const Code &code, const std::string &string)
std::shared_ptr< spdlog::sinks::rotating_file_sink_st > fileSink
std::string colorizeLevel(const logger::Level level)
bool shouldEmit(const logger::Level level)
std::string levelName(const logger::Level level)
int configuredVerboseLevel
Clock::time_point startTime
std::chrono::steady_clock Clock
std::shared_ptr< spdlog::logger > stdoutLogger
std::shared_ptr< spdlog::sinks::stdout_color_sink_st > stdoutSink
logger::Logger::TickResolver tickResolver
std::shared_ptr< spdlog::logger > fileLogger
std::string defaultTick()
Color::Code levelColor(const logger::Level level)
static void emitLine(Level level, std::string message, const bool withErrno, const int errnoValue)