StreamLog 1.0.0
A lightweight C++11 logging library with stream-based API
Loading...
Searching...
No Matches
streamlog.hpp
Go to the documentation of this file.
1#ifndef STREAMLOG_HPP
2#define STREAMLOG_HPP
3
52
53#include <ctime>
54#include <fstream>
55#include <iostream>
56#include <sstream>
57
58#ifdef ENABLE_VECTOR_LOGGING
59#include <vector>
60#endif
61
62#ifdef ENABLE_MAP_LOGGING
63#include <map>
64#endif
65
79
80#if DEBUG_LEVEL == 1
81#define LOG_LEVEL TRACE
82#elif DEBUG_LEVEL == 2
83#define LOG_LEVEL DEBUG
84#elif DEBUG_LEVEL == 3
85#define LOG_LEVEL INFO
86#elif DEBUG_LEVEL == 4
87#define LOG_LEVEL WARN
88#elif DEBUG_LEVEL == 5
89#define LOG_LEVEL ERROR
90#elif DEBUG_LEVEL == 6
91#define LOG_LEVEL FATAL
92#endif
93
101 static const std::string TraceColor;
102 static const std::string DebugColor;
103 static const std::string InfoColor;
104 static const std::string WarnColor;
105 static const std::string ErrorColor;
106 static const std::string FatalColor;
107 static const std::string reset;
108};
109
141public:
150 public:
155 LogStatement(LogStatement &&other) noexcept;
156
162
169 template <typename T> LogStatement &operator<<(const T &value) {
170 m_buffer << value;
171 return *this;
172 }
173
179 LogStatement &operator<<(const char *value) {
180 m_buffer << value;
181 return *this;
182 }
183
184#ifdef ENABLE_VECTOR_LOGGING
192 template <typename T> LogStatement &operator<<(const std::vector<T> &vec) {
193 m_buffer << "[";
194 for (size_t i = 0; i < vec.size(); ++i) {
195 m_buffer << vec[i];
196 if (i != vec.size() - 1) {
197 m_buffer << ", ";
198 }
199 }
200 m_buffer << "]";
201 return *this;
202 }
203#endif
204
205#ifdef ENABLE_MAP_LOGGING
214 template <typename K, typename V>
215 LogStatement &operator<<(const std::map<K, V> &m) {
216 m_buffer << "{";
217 for (auto it = m.begin(); it != m.end(); ++it) {
218 m_buffer << it->first << ": " << it->second;
219 if (std::next(it) != m.end()) {
220 m_buffer << ", ";
221 }
222 }
223 m_buffer << "}";
224 return *this;
225 }
226#endif
231
236 std::string getBufferContent() const;
237
242 void appendToBuffer(const std::string &content);
243
251
252 private:
253 StreamLog &m_logger;
254 std::ostringstream m_buffer;
255 };
256
263
269 LogStatement operator<<(std::ostream &(*manipulator)(std::ostream &));
270
271public:
286 static StreamLog &instance(const std::string &fileName = "output.log",
287 bool consoleOutput = false);
288
292 virtual ~StreamLog();
293
294 // Delete copy and move operations (Rule of Five)
295 StreamLog(const StreamLog &) = delete;
296 StreamLog &operator=(const StreamLog &) = delete;
297 StreamLog(StreamLog &&) = delete;
299
300protected:
308 explicit StreamLog(const std::string &fileName, bool consoleOutput = false);
309
312
313 std::string m_fileName;
315
321 std::string levelToString(const LogLevel &level) const;
322
327 std::string getColor() const;
328
334 virtual std::string getTimestamp() const;
335
342 virtual std::stringstream buildLog(const std::string &message) const;
343
348 void writeLog(const std::string &message);
349
354 void commitLog(const std::string &message);
355
356private:
362 bool createDirectories(const std::string &path) const;
363};
364
375#endif
RAII wrapper for building and committing log messages.
Definition streamlog.hpp:149
LogStatement(LogStatement &&other) noexcept
Move constructor.
LogStatement(StreamLog &logger)
Construct LogStatement bound to a logger.
LogStatement & operator<<(const char *value)
Stream insertion operator for C-strings.
Definition streamlog.hpp:179
LogStatement & operator<<(const T &value)
Stream insertion operator for generic types.
Definition streamlog.hpp:169
void appendToBuffer(const std::string &content)
Append content to buffer.
~LogStatement()
Destructor commits the log message.
void clearBuffer()
Clear the internal message buffer.
std::string getBufferContent() const
Get current buffer contents.
StreamLog & operator=(StreamLog &&)=delete
No move assignment.
virtual std::stringstream buildLog(const std::string &message) const
Build formatted log message with timestamp and level.
StreamLog(const StreamLog &)=delete
No copy constructor.
std::string levelToString(const LogLevel &level) const
Convert LogLevel enum to string.
StreamLog(StreamLog &&)=delete
No move constructor.
LogStatement getLogStatement(LogLevel level)
Create a LogStatement for the given log level.
StreamLog(const std::string &fileName, bool consoleOutput=false)
Construct logger with file and console settings.
StreamLog & operator=(const StreamLog &)=delete
No copy assignment.
LogLevel m_threshold
Minimum level to actually write (compile-time).
Definition streamlog.hpp:311
std::string getColor() const
Get ANSI color code for current log level.
void writeLog(const std::string &message)
Write formatted message to file and optionally console.
virtual ~StreamLog()
Virtual destructor for inheritance support.
std::string m_fileName
Path to log file.
Definition streamlog.hpp:313
bool m_consoleOutput
Whether to also write to stderr.
Definition streamlog.hpp:314
static StreamLog & instance(const std::string &fileName="output.log", bool consoleOutput=false)
Get the singleton logger instance.
LogStatement operator<<(std::ostream &(*manipulator)(std::ostream &))
Stream manipulator support (e.g., std::endl).
LogLevel m_level
Current log level being written.
Definition streamlog.hpp:310
virtual std::string getTimestamp() const
Get current timestamp.
void commitLog(const std::string &message)
Commit message if it meets threshold.
StreamLog::LogStatement log(LogLevel level)
Global logging function.
LogLevel
Log severity levels.
Definition streamlog.hpp:71
@ DEBUG
Debug information.
Definition streamlog.hpp:73
@ ERROR
Error conditions.
Definition streamlog.hpp:76
@ FATAL
Critical failures.
Definition streamlog.hpp:77
@ INFO
Informational messages.
Definition streamlog.hpp:74
@ WARN
Warning conditions.
Definition streamlog.hpp:75
@ TRACE
Detailed debugging information.
Definition streamlog.hpp:72
ANSI color codes for log levels.
Definition streamlog.hpp:100
static const std::string ErrorColor
ERROR level color.
Definition streamlog.hpp:105
static const std::string InfoColor
INFO level color.
Definition streamlog.hpp:103
static const std::string WarnColor
WARN level color.
Definition streamlog.hpp:104
static const std::string DebugColor
DEBUG level color.
Definition streamlog.hpp:102
static const std::string reset
Reset to default color.
Definition streamlog.hpp:107
static const std::string TraceColor
TRACE level color.
Definition streamlog.hpp:101
static const std::string FatalColor
FATAL level color.
Definition streamlog.hpp:106