Helios Engine
A modular ECS based data-oriented C++23 game engine framework
Loading...
Searching...
No Matches
config.hpp
Go to the documentation of this file.
1#pragma once
2
4
5#include <concepts>
6#include <cstddef>
7#include <cstdint>
8#include <string_view>
9#include <type_traits>
10
11namespace helios::log {
12
13/// @brief Log severity levels.
14enum class Level : uint8_t {
15 kTrace = 0,
16 kDebug = 1,
17 kInfo = 2,
18 kWarn = 3,
19 kError = 4,
21};
22
23/// @brief Configuration for logger behavior and output.
24struct Config {
25 std::string_view log_directory = "logs"; ///< Log output directory path.
26 std::string_view file_name_pattern =
27 "{name}_{timestamp}.log"; ///< Pattern for log file names (supports
28 ///< format placeholders: {name}, {timestamp}).
29 std::string_view console_pattern =
30 "[%H:%M:%S.%e] [%t] [%^%l%$] %n: %v"; ///< Console log pattern.
31 std::string_view file_pattern =
32 "[%Y-%m-%d %H:%M:%S.%e] [%t] [%l] %n: %v"; ///< File log pattern.
33
35 0; ///< Maximum size of a single log file in bytes (0 = no limit).
36 size_t max_files =
37 0; ///< Maximum number of log files to keep (0 = no limit).
39 Level::kWarn; ///< Minimum log level to flush automatically.
40
41 bool enable_console = true; ///< Enable console output.
42 bool enable_file = true; ///< Enable file output.
43 bool truncate_files = true; ///< Enable truncation of existing log files.
44 bool async_logging = false; ///< Enable async logging (better performance but
45 ///< may lose last logs on crash).
47 8192; ///< Queue size for async logging (only used when async_logging).
49 1; ///< Background thread count for async logging.
50
51 /**
52 * @brief Creates default configuration.
53 * @return Default `Config` instance
54 */
55 [[nodiscard]] static constexpr Config Default() noexcept { return {}; }
56
57 /**
58 * @brief Creates configuration for console-only output.
59 * @return `Config` instance for console-only logging
60 */
61 [[nodiscard]] static constexpr Config ConsoleOnly() noexcept {
62 return {.enable_console = true, .enable_file = false};
63 }
64
65 /**
66 * @brief Creates configuration for file-only output.
67 * @return `Config` instance for file-only logging
68 */
69 [[nodiscard]] static constexpr Config FileOnly() noexcept {
70 return {.enable_console = false, .enable_file = true};
71 }
72
73 /**
74 * @brief Creates configuration optimized for debug builds.
75 * @return `Config` instance for debug logging
76 */
77 [[nodiscard]] static constexpr Config Debug() noexcept {
78 return {.auto_flush_level = Level::kTrace,
79 .enable_console = true,
80 .enable_file = true,
81 .async_logging = false};
82 }
83
84 /**
85 * @brief Creates configuration optimized for release builds.
86 * @return `Config` instance for release logging
87 */
88 [[nodiscard]] static constexpr Config Release() noexcept {
89 return {
90 .enable_console = false, .enable_file = true, .async_logging = true};
91 }
92};
93
94/// @brief Type alias for logger type IDs.
96
97/// @brief Type alias for logger type indices.
99
100/**
101 * @brief Trait to identify valid logger types.
102 * @details A logger type must be an object, be empty and provide:
103 * - `static constexpr std::string_view kName` variable
104 * @tparam T Type to check
105 *
106 * @code
107 * struct MyLogger {
108 * static constexpr std::string_view kName = return "MyLogger";
109 * };
110 *
111 * static_assert(LoggerTrait<MyLogger>);
112 * @endcode
113 */
114template <typename T>
115concept LoggerTrait = std::is_object_v<std::remove_cvref_t<T>> &&
116 std::is_empty_v<std::remove_cvref_t<T>> && requires {
117 {
118 std::remove_cvref_t<T>::kName
119 } -> std::convertible_to<std::string_view>;
120 };
121
122/**
123 * @brief Trait to identify loggers with custom configuration.
124 * @details A logger with config trait must satisfy `LoggerTrait` and provide:
125 * - `static Config kConfig` variable
126 * @tparam T Type to check
127 *
128 * @code
129 * struct MyLogger {
130 * static constexpr std::string_view kName = "MyLogger";
131 * static constexpr auto kConfig = Config::Default();
132 * };
133 *
134 * static_assert(LoggerWithConfigTrait<MyLogger>);
135 * @endcode
136 */
137template <typename T>
139 { std::remove_cvref_t<T>::kConfig } -> std::convertible_to<Config>;
140};
141
142/**
143 * @brief Gets the name of a logger type.
144 * @details Returns the name provided by `kName` variable.
145 * @tparam T Logger type
146 * @param logger logger instance
147 * @return Name of the logger
148 *
149 * @code
150 * struct MyLogger {
151 * static constexpr std::string_view kName = "MyLogger";
152 * };
153 *
154 * constexpr auto name = LoggerNameOf<MyLogger>(); // "MyLogger"
155 * @endcode
156 */
157template <LoggerTrait T>
158[[nodiscard]] constexpr std::string_view LoggerNameOf(
159 T /*logger*/ = {}) noexcept {
160 return std::remove_cvref_t<T>::kName;
161}
162
163/**
164 * @brief Gets the configuration for a logger type.
165 * @details Returns the config provided by `kConfig` variable if available,
166 * otherwise returns the default configuration.
167 * @tparam T Logger type
168 * @param logger logger instance
169 * @return `Config` for the logger
170 *
171 * @code
172 * struct MyLogger {
173 * static constexpr std::string_view kName = "MyLogger";
174 * static constexpr auto kConfig = Config::ConsoleOnly();
175 * };
176 *
177 * constexpr auto config = LoggerConfigOf<MyLogger>();
178 * @endcode
179 */
180template <LoggerTrait T>
181[[nodiscard]] constexpr Config LoggerConfigOf(T /*logger*/ = {}) noexcept {
182 if constexpr (LoggerWithConfigTrait<T>) {
183 return std::remove_cvref_t<T>::kConfig;
184 } else {
185 return Config::Default();
186 }
187}
188
189} // namespace helios::log
Trait to identify valid logger types.
Definition config.hpp:115
Trait to identify loggers with custom configuration.
Definition config.hpp:138
utils::TypeId LoggerTypeId
Type alias for logger type IDs.
Definition config.hpp:95
utils::TypeIndex LoggerTypeIndex
Type alias for logger type indices.
Definition config.hpp:98
constexpr Config LoggerConfigOf(T={}) noexcept
Gets the configuration for a logger type.
Definition config.hpp:181
constexpr std::string_view LoggerNameOf(T={}) noexcept
Gets the name of a logger type.
Definition config.hpp:158
Level
Log severity levels.
Definition config.hpp:14
Configuration for logger behavior and output.
Definition config.hpp:24
bool enable_file
Enable file output.
Definition config.hpp:42
bool truncate_files
Enable truncation of existing log files.
Definition config.hpp:43
static constexpr Config ConsoleOnly() noexcept
Creates configuration for console-only output.
Definition config.hpp:61
std::string_view file_pattern
File log pattern.
Definition config.hpp:31
std::string_view console_pattern
Console log pattern.
Definition config.hpp:29
static constexpr Config Debug() noexcept
Creates configuration optimized for debug builds.
Definition config.hpp:77
std::string_view file_name_pattern
Definition config.hpp:26
size_t max_file_size
Maximum size of a single log file in bytes (0 = no limit).
Definition config.hpp:34
size_t async_queue_size
Queue size for async logging (only used when async_logging).
Definition config.hpp:46
static constexpr Config Default() noexcept
Creates default configuration.
Definition config.hpp:55
size_t async_thread_count
Background thread count for async logging.
Definition config.hpp:48
static constexpr Config FileOnly() noexcept
Creates configuration for file-only output.
Definition config.hpp:69
static constexpr Config Release() noexcept
Creates configuration optimized for release builds.
Definition config.hpp:88
std::string_view log_directory
Log output directory path.
Definition config.hpp:25
bool enable_console
Enable console output.
Definition config.hpp:41
Level auto_flush_level
Minimum log level to flush automatically.
Definition config.hpp:38
size_t max_files
Maximum number of log files to keep (0 = no limit).
Definition config.hpp:36