R-Type  2
Doom but in better
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 "Log.hpp"
15
17{
18 static Logging::Log instance;
19 instance.setStringAsDebug(debug);
20 return instance;
21}
22
23std::string Logging::Log::getLogLocation(const char *file, int line, const char *func)
24{
25 std::ostringstream oss;
26 oss << file << ":" << line << " " << func << "()";
27 return oss.str();
28}
29
31{
32 _logEnabled = enabled;
33}
34
36{
37 _debugEnabled = enabled;
38}
39
40void Logging::Log::log(const std::string &message)
41{
42 if (!_logEnabled) {
43 return;
44 }
45
46 if (_stringDebug && !_debugEnabled) {
47 return;
48 }
49
50 std::lock_guard<std::mutex> lock(_mtxLog);
51 std::cout << getCurrentDateTime() << message << std::endl;
52}
53
54void Logging::Log::log(const char *message)
55{
56 log(std::string(message));
57}
58
59void Logging::Log::setStringAsDebug(const bool stringDebug)
60{
61 _stringDebug = stringDebug;
62}
63
65{
66 auto now = std::chrono::system_clock::now();
67 std::time_t currentTime = std::chrono::system_clock::to_time_t(now);
68 auto millis = std::chrono::duration_cast<std::chrono::milliseconds>(
69 now.time_since_epoch()
70 ).count() % 1000;
71
72
73 std::tm *tm = std::localtime(&currentTime);
74
75 std::stringstream ss;
76 ss << "[" << (tm->tm_year + 1900) << "-"
77 << (tm->tm_mon + 1) << "-"
78 << tm->tm_mday << " "
79 << tm->tm_hour << ":"
80 << tm->tm_min << ":"
81 << tm->tm_sec << "."
82 << std::setfill('0') << std::setw(3) << millis << "]";
83 return ss.str();
84}
This is the file in charge of containing the Log class (the one in charge of outputing info only when...
A singleton class that provides thread-safe logging capabilities with timestamps, active only when lo...
Definition Log.hpp:31
void log(const std::string &message)
Logs a message if debugging is enabled.
Definition Log.cpp:40
void setStringAsDebug(const bool stringDebug=false)
Function to set the internal boolean _stringAsDebug.
Definition Log.cpp:59
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:23
void setLogEnabled(bool enabled)
Enables or disables the logging.
Definition Log.cpp:30
static Log & getInstance(const bool debug=false)
Provides access to the singleton instance of the Debug class.
Definition Log.cpp:16
void setDebugEnabled(bool enabled)
Enables or disables the debug logging.
Definition Log.cpp:35
std::string getCurrentDateTime()
Retrieves the current date and time as a formatted string.
Definition Log.cpp:64