-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.hpp
116 lines (102 loc) · 3.13 KB
/
logger.hpp
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#pragma once
#include <thread>
#include <iostream>
#include <fstream>
#include <filesystem>
#include <fmt/core.h>
#include <fmt/ranges.h>
#include <fmt/format.h>
#include <fmt/chrono.h>
#include <fmt/std.h>
#include <fmt/printf.h>
#if __cpp_lib_source_location
#include <source_location>
#endif
#if _WIN64
#define LOGGER_EXPORT __declspec(dllexport)
#else
#define LOGGER_EXPORT
#endif
#if ENABLE_SLOG
#include "slog.h"
#else
enum { L_FATAL, L_ERR, L_WARN, L_INFO, L_STATUS, L_V, L_DEBUG, L_TRACE, L_STATS, L_MAX = L_STATS };
#endif
namespace logger
{
enum class LogLevel : int
{
Trace = L_TRACE,
Debug = L_DEBUG,
Info = L_INFO,
Warning = L_WARN,
Error = L_ERR,
Fatal = L_FATAL
};
extern "C" LOGGER_EXPORT bool SetLogFile(const char* path);
extern "C" LOGGER_EXPORT void ExternalLog(LogLevel level, const char* format);
std::string log_binary_data(const unsigned char *const src, int size, int chunk_size);
namespace {
const std::filesystem::path logFile;
}
template <typename... Args>
struct LOGGER_EXPORT Log
{
Log(LogLevel level, const char* format, Args &&...args
#if __cpp_lib_source_location
, const std::source_location& location = std::source_location::current()
#endif
)
{
#if ENABLE_SLOG
#if USE_CXX_FORMAT
slog_tag(SLOG_TAG, static_cast<int>(level), "%s\n", fmt::vformat(format, fmt::make_format_args(args...)).c_str());
#else
slog_tag(SLOG_TAG, static_cast<int>(level), "%s\n", fmt::sprintf(format, args...).c_str());
#endif
return;
#endif
std::string content;
std::time_t t = std::time(nullptr);
fmt::format_to(std::back_inserter(content), "{:%Y-%m-%d %H:%M:%S} [{}][{}] ", fmt::localtime(t), static_cast<int>(level), std::this_thread::get_id());
#if __cpp_lib_source_location
fmt::format_to(std::back_inserter(content), "{}({},{}): ", location.file_name(), location.line(), location.function_name());
#endif
#if USE_CXX_FORMAT
fmt::vformat_to(std::back_inserter(content), format, fmt::make_format_args(args...));
#else
content += fmt::sprintf(format, args...);
#endif
if (logFile.empty())
{
fmt::print("{:s}\n", content);
return;
}
std::ofstream logStream{logFile, std::ios::out | std::ios::app};
if (logStream.is_open())
{
fmt::print(logStream, "{:s}\n", content);
logStream.close();
}
}
};
template <typename... Args>
Log(LogLevel, const char*, Args &&...) -> Log<Args...>;
}
using namespace logger;
#if DEBUG
#define CHECK_VARIABLE(x, y) \
do \
{ \
Log(LogLevel::Debug, #x ": " y, x); \
} while (0);
#define TRACE(x) \
do \
{ \
Log(LogLevel::Trace, x); \
} while (0);
#else
#define CHECK_VARIABLE(x, y) ((void)0)
#define TRACE(x) ((void)0)
#endif
#undef LOGGER_EXPORT