FERS 1.0.0
The Flexible Extensible Radar Simulator
Loading...
Searching...
No Matches
logging.h
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-only
2//
3// Copyright (c) 2024-present FERS Contributors (see AUTHORS.md).
4//
5// See the GNU GPLv2 LICENSE file in the FERS project root for more information.
6
7/**
8 * @file logging.h
9 * @brief Header file for the logging system.
10 *
11 * This file defines a logging system with multiple log levels, supporting thread-safe logging
12 * to a file or standard output, and allowing formatted log messages with source location details.
13 * The logging system is customizable with log levels and file output for effective debugging
14 * and tracking of events in an application.
15 */
16
17#pragma once
18
19#define LOG(level, ...) log(level, std::source_location::current(), __VA_ARGS__)
20
21#include <expected>
22#include <format>
23#include <fstream>
24#include <mutex>
25#include <source_location>
26#include <string>
27#include <utility>
28
29namespace logging
30{
31 /**
32 * @class Level
33 * @brief Enum class representing the log levels.
34 */
35 enum class Level
36 {
37 TRACE, ///< Trace level for detailed debugging information.
38 DEBUG, ///< Debug level for general debugging information.
39 INFO, ///< Info level for informational messages.
40 WARNING, ///< Warning level for potentially harmful situations.
41 ERROR, ///< Error level for error events.
42 FATAL ///< Fatal level for severe error events.
43 };
44
45 /**
46 * @class Logger
47 * @brief Thread-safe logger class for handling logging operations.
48 */
49 class Logger
50 {
51 public:
52 /**
53 * @brief Sets the logging level.
54 *
55 * @param level The logging level to set.
56 */
57 void setLevel(const Level level) noexcept { _log_level = level; }
58
59 /**
60 * @brief Logs a message with a specific log level and source location.
61 *
62 * @param level The log level.
63 * @param message The message to log.
64 * @param location The source location of the log call.
65 */
66 void log(Level level, const std::string& message,
67 const std::source_location& location = std::source_location::current()) noexcept;
68
69 /**
70 * @brief Logs a formatted message with a specific log level and source location.
71 *
72 * @tparam Args Variadic template for format arguments.
73 * @param level The log level.
74 * @param location The source location of the log call.
75 * @param formatStr The format string.
76 * @param args The format arguments.
77 */
78 template <typename... Args>
79 void log(const Level level, const std::source_location& location, const std::string& formatStr,
80 Args&&... args) noexcept
81 {
82 if (level >= _log_level)
83 {
84 const std::string message = std::vformat(formatStr, std::make_format_args(args...));
85 log(level, message, location);
86 }
87 }
88
89 /**
90 * @brief Sets the log file path to log messages to a file.
91 *
92 * @param filePath The path to the log file.
93 * @return std::expected indicating success or error message on failure.
94 */
95 std::expected<void, std::string> logToFile(const std::string& filePath) noexcept;
96
97 private:
98 Level _log_level = Level::INFO; ///< Current log level.
99 std::optional<std::ofstream> _log_file; ///< Output file stream for logging to a file.
100 std::mutex _log_mutex; ///< Mutex for thread-safe logging.
101
102 /**
103 * @brief Gets the current timestamp as a string.
104 *
105 * @return The current timestamp.
106 */
107 static std::string getCurrentTimestamp() noexcept;
108 };
109
110 /**
111 * @brief Externally available logger object.
112 */
113 extern Logger logger;
114
115 /**
116 * @brief Converts a log level enum value to its string representation.
117 *
118 * @param level The log level.
119 * @return A string representing the log level.
120 */
121 inline std::string getLevelString(const Level level) noexcept
122 {
123 switch (level)
124 {
125 case Level::TRACE:
126 return "TRACE";
127 case Level::DEBUG:
128 return "DEBUG";
129 case Level::INFO:
130 return "INFO";
131 case Level::WARNING:
132 return "WARNING";
133 case Level::ERROR:
134 return "ERROR";
135 case Level::FATAL:
136 return "FATAL";
137 default:
138 return "UNKNOWN";
139 }
140 }
141
142 /**
143 * @brief Logs a formatted message with a specific log level and source location.
144 *
145 * @tparam Args Variadic template for format arguments.
146 * @param level The log level.
147 * @param location The source location of the log call.
148 * @param formatStr The format string.
149 * @param args The format arguments.
150 */
151 template <typename... Args>
152 void log(Level level, const std::source_location& location, const std::string& formatStr, Args&&... args) noexcept
153 {
154 logger.log(level, location, formatStr, std::forward<Args>(args)...);
155 }
156}
Enum class representing the log levels.
Thread-safe logger class for handling logging operations.
Definition logging.h:50
void log(Level level, const std::string &message, const std::source_location &location=std::source_location::current()) noexcept
Logs a message with a specific log level and source location.
Definition logging.cpp:37
std::expected< void, std::string > logToFile(const std::string &filePath) noexcept
Sets the log file path to log messages to a file.
Definition logging.cpp:59
void setLevel(const Level level) noexcept
Sets the logging level.
Definition logging.h:57
@ WARNING
Warning level for potentially harmful situations.
@ FATAL
Fatal level for severe error events.
@ TRACE
Trace level for detailed debugging information.
@ INFO
Info level for informational messages.
@ ERROR
Error level for error events.
@ DEBUG
Debug level for general debugging information.
std::string getLevelString(const Level level) noexcept
Converts a log level enum value to its string representation.
Definition logging.h:121
Logger logger
Externally available logger object.
Definition logging.cpp:23