LogIt++
Header-only, macro-first C++ logging library
Loading...
Searching...
No Matches
LogStream.hpp
Go to the documentation of this file.
1#pragma once
2#ifndef LOGIT_CPP_HEADER_LOGIT_CPP_LOGIT_DETAIL_LOGSTREAM_HPP_INCLUDED
3#define LOGIT_CPP_HEADER_LOGIT_CPP_LOGIT_DETAIL_LOGSTREAM_HPP_INCLUDED
4
7
8#include <sstream>
9
10namespace logit {
11
17 class LogStream {
18 public:
19
27 LogLevel level,
28 const std::string& file,
29 int line,
30 const std::string& function,
31 int logger_index)
32 : m_level(level), m_file(file), m_line(line), m_function(function),
33 m_logger_index(logger_index) {
34 }
35
42 // Automatically log when the LogStream object is destroyed (end of line).
46 m_stream.str(), std::string(), // No argument names for stream-based logs.
48 false
49 });
50 }
51
56 template <typename T>
57 LogStream& operator<<(const T& value) {
58 m_stream << value;
59 return *this;
60 }
61
62 // Overload of operator<< for manipulators (e.g., std::endl)
63 LogStream& operator<<(std::ostream& (*manip)(std::ostream&)) {
64 m_stream << manip; // Apply the manipulator to the internal stream
65 return *this;
66 }
67
68 private:
70 std::ostringstream m_stream;
71 std::string m_file;
72 int m_line;
73 std::string m_function;
75 };
76
77} // namespace logit
78
79#endif // LOGIT_CPP_HEADER_LOGIT_CPP_LOGIT_DETAIL_LOGSTREAM_HPP_INCLUDED
A stream-based logger that collects log messages using the << operator.
Definition LogStream.hpp:17
LogStream & operator<<(const T &value)
Overloaded << operator to accumulate log content.
Definition LogStream.hpp:57
LogLevel m_level
Log level.
Definition LogStream.hpp:69
int m_logger_index
Logger index.
Definition LogStream.hpp:74
int m_line
Line number.
Definition LogStream.hpp:72
std::ostringstream m_stream
Stream for accumulating log content.
Definition LogStream.hpp:70
std::string m_file
Source file name.
Definition LogStream.hpp:71
std::string m_function
Function name.
Definition LogStream.hpp:73
~LogStream()
Destructor that logs the collected message when the object goes out of scope.
Definition LogStream.hpp:41
LogStream & operator<<(std::ostream &(*manip)(std::ostream &))
Definition LogStream.hpp:63
LogStream(LogLevel level, const std::string &file, int line, const std::string &function, int logger_index)
Constructor.
Definition LogStream.hpp:26
auto log_and_return(const LogRecord &record, Ts &&... args) -> decltype(std::forward_as_tuple(std::forward< Ts >(args)...))
Logs message and returns tuple of arguments.
Definition Logger.hpp:386
static Logger & get_instance()
Retrieves singleton instance of Logger.
Definition Logger.hpp:53
#define LOGIT_CURRENT_TIMESTAMP_MS()
Macro to get the current timestamp in milliseconds.
Definition config.hpp:84
The primary namespace for the LogIt++ library.
LogLevel
Logging levels.
Definition enums.hpp:15
Stores log metadata and content.
Definition LogRecord.hpp:17