R-Type  2
Doom but in better
Loading...
Searching...
No Matches
Log.hpp
Go to the documentation of this file.
1/*
2** EPITECH PROJECT, 2024
3** rtype (Workspace)
4** File description:
5** Log.hpp
6*/
7
13#pragma once
14#include <ctime>
15#include <mutex>
16#include <string>
17#include <chrono>
18#include <sstream>
19#include <iomanip>
20#include <iostream>
21// #include <unistd.h>
23
24namespace Logging
25{
31 class Log {
32 public:
40 static Log &getInstance(const bool debug = false);
50 std::string getLogLocation(const char *file, int line, const char *func);
51
57 void setLogEnabled(bool enabled);
58
64 void setDebugEnabled(bool enabled);
65
71 void log(const std::string &message);
72
78 void log(const char *message);
79
92 template <typename T>
93 Log &operator<<(const T &message)
94 {
95 if (_logEnabled) {
96 std::lock_guard<std::mutex> lock(_mtx);
97 _buffer << message;
98 }
99 return *this;
100 };
101
112 Log &operator<<(const std::string &message)
113 {
114 if (_logEnabled) {
115 std::lock_guard<std::mutex> lock(_mtx);
116 _buffer << message;
117 }
118 return *this;
119 };
120
132 Log &operator<<(std::ostream &(*os)(std::ostream &))
133 {
134 if (_logEnabled) {
135 std::lock_guard<std::mutex> lock(_mtx);
136 if (os == static_cast<std::ostream & (*)(std::ostream &)>(std::endl)) {
137 log(_buffer.str());
138 _buffer.str("");
139 _buffer.clear();
140 } else {
141 os(_buffer);
142 }
143 }
144 return *this;
145 };
146
152 std::string getCurrentDateTime();
153
163 void setStringAsDebug(const bool stringDebug = false);
164
165 private:
166 bool _stringDebug = false;
167 bool _logEnabled = false;
168 bool _debugEnabled = false;
169 std::mutex _mtx;
170 std::mutex _mtxLog;
171 std::ostringstream _buffer;
172
176 Log() : _debugEnabled(false) {}
177
178 Log(const Log &) = delete;
179 Log &operator=(const Log &) = delete;
180
181 };
182
189 inline bool isRedirected()
190 {
191 // return !isatty(fileno(stdout));
192 return false;
193 }
194}
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
Log & operator<<(const std::string &message)
Appends a string message to the debug log if debugging is enabled.
Definition Log.hpp:112
static Log & getInstance(const bool debug=false)
Provides access to the singleton instance of the Debug class.
Definition Log.cpp:16
Log & operator<<(const T &message)
Appends a message to the debug log if debugging is enabled.
Definition Log.hpp:93
Log & operator<<(std::ostream &(*os)(std::ostream &))
Handles special stream manipulators (e.g., std::endl) for logging with timestamps if debugging is ena...
Definition Log.hpp:132
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
bool isRedirected()
Check if the output is being redirected to a file.
Definition Log.hpp:189