Antkeeper  0.0.1
log.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2023 Christopher J. Howard
3  *
4  * This file is part of Antkeeper source code.
5  *
6  * Antkeeper source code is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * Antkeeper source code is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef ANTKEEPER_DEBUG_LOG_HPP
21 #define ANTKEEPER_DEBUG_LOG_HPP
22 
23 #include <engine/config.hpp>
26 #include <source_location>
27 #include <string>
28 #include <format>
29 
30 // Enable logging of messages of all severities by default.
31 #if !defined(ANTKEEPER_DEBUG_LOG_MIN_MESSAGE_SEVERITY)
32  #define ANTKEEPER_DEBUG_LOG_MIN_MESSAGE_SEVERITY (ANTKEEPER_DEBUG_LOG_MESSAGE_SEVERITY_TRACE)
33 #endif
34 
35 namespace debug {
36 
39 
43 [[nodiscard]] logger& default_logger() noexcept;
44 
51 template <log_message_severity Severity, class... Args>
53 {
64  (
65  [[maybe_unused]] std::string_view format,
66  [[maybe_unused]] Args&&... args,
67  [[maybe_unused]] std::source_location&& location = std::source_location::current()
68  )
69  {
70  if constexpr (ANTKEEPER_DEBUG_LOG_MIN_MESSAGE_SEVERITY <= static_cast<std::underlying_type_t<log_message_severity>>(Severity))
71  {
72  default_logger().log(std::vformat(format, std::make_format_args(std::forward<Args>(args)...)), Severity, std::forward<std::source_location>(location));
73  }
74  }
75 };
76 
77 // Use class template argument deduction (CTAD) to capture source location as a default argument following variadic format arguments.
78 template <log_message_severity Severity, class... Args>
79 log_message(std::string_view, Args&&...) -> log_message<Severity, Args...>;
80 
81 #if (ANTKEEPER_DEBUG_LOG_MIN_MESSAGE_SEVERITY <= ANTKEEPER_DEBUG_LOG_MESSAGE_SEVERITY_TRACE)
87  template <class... Args>
89 #else
90  // Disable trace message logging.
91  template <class... Args>
92  inline void log_trace([[maybe_unused]] Args&&...) noexcept {};
93 #endif
94 
95 #if (ANTKEEPER_DEBUG_LOG_MIN_MESSAGE_SEVERITY <= ANTKEEPER_DEBUG_LOG_MESSAGE_SEVERITY_DEBUG)
101  template <class... Args>
103 #else
104  // Disable debug message logging.
105  template <class... Args>
106  inline void log_debug([[maybe_unused]] Args&&...) noexcept {};
107 #endif
108 
109 #if (ANTKEEPER_DEBUG_LOG_MIN_MESSAGE_SEVERITY <= ANTKEEPER_DEBUG_LOG_MESSAGE_SEVERITY_INFO)
115  template <class... Args>
117 #else
118  // Disable info message logging.
119  template <class... Args>
120  inline void log_info([[maybe_unused]] Args&&...) noexcept {};
121 #endif
122 
123 #if (ANTKEEPER_DEBUG_LOG_MIN_MESSAGE_SEVERITY <= ANTKEEPER_DEBUG_LOG_MESSAGE_SEVERITY_WARNING)
129  template <class... Args>
131 #else
132  // Disable warning message logging.
133  template <class... Args>
134  inline void log_warning([[maybe_unused]] Args&&...) noexcept {};
135 #endif
136 
137 #if (ANTKEEPER_DEBUG_LOG_MIN_MESSAGE_SEVERITY <= ANTKEEPER_DEBUG_LOG_MESSAGE_SEVERITY_ERROR)
143  template <class... Args>
145 #else
146  // Disable error message logging.
147  template <class... Args>
148  inline void log_error([[maybe_unused]] Args&&...) noexcept {};
149 #endif
150 
151 #if (ANTKEEPER_DEBUG_LOG_MIN_MESSAGE_SEVERITY <= ANTKEEPER_DEBUG_LOG_MESSAGE_SEVERITY_FATAL)
157  template <class... Args>
159 #else
160  // Disable fatal error message logging.
161  template <class... Args>
162  inline void log_fatal([[maybe_unused]] Args&&...) noexcept {};
163 #endif
164 
166 
167 } // namespace debug
168 
169 #endif // ANTKEEPER_DEBUG_LOG_HPP
void log(std::string &&message, log_message_severity severity=log_message_severity::info, std::source_location &&location=std::source_location::current())
Logs a message.
Definition: logger.cpp:26
#define ANTKEEPER_DEBUG_LOG_MIN_MESSAGE_SEVERITY
Definition: log.hpp:32
Debugging functions and classes.
Definition: cli.cpp:22
log_message< log_message_severity::fatal, Args... > log_fatal
Formats and logs a fatal error message.
Definition: log.hpp:158
log_message< log_message_severity::warning, Args... > log_warning
Formats and logs a warning message.
Definition: log.hpp:130
log_message< log_message_severity::trace, Args... > log_trace
Formats and logs a trace message.
Definition: log.hpp:88
log_message< log_message_severity::debug, Args... > log_debug
Formats and logs a debug message.
Definition: log.hpp:102
log_message_severity
Log message severity levels.
@ trace
Trace message severity.
@ warning
Warning message severity.
@ debug
Debug message severity.
@ info
Info message severity.
@ error
Error message severity.
@ fatal
Fatal error message severity.
log_message(std::string_view, Args &&...) -> log_message< Severity, Args... >
Formats and logs a trace message.
log_message< log_message_severity::error, Args... > log_error
Formats and logs an error message.
Definition: log.hpp:144
logger & default_logger() noexcept
Returns the default logger.
Definition: log.cpp:24
log_message< log_message_severity::info, Args... > log_info
Formats and logs an info message.
Definition: log.hpp:116
format
Image and vertex formats.
Definition: format.hpp:29
Self-formatting message that logs itself to the default logger on construction.
Definition: log.hpp:53
log_message([[maybe_unused]] std::string_view format, [[maybe_unused]] Args &&... args, [[maybe_unused]] std::source_location &&location=std::source_location::current())
Formats and logs a message.
Definition: log.hpp:64