67 if (this->
_file.is_open()) {
74 std::lock_guard lock(this->
_mutex);
75 if (this->
_file.is_open())
77 this->
_file.open(std::string(filename), std::ios::app);
81 const std::source_location &loc)
83 auto now = std::chrono::system_clock::now();
84 auto timePoint = std::chrono::floor<std::chrono::seconds>(
86 auto cleanFileName = std::filesystem::path(
87 loc.file_name()).filename().string();
89 std::string_view color =
"";
106 color =
"\033[1;31m";
113 std::lock_guard lock{this->
_mutex};
115 std::println(std::cout,
"{}[{:%T}] [{}] {} \033[0m "
117 color, timePoint, level, msg,
118 loc.file_name(), loc.line());
119 if (this->
_file.is_open()) {
120 std::println(this->
_file,
"[{:%T}] [{}] {} ({}:{})",
121 timePoint, level, msg,
122 cleanFileName, loc.line());
138 const std::source_location &loc)
143 std::println(std::cerr,
"\033[31m[LOGGER FAILURE] {} \033[0m",
154 -> std::expected<void, rtp::Error>
158 }
catch (
const std::exception &e) {
159 return std::unexpected{
161 "Logger configuration failed: {}", e.what())};
163 return std::unexpected{
165 "Logger configuration failed: unknown error")};
Logger declaration with support for multiple log levels.
static auto failure(ErrorCode code, std::format_string< Args... > fmt, Args &&...args) -> Error
Create a failure-level error.
void write(Level level, std::string_view msg, const std::source_location &loc)
void setOutputFile(std::string_view filename)
LoggerBackend(void) noexcept
~LoggerBackend() noexcept
void logImpl(Level level, std::string_view message, const std::source_location &loc)
static LoggerBackend & getBackend(void) noexcept
Level
Severity levels for log messages.
@ Warning
Warning messages for potentially harmful situations.
@ Info
General informational messages.
@ None
No logging (used to disable logging)
@ Fatal
Fatal error messages indicating critical failures.
@ Error
Error messages for error events.
@ Debug
Detailed information for debugging purposes.
auto configure(std::string_view logFilePath) noexcept -> std::expected< void, rtp::Error >
Configure the logger using a configuration file.
@ InvalidParameter
Invalid function parameter provided.
@ Unknown
Unknown or unspecified error.