FireSTARR
Loading...
Searching...
No Matches
Log.h
1/* Copyright (c) Queen's Printer for Ontario, 2020. */
2/* Copyright (c) His Majesty the King in Right of Canada as represented by the Minister of Natural Resources, 2024-2025. */
3
4/* SPDX-License-Identifier: AGPL-3.0-or-later */
5
6#pragma once
7namespace fs::logging
8{
9static const int LOG_EXTENSIVE = 0;
10static const int LOG_VERBOSE = 1;
11static const int LOG_DEBUG = 2;
12static const int LOG_INFO = 3;
13static const int LOG_NOTE = 4;
14static const int LOG_WARNING = 5;
15static const int LOG_ERROR = 6;
16static const int LOG_FATAL = 7;
17static const int LOG_SILENT = 8;
18
22class Log
23{
27 static int logging_level_;
28public:
34 static void setLogLevel(int log_level) noexcept;
39 static void increaseLogLevel() noexcept;
44 static void decreaseLogLevel() noexcept;
49 static int getLogLevel() noexcept;
54 static int openLogFile(const char* filename) noexcept;
59 static int closeLogFile() noexcept;
60};
61string format_log_message(const char* prefix, const char* format, va_list* args);
69void output(int log_level, const char* format, va_list* args)
70#ifdef NDEBUG
71 noexcept
72#endif
73 ;
81void output(int log_level, const char* format, ...)
82#ifdef NDEBUG
83 noexcept
84#endif
85 ;
91void extensive(const char* format, ...) noexcept;
97void verbose(const char* format, ...) noexcept;
103void debug(const char* format, ...) noexcept;
109void info(const char* format, ...) noexcept;
115void note(const char* format, ...) noexcept;
121void warning(const char* format, ...) noexcept;
127void error(const char* format, ...) noexcept;
134void check_fatal(bool condition, const char* format, ...)
135#ifdef NDEBUG
136 noexcept
137#endif
138 ;
145void check_equal(const MathSize lhs, const MathSize rhs, const char* name)
146#ifdef NDEBUG
147 noexcept
148#endif
149 ;
156void check_equal(const char* lhs, const char* rhs, const char* name)
157#ifdef NDEBUG
158 noexcept
159#endif
160 ;
167template <class V>
168void check_equal(const V& lhs, const V& rhs, const char* name)
169#ifdef NDEBUG
170 noexcept
171#endif
172{
173 logging::check_fatal(lhs != rhs,
174 "Expected %s to be %d but got %d",
175 name,
176 rhs,
177 lhs);
178}
184void fatal(const char* format, ...)
185#ifdef NDEBUG
186 noexcept
187#endif
188 ;
193void fatal(const std::exception& ex);
200void fatal(const std::exception& ex, const char* format, ...);
201// templated so we can return it from any function and not get an error
202// about not returning on all paths
210template <class T>
211T fatal(const char* format, va_list* args)
212#ifdef NDEBUG
213 noexcept
214#endif
215{
216 // format message and then output so we don't parse args twice and can use for error
217 auto msg = format_log_message("", format, args);
218 output(LOG_FATAL, msg.c_str());
220#ifdef NDEBUG
221 exit(EXIT_FAILURE);
222#else
223 // HACK: just throw the format for a start - just want to see stack traces when debugging
224 throw std::runtime_error(msg);
225#endif
226}
234template <class T>
235T fatal(const char* format, ...)
236#ifdef NDEBUG
237 noexcept
238#endif
239{
240 va_list args;
241 va_start(args, format);
242 // cppcheck-suppress va_end_missing
243 return fatal<T>(format, &args);
244 // va_end(args);
245}
247{
248protected:
249 virtual string add_log(const char* format) const noexcept = 0;
250 void log_output(const int level, const char* format, ...) const noexcept;
251 void log_extensive(const char* format, ...) const noexcept;
252 void log_verbose(const char* format, ...) const noexcept;
253 void log_debug(const char* format, ...) const noexcept;
254 void log_info(const char* format, ...) const noexcept;
255 void log_note(const char* format, ...) const noexcept;
256 void log_warning(const char* format, ...) const noexcept;
257 void log_error(const char* format, ...) const noexcept;
258 void log_check_fatal(bool condition, const char* format, ...) const
259#ifdef NDEBUG
260 noexcept
261#endif
262 ;
263 void log_fatal(const char* format, ...) const
264#ifdef NDEBUG
265 noexcept
266#endif
267 ;
268};
269}
Provides logging functionality.
Definition Log.h:23
static int logging_level_
Current logging level.
Definition Log.h:27
static int closeLogFile() noexcept
Set output log file.
Definition Log.cpp:54
static int getLogLevel() noexcept
Get current logging level.
Definition Log.cpp:38
static void increaseLogLevel() noexcept
Increase amount of logging output by one level.
Definition Log.cpp:28
static void setLogLevel(int log_level) noexcept
Set logging level to a specific level.
Definition Log.cpp:24
static int openLogFile(const char *filename) noexcept
Set output log file.
Definition Log.cpp:43
static void decreaseLogLevel() noexcept
Decrease amount of logging output by one level.
Definition Log.cpp:33
Definition Log.h:247