forked from LearningByExample/ModernCppCI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.h
91 lines (71 loc) · 2.21 KB
/
logger.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
* Distributed under the MIT License (See accompanying file /LICENSE )
*/
#ifndef LOG_HPP
#define LOG_HPP
// MS compatible compilers support #pragma once
#if defined(_MSC_VER)
#pragma once
#endif
#include <spdlog/fmt/ostr.h>
#include <spdlog/spdlog.h>
#include <string>
namespace ModernCppCI {
enum class LogLevel {
info = spdlog::level::level_enum::info,
trace = spdlog::level::level_enum::trace,
debug = spdlog::level::level_enum::debug,
error = spdlog::level::level_enum::err
};
class Logger {
public:
Logger(const std::string §ion);
auto section() const noexcept { return section_; }
template <typename Arg1, typename... Args>
void log(const LogLevel level, const char *fmt, const Arg1 &arg1,
const Args &... args) {
auto new_fmt = "[{}] " + std::string(fmt);
internal_logger_->log(static_cast<spdlog::level::level_enum>(level),
new_fmt.c_str(), section(), arg1, args...);
}
template <typename Arg1, typename... Args>
void info(const char *fmt, const Arg1 &arg1, const Args &... args) {
log(LogLevel::info, fmt, arg1, args...);
}
template <typename Arg1>
void info(const Arg1 &arg1) {
info("{}", arg1);
}
template <typename Arg1, typename... Args>
void debug(const char *fmt, const Arg1 &arg1, const Args &... args) {
log(LogLevel::debug, fmt, arg1, args...);
}
template <typename Arg1>
void debug(const Arg1 &arg1) {
debug("{}", arg1);
}
template <typename Arg1, typename... Args>
void trace(const char *fmt, const Arg1 &arg1, const Args &... args) {
log(LogLevel::trace, fmt, arg1, args...);
}
template <typename Arg1>
void trace(const Arg1 &arg1) {
trace("{}", arg1);
}
template <typename Arg1, typename... Args>
void error(const char *fmt, const Arg1 &arg1, const Args &... args) {
log(LogLevel::error, fmt, arg1, args...);
}
template <typename Arg1>
void error(const Arg1 &arg1) {
error("{}", arg1);
}
static void level(const LogLevel level) noexcept {
spdlog::set_level(static_cast<spdlog::level::level_enum>(level));
}
private:
std::string section_{""};
std::shared_ptr<spdlog::logger> internal_logger_{nullptr};
};
} // namespace ModernCppCI
#endif // LOG_HPP