Load Balancer Simulation
A simulation of a load balancer with dynamic server scaling in C++
Loading...
Searching...
No Matches
Logger.cpp
Go to the documentation of this file.
1
5
6#include "Logger.h"
7#include <iostream>
8#include <regex>
9
10std::ofstream Logger::logFile_;
11
13bool Logger::init(const std::string& filename) {
14 if (logFile_.is_open()) {
15 logFile_.close();
16 }
17 logFile_.open(filename);
18 return logFile_.is_open();
19}
20
23 if (logFile_.is_open()) {
24 logFile_.close();
25 }
26}
27
29std::string Logger::stripAnsi(const std::string& str) {
30 /* Remove ANSI escape sequences (\033[...m or \x1b[...m) */
31 static const std::regex ansiRegex("\033\\[[0-9;]*m");
32 return std::regex_replace(str, ansiRegex, "");
33}
34
36void Logger::log(const std::string& message) {
37 std::cout << message;
38 if (logFile_.is_open()) {
39 logFile_ << stripAnsi(message);
40 }
41}
Defines the Logger class for console and file output.
static std::string stripAnsi(const std::string &str)
Remove ANSI escape sequences from a string.
Definition Logger.cpp:29
static std::ofstream logFile_
Definition Logger.h:48
static bool init(const std::string &filename)
Initialize the logger with an output file.
Definition Logger.cpp:13
static void log(const std::string &message)
Write a message to both console and log file.
Definition Logger.cpp:36
static void close()
Close the log file.
Definition Logger.cpp:22