2#ifndef _LOGIT_CONSOLE_LOGGER_HPP_INCLUDED
3#define _LOGIT_CONSOLE_LOGGER_HPP_INCLUDED
11#if defined(__MINGW32__) || defined(_WIN32)
64 std::lock_guard<std::mutex> lock(
m_mutex);
72 std::lock_guard<std::mutex> lock(
m_mutex);
83 void log(
const LogRecord& record,
const std::string& message)
override {
85 std::unique_lock<std::mutex> lock(
m_mutex);
87# if defined(__MINGW32__) || defined(_WIN32)
89 handle_ansi_colors_windows(message);
92 std::cout << message << std::endl;
98 std::lock_guard<std::mutex> lock(
m_mutex);
99# if defined(__MINGW32__) || defined(_WIN32)
101 handle_ansi_colors_windows(message);
104 std::cout << message << std::endl;
123 return std::string();
155 std::unique_lock<std::mutex> lock(
m_mutex);
166# if defined(__MINGW32__) || defined(_WIN32)
169 enum class WinColor {
190 void handle_ansi_colors_windows(
const std::string& message)
const {
191 std::string::size_type start = 0;
192 std::string::size_type pos = 0;
194 HANDLE handle_stdout = GetStdHandle(STD_OUTPUT_HANDLE);
196 while ((pos = message.find(
"\033[", start)) != std::string::npos) {
199 std::cout << message.substr(start, pos - start);
203 std::string::size_type end_pos = message.find(
'm', pos);
204 if (end_pos != std::string::npos) {
206 std::string ansi_code = message.substr(pos + 2, end_pos - pos - 2);
207 apply_color_from_ansi_code(ansi_code, handle_stdout);
217 if (start < message.size()) {
218 std::cout << message.substr(start);
220 if (!message.empty()) std::cout << std::endl;
223 SetConsoleTextAttribute(handle_stdout,
static_cast<WORD
>(text_color_to_win_color(
m_config.
default_color)));
229 void apply_color_from_ansi_code(
const std::string& ansi_code, HANDLE handle_stdout)
const {
231 const int code = std::stoi(ansi_code);
233 case 30: color_value =
static_cast<WORD
>(WinColor::Black);
break;
234 case 31: color_value =
static_cast<WORD
>(WinColor::DarkRed);
break;
235 case 32: color_value =
static_cast<WORD
>(WinColor::DarkGreen);
break;
236 case 33: color_value =
static_cast<WORD
>(WinColor::DarkYellow);
break;
237 case 34: color_value =
static_cast<WORD
>(WinColor::DarkBlue);
break;
238 case 35: color_value =
static_cast<WORD
>(WinColor::DarkMagenta);
break;
239 case 36: color_value =
static_cast<WORD
>(WinColor::DarkCyan);
break;
240 case 37: color_value =
static_cast<WORD
>(WinColor::Gray);
break;
241 case 90: color_value =
static_cast<WORD
>(WinColor::DarkGray);
break;
242 case 91: color_value =
static_cast<WORD
>(WinColor::Red);
break;
243 case 92: color_value =
static_cast<WORD
>(WinColor::Green);
break;
244 case 93: color_value =
static_cast<WORD
>(WinColor::Yellow);
break;
245 case 94: color_value =
static_cast<WORD
>(WinColor::Blue);
break;
246 case 95: color_value =
static_cast<WORD
>(WinColor::Magenta);
break;
247 case 96: color_value =
static_cast<WORD
>(WinColor::Cyan);
break;
248 case 97: color_value =
static_cast<WORD
>(WinColor::White);
break;
254 SetConsoleTextAttribute(handle_stdout, color_value);
260 WinColor text_color_to_win_color(
const TextColor& color)
const {
278 default:
return WinColor::White;
285# if defined(__MINGW32__) || defined(_WIN32)
286 handle_ansi_colors_windows(std::string());
Defines the interface for loggers used in the logging system.
Provides various logging macros for different log levels and options.
Defines the TaskExecutor class, which manages task execution in a separate thread.
Outputs log messages to the console with optional ANSI color support.
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.
#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,...
The primary namespace for the LogIt++ library.
std::string to_string(LogLevel level, 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.