💿🐜 Antkeeper source code https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

169 lines
5.4 KiB

  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. #ifndef ANTKEEPER_DEBUG_LOG_HPP
  20. #define ANTKEEPER_DEBUG_LOG_HPP
  21. #include <engine/config.hpp>
  22. #include <engine/debug/log/log-message-severity.hpp>
  23. #include <engine/debug/log/logger.hpp>
  24. #include <source_location>
  25. #include <string>
  26. #include <format>
  27. // Enable logging of messages of all severities by default.
  28. #if !defined(ANTKEEPER_DEBUG_LOG_MIN_MESSAGE_SEVERITY)
  29. #define ANTKEEPER_DEBUG_LOG_MIN_MESSAGE_SEVERITY (ANTKEEPER_DEBUG_LOG_MESSAGE_SEVERITY_TRACE)
  30. #endif
  31. namespace debug {
  32. /// @name Debug logging
  33. /// @{
  34. /**
  35. * Returns the default logger.
  36. */
  37. [[nodiscard]] logger& default_logger() noexcept;
  38. /**
  39. * Self-formatting message that logs itself to the default logger on construction.
  40. *
  41. * @tparam Severity Message severity. A message will not log itself if @p Severity is less than the user-defined macro `ANTKEEPER_DEBUG_LOG_MIN_MESSAGE_SEVERITY`.
  42. * @tparam Args Types of arguments to be formatted.
  43. */
  44. template <log_message_severity Severity, class... Args>
  45. struct log_message
  46. {
  47. /**
  48. * Formats and logs a message.
  49. *
  50. * Class template argument deduction (CTAD) is utilized to capture source location as a default argument following variadic format arguments.
  51. *
  52. * @param format Message format string.
  53. * @param args Arguments to be formatted.
  54. * @param location Source location from which the message was sent.
  55. */
  56. log_message
  57. (
  58. [[maybe_unused]] std::string_view format,
  59. [[maybe_unused]] Args&&... args,
  60. [[maybe_unused]] std::source_location&& location = std::source_location::current()
  61. )
  62. {
  63. if constexpr (ANTKEEPER_DEBUG_LOG_MIN_MESSAGE_SEVERITY <= static_cast<std::underlying_type_t<log_message_severity>>(Severity))
  64. {
  65. default_logger().log(std::vformat(format, std::make_format_args(std::forward<Args>(args)...)), Severity, std::forward<std::source_location>(location));
  66. }
  67. }
  68. };
  69. // Use class template argument deduction (CTAD) to capture source location as a default argument following variadic format arguments.
  70. template <log_message_severity Severity, class... Args>
  71. log_message(std::string_view, Args&&...) -> log_message<Severity, Args...>;
  72. #if (ANTKEEPER_DEBUG_LOG_MIN_MESSAGE_SEVERITY <= ANTKEEPER_DEBUG_LOG_MESSAGE_SEVERITY_TRACE)
  73. /**
  74. * Formats and logs a trace message.
  75. *
  76. * @tparam Args Types of arguments to be formatted.
  77. */
  78. template <class... Args>
  79. using log_trace = log_message<log_message_severity::trace, Args...>;
  80. #else
  81. // Disable trace message logging.
  82. template <class... Args>
  83. inline void log_trace([[maybe_unused]] Args&&...) noexcept {};
  84. #endif
  85. #if (ANTKEEPER_DEBUG_LOG_MIN_MESSAGE_SEVERITY <= ANTKEEPER_DEBUG_LOG_MESSAGE_SEVERITY_DEBUG)
  86. /**
  87. * Formats and logs a debug message.
  88. *
  89. * @tparam Args Types of arguments to be formatted.
  90. */
  91. template <class... Args>
  92. using log_debug = log_message<log_message_severity::debug, Args...>;
  93. #else
  94. // Disable debug message logging.
  95. template <class... Args>
  96. inline void log_debug([[maybe_unused]] Args&&...) noexcept {};
  97. #endif
  98. #if (ANTKEEPER_DEBUG_LOG_MIN_MESSAGE_SEVERITY <= ANTKEEPER_DEBUG_LOG_MESSAGE_SEVERITY_INFO)
  99. /**
  100. * Formats and logs an info message.
  101. *
  102. * @tparam Args Types of arguments to be formatted.
  103. */
  104. template <class... Args>
  105. using log_info = log_message<log_message_severity::info, Args...>;
  106. #else
  107. // Disable info message logging.
  108. template <class... Args>
  109. inline void log_info([[maybe_unused]] Args&&...) noexcept {};
  110. #endif
  111. #if (ANTKEEPER_DEBUG_LOG_MIN_MESSAGE_SEVERITY <= ANTKEEPER_DEBUG_LOG_MESSAGE_SEVERITY_WARNING)
  112. /**
  113. * Formats and logs a warning message.
  114. *
  115. * @tparam Args Types of arguments to be formatted.
  116. */
  117. template <class... Args>
  118. using log_warning = log_message<log_message_severity::warning, Args...>;
  119. #else
  120. // Disable warning message logging.
  121. template <class... Args>
  122. inline void log_warning([[maybe_unused]] Args&&...) noexcept {};
  123. #endif
  124. #if (ANTKEEPER_DEBUG_LOG_MIN_MESSAGE_SEVERITY <= ANTKEEPER_DEBUG_LOG_MESSAGE_SEVERITY_ERROR)
  125. /**
  126. * Formats and logs an error message.
  127. *
  128. * @tparam Args Types of arguments to be formatted.
  129. */
  130. template <class... Args>
  131. using log_error = log_message<log_message_severity::error, Args...>;
  132. #else
  133. // Disable error message logging.
  134. template <class... Args>
  135. inline void log_error([[maybe_unused]] Args&&...) noexcept {};
  136. #endif
  137. #if (ANTKEEPER_DEBUG_LOG_MIN_MESSAGE_SEVERITY <= ANTKEEPER_DEBUG_LOG_MESSAGE_SEVERITY_FATAL)
  138. /**
  139. * Formats and logs a fatal error message.
  140. *
  141. * @tparam Args Types of arguments to be formatted.
  142. */
  143. template <class... Args>
  144. using log_fatal = log_message<log_message_severity::fatal, Args...>;
  145. #else
  146. // Disable fatal error message logging.
  147. template <class... Args>
  148. inline void log_fatal([[maybe_unused]] Args&&...) noexcept {};
  149. #endif
  150. /// @}
  151. } // namespace debug
  152. #endif // ANTKEEPER_DEBUG_LOG_HPP