Logger Core
The following
-
class LoggerCore
Created during a LOG call as a temporary object, and acts as a custom output stream.
Each LoggerCore object is associated with a single ‘log entry’.
Public Functions
-
LoggerCore(LogLevel level, int callingLine, const std::string &callingFunction, std::string callingFile)
Constructor for the LoggerCore, initialises an entry in the output log. Should never be called outside LOG.
Note that the Level is used purely for formatting purposes: a LoggerCore object always outputs a stream. The log suppression checks are performed by LOG. The three ‘calling’ parameters are used to locate the origin of a LOG; they are provided by compile-time flags, but are only used by ERROR and WARN output.
- Parameters:
level – The LogLevel that the associated entry will be on. Determines formatting.
callingLine – This is the ƒile line on which the LOG call appears
callingFunction – The function in which the LOG call occurred. Provides a pseudo-stack-unwinding.
callingFile – The file in which the callingFunction is found.
-
~LoggerCore()
Custom destructor which performs the actual output to the terminal.
Under normal usage, the LOG(LEVEL) call will cause the temporary object to almost instantly go out of scope. Therefore this destructor will usually be called as soon as the last element is streamed into the object, however there may sometimes be a delay.
-
template<class T>
inline LoggerCore &operator<<(const T &msg) The streaming operator, allowing data to be piped into the stream just as if it were std::cout.
- Parameters:
msg – The data to be added to the stream. Must be convertible to a stringstream object
- Returns:
A reference to the object, allowing for ‘chaining’ the stream; stream << a << b << c
-
void Erase(int nLines)
Deletes the specified number of lines from the terminal.
Uses ANSI escape codes to clear lines and reset the cursor to emulate ‘deleting’ lines. See Erasing Logs Documentation for more information.
- Parameters:
nLines – The number of lines (counted by line breaks, not word wrapping) to be deleted
-
void ErasePrevious()
Deletes log entries until it has deleted an entry equal to the Level, or it encounters a higher level entry, which is not deleted.
This function is designed to delete previously added lines, but is respectful of those which are higher priority (and disrespectful of lower-priority lines).See Erasing Logs Documentation for more information.
Private Functions
-
void Header()
If Config.UseHeaders is true, this function generates headers (such as [ERROR]) for the log entry.
-
void endMessage()
The point at which the Buffer is added to the output stream.
Called by the destructor if the Buffer contains data. This function tidies up the buffer and formats it for output, before adding it to the output stream.
Private Members
-
std::stringstream Buffer
The internal Buffer to which LoggerCore::operator<< is streamed, and which is then output to terminal.
-
LogLevel Level
The LogLevel of the log entry associated with this object. Used only to determine formatting.
-
bool StreamActive
An internal flag which checks if anything was actually streamed to the object. If this is true, then the destructor calls endMessage() and outputs the Buffer to stream.
-
std::string Insert
A string which holds the callingLine/Function/File data after the constructor is called, but before the stream is activated.
-
LoggerCore(LogLevel level, int callingLine, const std::string &callingFunction, std::string callingFile)