FireSTARR
Loading...
Searching...
No Matches
Log.h
1/* SPDX-License-Identifier: AGPL-3.0-or-later */
2#ifndef FS_LOG_H
3#define FS_LOG_H
4#include "stdafx.h"
5#include <cstdlib>
6#include <exception>
7#include <format>
8#ifdef NDEBUG
9#define DEBUG_NOEXCEPT_OFF noexcept
10#else
11#define DEBUG_NOEXCEPT_OFF
12#endif
13namespace fs::logging
14{
15enum class level
16{
17 silent = 0,
18 fatal = 1,
19 error = 2,
20 warning = 3,
21 note = 4,
22 info = 5,
23 debug = 6,
24 verbose = 7,
25 extensive = 8,
26 min = silent,
27 max = extensive
28};
29void set_log_level(const logging::level log_level) noexcept;
30void increase_log_level() noexcept;
31void decrease_log_level() noexcept;
32logging::level get_log_level() noexcept;
33inline bool should_log(const logging::level log_level) noexcept
34{
35 return get_log_level() >= log_level;
36}
37inline bool should_not_log(const logging::level log_level) noexcept
38{
39 return get_log_level() < log_level;
40}
41int open_log_file(const char* filename) noexcept;
42int close_log_file() noexcept;
43// use [[nodiscard]] so we notice if output is used directly somewhere
44// since we're not checking log level at this point the message can be resolved to a string
45[[nodiscard]] string output_no_check(logging::level log_level, const string msg) DEBUG_NOEXCEPT_OFF;
46template <typename... Args>
47[[nodiscard]] inline string output_no_check(
48 logging::level log_level,
49 std::format_string<Args...> format,
50 Args&&... args
51) DEBUG_NOEXCEPT_OFF
52{
53 return output_no_check(log_level, std::format(format, std::forward<Args>(args)...));
54}
55template <typename... Args>
56[[nodiscard]] inline string output(
57 logging::level log_level,
58 std::format_string<Args...> format,
59 Args&&... args
60) DEBUG_NOEXCEPT_OFF
61{
62 if (should_not_log(log_level))
63 {
64 return "";
65 }
66 return output_no_check(log_level, format, std::forward<Args>(args)...);
67}
68template <typename... Args>
69inline void extensive(std::format_string<Args...> format, Args&&... args) DEBUG_NOEXCEPT_OFF
70{
71 std::ignore = output(logging::level::extensive, format, std::forward<Args>(args)...);
72}
73template <typename... Args>
74inline void verbose(std::format_string<Args...> format, Args&&... args) DEBUG_NOEXCEPT_OFF
75{
76 std::ignore = output(logging::level::verbose, format, std::forward<Args>(args)...);
77}
78template <typename... Args>
79inline void debug(std::format_string<Args...> format, Args&&... args) DEBUG_NOEXCEPT_OFF
80{
81 std::ignore = output(logging::level::debug, format, std::forward<Args>(args)...);
82}
83template <typename... Args>
84inline void info(std::format_string<Args...> format, Args&&... args) DEBUG_NOEXCEPT_OFF
85{
86 std::ignore = output(logging::level::info, format, std::forward<Args>(args)...);
87}
88template <typename... Args>
89inline void note(std::format_string<Args...> format, Args&&... args) DEBUG_NOEXCEPT_OFF
90{
91 std::ignore = output(logging::level::note, format, std::forward<Args>(args)...);
92}
93template <typename... Args>
94inline void warning(std::format_string<Args...> format, Args&&... args) DEBUG_NOEXCEPT_OFF
95{
96 std::ignore = output(logging::level::warning, format, std::forward<Args>(args)...);
97}
98template <typename... Args>
99inline void error(std::format_string<Args...> format, Args&&... args) DEBUG_NOEXCEPT_OFF
100{
101 std::ignore = output(logging::level::error, format, std::forward<Args>(args)...);
102}
103template <typename... Args>
104inline int fatal(std::format_string<Args...> format, Args&&... args) DEBUG_NOEXCEPT_OFF
105{
106 auto msg = output(logging::level::fatal, format, std::forward<Args>(args)...);
107#ifndef NDEBUG
108 // HACK: just throw the format for a start - just want to see stack traces when debugging
109 throw std::runtime_error(msg);
110#else
111 exit(EXIT_FAILURE);
112 return EXIT_FAILURE;
113#endif
114}
115inline int fatal(const std::exception& ex) DEBUG_NOEXCEPT_OFF
116{
117 std::ignore = output(logging::level::fatal, "{:s}", ex.what());
118#ifndef NDEBUG
119 throw ex;
120#else
121 exit(EXIT_FAILURE);
122 // HACK: to satisfy return type
123 return EXIT_FAILURE;
124#endif
125}
126template <typename... Args>
127inline int fatal(const std::exception& ex, std::format_string<Args...> format, Args&&... args)
128 DEBUG_NOEXCEPT_OFF
129{
130 std::ignore = output(logging::level::fatal, format, std::forward<Args>(args)...);
131 return fatal(ex);
132}
133template <typename... Args>
134inline constexpr void check_fatal(
135 bool condition,
136 std::format_string<Args...> format,
137 Args&&... args
138) DEBUG_NOEXCEPT_OFF
139{
140 if (!condition)
141 {
142 return;
143 }
144 fatal(format, std::forward<Args>(args)...);
145}
146template <typename T>
147inline void check_equal(const T lhs, const T rhs, const char* name) DEBUG_NOEXCEPT_OFF
148{
149 check_fatal(lhs != rhs, "Expected {:s} to be {} but got {}", name, rhs, lhs);
150}
151inline void check_equal(const char* lhs, const char* rhs, const char* name) DEBUG_NOEXCEPT_OFF
152{
153 check_fatal(0 != strcmp(lhs, rhs), "Expected {:s} to be {:s} got {:s}", name, rhs, lhs);
154}
155inline void check_equal(const bool lhs, const bool rhs, const char* name) DEBUG_NOEXCEPT_OFF
156{
157 check_fatal(
158 lhs != rhs,
159 "Expected {:s} to be {:s} but got {:s}",
160 name,
161 lhs ? "true" : "false",
162 rhs ? "true" : "false"
163 );
164}
165template <class V>
166inline void check_equal_verbose(
167 const logging::level log_level,
168 const V& lhs,
169 const V& rhs,
170 const char* name
171) DEBUG_NOEXCEPT_OFF
172{
173 check_equal(lhs, rhs, name);
174 std::ignore = output(log_level, "{:s} matches", name);
175}
176inline void check_tolerance(
177 const MathSize epsilon,
178 const MathSize lhs,
179 const MathSize rhs,
180 const char* name
181) DEBUG_NOEXCEPT_OFF
182{
183 const auto difference = abs(lhs - rhs);
184 check_fatal(
185 difference >= epsilon,
186 "Difference too big for {:s}: ({:g} > {:g}) for {:g} vs {:g}",
187 name,
188 difference,
189 epsilon,
190 rhs,
191 lhs
192 );
193}
194}
195#endif