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(DEBUG) || defined(TRACE)
26#define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_TRACE
27#define SPDLOG_FUNCTION __PRETTY_FUNCTION__
28#elif defined(RELEASE)
29#define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_ERROR
30#else
31#define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_INFO
32#endif
33
34#include <spdlog/cfg/env.h>
35#include <spdlog/common.h>
36#include <spdlog/sinks/stdout_color_sinks.h>
37#include <spdlog/spdlog.h>
38
39// Wrappers for spdlog macros
40#define LOG_TRACE(...) SPDLOG_TRACE(__VA_ARGS__)
41#define LOG_DEBUG(...) SPDLOG_DEBUG(__VA_ARGS__)
42#define LOG_INFO(...) SPDLOG_INFO(__VA_ARGS__)
43#define LOG_WARN(...) SPDLOG_WARN(__VA_ARGS__)
44#define LOG_ERROR(...) SPDLOG_ERROR(__VA_ARGS__)
45#define LOG_FATAL(...) SPDLOG_CRITICAL(__VA_ARGS__)
46
49#define UNIMPLEMENTED(msg, ...) \
50 do { /* NOLINT(cppcoreguidelines-avoid-do-while) */ \
51 LOG_FATAL("Feature not implemented! " msg __VA_OPT__(, ) __VA_ARGS__); \
52 std::abort(); \
53 } while (false)
54
55#ifdef DEBUG
56#define DEBUG_TIME(expr) \
57 [&]() { \
58 ::asmgrader::detail::DebugTimeHelper debug_time_helper__(#expr); \
59 return expr; \
60 }()
61#else
62#define DEBUG_TIME(fn, ...)
63#endif
64
65namespace asmgrader {
66namespace detail {
67
68// AllowImplicitlyDeletedCopyOrMove is set to true, so the lint here seems like a false positive
69// NOLINTNEXTLINE(cppcoreguidelines-special-member-functions)
71{
72 explicit DebugTimeHelper(std::string_view str)
73 : str_expr(str) {
74 start = std::chrono::steady_clock::now();
75 }
76
78 [[maybe_unused]] const auto& stop = std::chrono::steady_clock::now();
79 LOG_DEBUG("{} took {:%S}s to execute", str_expr, stop - start);
80 }
81
82 std::string_view str_expr;
83 std::chrono::steady_clock::time_point start;
84};
85
86} // namespace detail
87
89inline std::string get_err_msg(int err) {
90 return std::error_code(err, std::generic_category()).message();
91}
92
94inline std::string get_err_msg() {
95 return get_err_msg(errno);
96}
97
98inline void init_loggers() {
99#if defined(DEBUG)
100 spdlog::set_level(spdlog::level::warn);
101#elif defined(RELEASE)
102 spdlog::set_level(spdlog::level::err);
103#else
104 spdlog::set_level(spdlog::level::info);
105#endif
106
107 // Override any previously set log-level with the enviornment variable SPDLOG_LEVEL, if set
108 spdlog::cfg::load_env_levels("LOG_LEVEL");
109
110#if defined(DEBUG) || defined(TRACE)
111 spdlog::set_pattern("[%T.%e] [%^%8l%$] [pid %6P] [%30!!@%20!s:%-4#] %v");
112#else
113 // Pattern:
114 // time - [HH:MM:SS.MS]
115 // level (colored, center aligned) - [ info ]
116 // process id - [pid 12345]
117 // message - "foo bar"
118 spdlog::set_pattern("[%T.%e] [%^%=8l%$] [pid %6P] %v");
119#endif
120
121 // Log to stderr. See https://github.com/gabime/spdlog/wiki/FAQ#switch-the-default-logger-to-stderr
122 spdlog::set_default_logger(spdlog::stderr_color_st("default"));
123}
124
125} // namespace asmgrader
A non-movable and non-copyable type.
Definition class_traits.hpp:38
#define LOG_DEBUG(...)
Definition logging.hpp:41
Definition asm_buffer.hpp:20
void init_loggers()
Definition logging.hpp:98
std::string get_err_msg()
Obtain Linux error (i.e., errno) message via libc functions.
Definition logging.hpp:94
Definition logging.hpp:71
DebugTimeHelper(std::string_view str)
Definition logging.hpp:72
~DebugTimeHelper()
Definition logging.hpp:77
std::chrono::steady_clock::time_point start
Definition logging.hpp:83
std::string_view str_expr
Definition logging.hpp:82