Babel  1
The voip software that only works on your local network
Loading...
Searching...
No Matches
Log.cpp
Go to the documentation of this file.
1/*
2** EPITECH PROJECT, 2024
3** rtype (Workspace)
4** File description:
5** Log.cpp
6*/
7
14#include "Logging/Log.hpp"
15
23{
24 static Logging::Log instance;
25 // std::ios_base::sync_with_stdio(true); // Enable synchronization with C stdio
26 std::ios_base::sync_with_stdio(false); // Disable synchronization with C stdio
27 instance.setStringAsDebug(debug);
28 return instance;
29}
30
34Logging::Log::Log() : _debugEnabled(false) {}
35
44std::string Logging::Log::getLogLocation(const char *file, int line, const char *func)
45{
46 std::ostringstream oss;
47 oss << file << ":" << line << " " << func << "()";
48 return oss.str();
49}
50
57{
58 _logEnabled = enabled;
59}
60
67{
68 _debugEnabled = enabled;
69}
70
77{
78 return _logEnabled;
79}
80
87{
88 return _debugEnabled;
89}
90
96void Logging::Log::log(const std::string &message)
97{
98 if (!_logEnabled) {
99 return;
100 }
101
102 if (_stringDebug && !_debugEnabled) {
103 return;
104 }
105
106 std::lock_guard<std::mutex> lock(_mtxLog);
107 std::cout << getCurrentDateTime() << message << std::endl;
108}
109
115void Logging::Log::log(const char *message)
116{
117 log(std::string(message));
118}
119
127void Logging::Log::setStringAsDebug(const bool stringDebug)
128{
129 _stringDebug = stringDebug;
130}
131
138{
139 auto now = std::chrono::system_clock::now();
140 std::time_t currentTime = std::chrono::system_clock::to_time_t(now);
141 auto millis = std::chrono::duration_cast<std::chrono::milliseconds>(
142 now.time_since_epoch()
143 ).count() % 1000;
144
145 std::tm *tm = std::localtime(&currentTime);
146
147 std::stringstream ss;
148 ss << "[" << (tm->tm_year + 1900) << "-"
149 << (tm->tm_mon + 1) << "-"
150 << tm->tm_mday << " "
151 << tm->tm_hour << ":"
152 << tm->tm_min << ":"
153 << tm->tm_sec << "."
154 << std::setfill('0') << std::setw(3) << millis << "]";
155 return ss.str();
156}
This file contains the Log class responsible for outputting information only when requested.
A singleton class that provides thread-safe logging capabilities with timestamps, active only when lo...
Definition Log.hpp:38
void log(const std::string &message)
Logs a message if logging is enabled.
Definition Log.cpp:96
void setStringAsDebug(const bool stringDebug=false)
Sets the internal boolean _stringDebug.
Definition Log.cpp:127
std::string getLogLocation(const char *file, int line, const char *func)
Generates a formatted debug information string with file, line, and function details.
Definition Log.cpp:44
void setLogEnabled(bool enabled)
Enables or disables logging.
Definition Log.cpp:56
static Log & getInstance(const bool debug=false)
Provides access to the singleton instance of the Log class.
Definition Log.cpp:22
const bool getDebugEnabled() const
Checks if debug logging is enabled.
Definition Log.cpp:86
void setDebugEnabled(bool enabled)
Enables or disables debug logging.
Definition Log.cpp:66
std::string getCurrentDateTime()
Retrieves the current date and time as a formatted string.
Definition Log.cpp:137
const bool getLogEnabled() const
Checks if logging is enabled.
Definition Log.cpp:76