Babel  1
The voip software that only works on your local network
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#ifdef _WIN32
22 #include <io.h>
23 #include <windows.h>
24 #define isatty _isatty
25 #define fileno _fileno
26#else
27 #include <unistd.h>
28#endif
30
31namespace Logging
32{
38 class Log {
39 public:
46 static Log &getInstance(const bool debug = false);
47
56 std::string getLogLocation(const char *file, int line, const char *func);
57
63 void setLogEnabled(bool enabled);
64
70 void setDebugEnabled(bool enabled);
71
77 const bool getLogEnabled() const;
78
84 const bool getDebugEnabled() const;
85
91 void log(const std::string &message);
92
98 void log(const char *message);
99
110 template <typename T>
111 Log &operator<<(const T &message)
112 {
113 if (_logEnabled) {
114 std::lock_guard<std::mutex> lock(_mtx);
115 _buffer << message;
116 }
117 return *this;
118 };
119
129 Log &operator<<(const std::string &message)
130 {
131 if (_logEnabled) {
132 std::lock_guard<std::mutex> lock(_mtx);
133 _buffer << message;
134 }
135 return *this;
136 };
137
148 Log &operator<<(std::ostream &(*os)(std::ostream &))
149 {
150 if (_logEnabled) {
151 std::lock_guard<std::mutex> lock(_mtx);
152 if (os == static_cast<std::ostream & (*)(std::ostream &)>(std::endl)) {
153 log(_buffer.str());
154 _buffer.str("");
155 _buffer.clear();
156 } else {
157 os(_buffer);
158 }
159 }
160 return *this;
161 };
162
168 std::string getCurrentDateTime();
169
177 void setStringAsDebug(const bool stringDebug = false);
178
179 private:
180 bool _stringDebug = false;
181 bool _logEnabled = false;
182 bool _debugEnabled = false;
183 std::mutex _mtx;
184 std::mutex _mtxLog;
185 std::ostringstream _buffer;
186
190 Log();
191
192 Log(const Log &) = delete;
193 Log &operator=(const Log &) = delete;
194
195 };
196
202 inline bool isRedirected()
203 {
204 return !isatty(fileno(stdout));
205 }
206}
Provides custom operator<< overloads for various types.
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
Log & operator<<(const std::string &message)
Appends a string message to the log if logging is enabled.
Definition Log.hpp:129
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
Log & operator<<(const T &message)
Appends a message to the log if logging is enabled.
Definition Log.hpp:111
Log & operator<<(std::ostream &(*os)(std::ostream &))
Handles special stream manipulators (e.g., std::endl) for logging with timestamps if logging is enabl...
Definition Log.hpp:148
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
bool isRedirected()
Checks if the output is being redirected to a file.
Definition Log.hpp:202