AsmGrader 0.0.0
Loading...
Searching...
No Matches
logging.hpp
Go to the documentation of this file.
1#pragma once
2
4// clangd isn't good at recognizing/finding template specializations, so just including
5// fmt::formatter specializations here makes my life easier, even if compile times are
6// worse every time it's changed
7#include <asmgrader/common/extra_formatters.hpp> // IWYU pragma: keep
8
9// Set log level based on whether we're in DEBUG mode
10// Needs to be done before including spdlog
11#include <fmt/chrono.h>
12#include <fmt/color.h>
13#include <fmt/ranges.h>
14
15#include <cerrno>
16#include <chrono>
17#include <cstdio>
18#include <cstdlib> // For abort
19#include <stdexcept>
20#include <string>
21#include <string_view>
22#include <system_error>
23// #include <fmt/std.h> // FIXME: This generates errors...
24
25#if defined(TRACE)
26#define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_TRACE
27#define SPDLOG_FUNCTION __PRETTY_FUNCTION__
28#elif defined(DEBUG)
29#define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_DEBUG
30#define SPDLOG_FUNCTION __PRETTY_FUNCTION__
31#elif defined(RELEASE)
32#define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_ERROR
33#else
34#define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_INFO
35#endif
36
37#include <spdlog/cfg/env.h>
38#include <spdlog/common.h>
39#include <spdlog/sinks/stdout_color_sinks.h>
40#include <spdlog/spdlog.h>
41
42// Wrappers for spdlog macros
43#define LOG_TRACE(...) SPDLOG_TRACE(__VA_ARGS__)
44#define LOG_DEBUG(...) SPDLOG_DEBUG(__VA_ARGS__)
45#define LOG_INFO(...) SPDLOG_INFO(__VA_ARGS__)
46#define LOG_WARN(...) SPDLOG_WARN(__VA_ARGS__)
47#define LOG_ERROR(...) SPDLOG_ERROR(__VA_ARGS__)
48#define LOG_FATAL(...) SPDLOG_CRITICAL(__VA_ARGS__)
49
52#define UNIMPLEMENTED(msg, ...) \
53 do { /* NOLINT(cppcoreguidelines-avoid-do-while) */ \
54 LOG_FATAL("Feature not implemented! " msg __VA_OPT__(, ) __VA_ARGS__); \
55 std::abort(); \
56 } while (false)
57
58#ifdef DEBUG
59#define DEBUG_TIME(expr) \
60 [&]() { \
61 ::asmgrader::detail::DebugTimeHelper debug_time_helper__(#expr); \
62 return expr; \
63 }()
64#else
65#define DEBUG_TIME(fn, ...)
66#endif
67
68namespace asmgrader {
69namespace detail {
70
71// AllowImplicitlyDeletedCopyOrMove is set to true, so the lint here seems like a false positive
72// NOLINTNEXTLINE(cppcoreguidelines-special-member-functions)
74{
75 explicit DebugTimeHelper(std::string_view str)
76 : str_expr(str) {
77 start = std::chrono::steady_clock::now();
78 }
79
81 [[maybe_unused]] const auto& stop = std::chrono::steady_clock::now();
82 LOG_DEBUG("{} took {:%S}s to execute", str_expr, stop - start);
83 }
84
85 std::string_view str_expr;
86 std::chrono::steady_clock::time_point start;
87};
88
89} // namespace detail
90
92inline std::string get_err_msg(int err) {
93 return std::error_code(err, std::generic_category()).message();
94}
95
97inline std::string get_err_msg() {
98 return get_err_msg(errno);
99}
100
101inline void init_loggers() {
102#if defined(DEBUG)
103 spdlog::set_level(spdlog::level::warn);
104#elif defined(RELEASE)
105 spdlog::set_level(spdlog::level::err);
106#else
107 spdlog::set_level(spdlog::level::info);
108#endif
109
110 // Override any previously set log-level with the enviornment variable SPDLOG_LEVEL, if set
111 spdlog::cfg::load_env_levels("LOG_LEVEL");
112
113#if defined(DEBUG) || defined(TRACE)
114 spdlog::set_pattern("[%T.%e] [%^%8l%$] [pid %6P] [%30!!@%20!s:%-4#] %v");
115#else
116 // Pattern:
117 // time - [HH:MM:SS.MS]
118 // level (colored, center aligned) - [ info ]
119 // process id - [pid 12345]
120 // message - "foo bar"
121 spdlog::set_pattern("[%T.%e] [%^%=8l%$] [pid %6P] %v");
122#endif
123
124 // Log to stderr. See https://github.com/gabime/spdlog/wiki/FAQ#switch-the-default-logger-to-stderr
125 spdlog::set_default_logger(spdlog::stderr_color_st("default"));
126}
127
128} // namespace asmgrader
A non-movable and non-copyable type.
Definition class_traits.hpp:38
#define LOG_DEBUG(...)
Definition logging.hpp:44
Definition asm_buffer.hpp:19
void init_loggers()
Definition logging.hpp:101
std::string get_err_msg()
Obtain Linux error (i.e., errno) message via libc functions.
Definition logging.hpp:97
Definition logging.hpp:74
DebugTimeHelper(std::string_view str)
Definition logging.hpp:75
~DebugTimeHelper()
Definition logging.hpp:80
std::chrono::steady_clock::time_point start
Definition logging.hpp:86
std::string_view str_expr
Definition logging.hpp:85