2#ifndef _LOGIT_CONSOLE_LOGGER_HPP_INCLUDED
3#define _LOGIT_CONSOLE_LOGGER_HPP_INCLUDED
11#if defined(__MINGW32__) || defined(_WIN32)
59 std::lock_guard<std::mutex> lock(
m_mutex);
67 std::lock_guard<std::mutex> lock(
m_mutex);
78 void log(
const LogRecord& record,
const std::string& message)
override {
80 std::unique_lock<std::mutex> lock(
m_mutex);
82# if defined(__MINGW32__) || defined(_WIN32)
84 handle_ansi_colors_windows(message);
87 std::cout << message << std::endl;
93 std::lock_guard<std::mutex> lock(
m_mutex);
94# if defined(__MINGW32__) || defined(_WIN32)
96 handle_ansi_colors_windows(message);
99 std::cout << message << std::endl;
118 return std::string();
150 std::unique_lock<std::mutex> lock(
m_mutex);
161# if defined(__MINGW32__) || defined(_WIN32)
164 enum class WinColor {
185 void handle_ansi_colors_windows(
const std::string& message)
const {
186 std::string::size_type start = 0;
187 std::string::size_type pos = 0;
189 HANDLE handle_stdout = GetStdHandle(STD_OUTPUT_HANDLE);
191 while ((pos = message.find(
"\033[", start)) != std::string::npos) {
194 std::cout << message.substr(start, pos - start);
198 std::string::size_type end_pos = message.find(
'm', pos);
199 if (end_pos != std::string::npos) {
201 std::string ansi_code = message.substr(pos + 2, end_pos - pos - 2);
202 apply_color_from_ansi_code(ansi_code, handle_stdout);
212 if (start < message.size()) {
213 std::cout << message.substr(start);
215 if (!message.empty()) std::cout << std::endl;
218 SetConsoleTextAttribute(handle_stdout,
static_cast<WORD
>(text_color_to_win_color(
m_config.
default_color)));
224 void apply_color_from_ansi_code(
const std::string& ansi_code, HANDLE handle_stdout)
const {
226 const int code = std::stoi(ansi_code);
228 case 30: color_value =
static_cast<WORD
>(WinColor::Black);
break;
229 case 31: color_value =
static_cast<WORD
>(WinColor::DarkRed);
break;
230 case 32: color_value =
static_cast<WORD
>(WinColor::DarkGreen);
break;
231 case 33: color_value =
static_cast<WORD
>(WinColor::DarkYellow);
break;
232 case 34: color_value =
static_cast<WORD
>(WinColor::DarkBlue);
break;
233 case 35: color_value =
static_cast<WORD
>(WinColor::DarkMagenta);
break;
234 case 36: color_value =
static_cast<WORD
>(WinColor::DarkCyan);
break;
235 case 37: color_value =
static_cast<WORD
>(WinColor::Gray);
break;
236 case 90: color_value =
static_cast<WORD
>(WinColor::DarkGray);
break;
237 case 91: color_value =
static_cast<WORD
>(WinColor::Red);
break;
238 case 92: color_value =
static_cast<WORD
>(WinColor::Green);
break;
239 case 93: color_value =
static_cast<WORD
>(WinColor::Yellow);
break;
240 case 94: color_value =
static_cast<WORD
>(WinColor::Blue);
break;
241 case 95: color_value =
static_cast<WORD
>(WinColor::Magenta);
break;
242 case 96: color_value =
static_cast<WORD
>(WinColor::Cyan);
break;
243 case 97: color_value =
static_cast<WORD
>(WinColor::White);
break;
249 SetConsoleTextAttribute(handle_stdout, color_value);
255 WinColor text_color_to_win_color(
const TextColor& color)
const {
273 default:
return WinColor::White;
280# if defined(__MINGW32__) || defined(_WIN32)
281 handle_ansi_colors_windows(std::string());
Defines the interface for loggers used in the logging system.
#define LOGIT_DEFAULT_COLOR
Defines the default color for console output. If LOGIT_DEFAULT_COLOR is not defined,...
#define LOGIT_CURRENT_TIMESTAMP_MS()
Macro to get the current timestamp in milliseconds. If LOGIT_CURRENT_TIMESTAMP_MS is not defined,...
Provides various logging macros for different log levels and options.
Defines the TaskExecutor class, which manages task execution in a separate thread.
Logger that outputs log messages to the console with optional color coding.
void log(const LogRecord &record, const std::string &message) override
Logs a message to the console with thread safety.
ConsoleLogger()
Default constructor that uses default configuration.
double get_float_param(const LoggerParam ¶m) const override
Retrieves a floating-point parameter from the logger.
std::mutex m_mutex
Mutex to protect console output.
int64_t get_last_log_ts() const
Retrieves the timestamp of the last log.
virtual ~ConsoleLogger()=default
int64_t get_time_since_last_log() const
Retrieves the time since the last log.
std::string get_string_param(const LoggerParam ¶m) const override
Retrieves a string parameter from the logger.
void set_config(const Config &config)
Sets the logger configuration. This method sets the logger's configuration and ensures thread safety ...
int64_t get_int_param(const LoggerParam ¶m) const override
Retrieves an integer parameter from the logger.
ConsoleLogger(const bool async)
Constructor with asynchronous flag.
Config get_config()
Gets the current logger configuration. Returns the logger's configuration with thread safety ensured.
std::atomic< int64_t > m_last_log_ts
ConsoleLogger(const Config &config)
Constructor with custom configuration.
Config m_config
Configuration for the console logger.
void wait() override
Waits for all asynchronous tasks to complete. If asynchronous logging is enabled, waits for all pendi...
void reset_color()
Resets the console text color to the default.
Interface for loggers that handle log message output.
void add_task(std::function< void()> task)
Adds a task to the queue in a thread-safe manner.
void wait()
Waits for all tasks in the queue to be processed.
static TaskExecutor & get_instance()
Get the singleton instance of the TaskExecutor.
The primary namespace for the LogIt++ library.
std::string to_string(const LogLevel &level, const int &mode=0)
Convert LogLevel to a std::string representation.
LoggerParam
Enumeration for different logger parameters that can be retrieved.
@ TimeSinceLastLog
The time elapsed since the last log in seconds.
@ LastLogTimestamp
The timestamp of the last log.
TextColor
Text colors for console output.
Configuration for the console logger.
bool async
Flag indicating whether logging should be asynchronous.
TextColor default_color
Default text color for console output.
Stores log metadata and content.
const int64_t timestamp_ms
Timestamp in milliseconds.