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 Member Functions

void setLevel (const Level level) noexcept
 Sets the logging level.
 
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 49 of file logging.h.

Member Function Documentation

◆ 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 79 of file logging.h.

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 }
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

References log().

+ 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 37 of file logging.cpp.

38 {
39 if (level >= _log_level)
40 {
41 std::scoped_lock lock(_log_mutex);
42
43 const std::string filename = std::filesystem::path(location.file_name()).filename().string();
44 const std::string file_line = filename + ":" + std::to_string(location.line());
45
46 std::ostringstream oss;
47 oss << "[" << getCurrentTimestamp() << "] " << "[" << std::setw(7) << std::left << getLevelString(level)
48 << "] " << "[" << std::setw(30) << std::left << file_line << "] " << message << std::endl;
49
50 std::cerr << oss.str();
51
52 if (_log_file && _log_file->is_open())
53 {
54 *_log_file << oss.str();
55 }
56 }
57 }
std::string getLevelString(const Level level) noexcept
Converts a log level enum value to its string representation.
Definition logging.h:121

References logging::getLevelString().

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 59 of file logging.cpp.

60 {
61 std::scoped_lock lock(_log_mutex);
62
63 std::ofstream file(filePath, std::ios::out | std::ios::trunc);
64 if (!file)
65 {
66 return std::unexpected("Unable to open log file: " + filePath);
67 }
68
69 _log_file = std::move(file);
70 return {};
71 }

Referenced by fers_configure_logging().

+ Here is the caller graph for this function:

◆ setLevel()

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

Sets the logging level.

Parameters
levelThe logging level to set.

Definition at line 57 of file logging.h.

57{ _log_level = level; }

Referenced by fers_configure_logging().

+ Here is the caller graph for this function:

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