FERS 1.0.0
The Flexible Extensible Radar Simulator
Loading...
Searching...
No Matches
logging::Logger Class Reference

Thread-safe logger class for handling logging operations. More...

#include "logging.h"

Public Types

using Callback = void(*)(Level level, const std::string &line, void *user_data)
 

Public Member Functions

void setLevel (Level level) noexcept
 Sets the logging level.
 
Level getLevel () const noexcept
 Gets the current logging level.
 
void setCallback (Callback callback, void *user_data) noexcept
 Sets an optional callback that receives each formatted log line.
 
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.
 
template<typename... Args>
void log (const Level level, const std::source_location &location, const std::string &formatStr, Args &&... args) noexcept
 Logs a formatted message with a specific log level and source location.
 
std::expected< void, std::string > logToFile (const std::string &filePath) noexcept
 Sets the log file path to log messages to a file.
 

Detailed Description

Thread-safe logger class for handling logging operations.

Definition at line 52 of file logging.h.

Member Typedef Documentation

◆ Callback

using logging::Logger::Callback = void (*)(Level level, const std::string& line, void* user_data)

Definition at line 55 of file logging.h.

Member Function Documentation

◆ getLevel()

Level logging::Logger::getLevel ( ) const
noexcept

Gets the current logging level.

Returns
The current minimum logging level.

Definition at line 27 of file logging.cpp.

27{ return _log_level.load(std::memory_order_relaxed); }

Referenced by log().

+ Here is the caller graph for this function:

◆ log() [1/2]

template<typename... Args>
void logging::Logger::log ( const Level  level,
const std::source_location &  location,
const std::string &  formatStr,
Args &&...  args 
)
noexcept

Logs a formatted message with a specific log level and source location.

Template Parameters
ArgsVariadic template for format arguments.
Parameters
levelThe log level.
locationThe source location of the log call.
formatStrThe format string.
argsThe format arguments.

Definition at line 99 of file logging.h.

101 {
102 if (level != Level::OFF && level >= getLevel())
103 {
104 const std::string message = std::vformat(formatStr, std::make_format_args(args...));
105 log(level, message, location);
106 }
107 }
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:45
Level getLevel() const noexcept
Gets the current logging level.
Definition logging.cpp:27
@ OFF
Special level to disable all logging.

References getLevel(), log(), and logging::OFF.

+ Here is the call graph for this function:

◆ log() [2/2]

void logging::Logger::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.

Parameters
levelThe log level.
messageThe message to log.
locationThe source location of the log call.

Definition at line 45 of file logging.cpp.

46 {
47 if (level != Level::OFF && level >= getLevel())
48 {
49 Callback callback = nullptr;
50 void* callback_user_data = nullptr;
51 std::string line;
52
53 {
54 std::scoped_lock lock(_log_mutex);
55
56 const std::string filename = std::filesystem::path(location.file_name()).filename().string();
57 const std::string file_line = filename + ":" + std::to_string(location.line());
58
59 std::ostringstream oss;
60 oss << "[" << getCurrentTimestamp() << "] " << "[" << std::setw(7) << std::left << getLevelString(level)
61 << "] " << "[" << std::setw(30) << std::left << file_line << "] " << message;
62 line = oss.str();
63
64 std::cerr << line << '\n';
65
66 if (_log_file && _log_file->is_open())
67 {
68 *_log_file << line << '\n';
69 _log_file->flush();
70 }
71
72 callback = _callback;
73 callback_user_data = _callback_user_data;
74 }
75
76 if (callback != nullptr)
77 {
78 callback(level, line, callback_user_data);
79 }
80 }
81 }
void(*)(Level level, const std::string &line, void *user_data) Callback
Definition logging.h:55
std::string getLevelString(const Level level) noexcept
Converts a log level enum value to its string representation.
Definition logging.h:143

References logging::getLevelString(), and logging::OFF.

Referenced by fers_log(), log(), and logging::log().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ logToFile()

std::expected< void, std::string > logging::Logger::logToFile ( const std::string &  filePath)
noexcept

Sets the log file path to log messages to a file.

Parameters
filePathThe path to the log file.
Returns
std::expected indicating success or error message on failure.

Definition at line 90 of file logging.cpp.

91 {
92 std::scoped_lock lock(_log_mutex);
93
94 std::ofstream file(filePath, std::ios::out | std::ios::trunc);
95 if (!file)
96 {
97 return std::unexpected("Unable to open log file: " + filePath);
98 }
99
100 _log_file = std::move(file);
101 return {};
102 }

Referenced by fers_configure_logging().

+ Here is the caller graph for this function:

◆ setCallback()

void logging::Logger::setCallback ( Callback  callback,
void *  user_data 
)
noexcept

Sets an optional callback that receives each formatted log line.

Parameters
callbackCallback to invoke after a log line is accepted, or nullptr to disable.
user_dataOpaque caller data passed back to the callback.

Definition at line 83 of file logging.cpp.

84 {
85 std::scoped_lock lock(_log_mutex);
86 _callback = callback;
87 _callback_user_data = user_data;
88 }

Referenced by fers_set_log_callback().

+ Here is the caller graph for this function:

◆ setLevel()

void logging::Logger::setLevel ( Level  level)
noexcept

Sets the logging level.

Parameters
levelThe logging level to set.

Definition at line 25 of file logging.cpp.

25{ _log_level.store(level, std::memory_order_relaxed); }

Referenced by fers_configure_logging().

+ Here is the caller graph for this function:

The documentation for this class was generated from the following files: