CUGL 2.5
Cornell University Game Library
|
#include <CULogger.h>
Public Types | |
enum class | Level : int { NO_MSG = 0 , FATAL_MSG = 1 , ERROR_MSG = 2 , WARN_MSG = 3 , INFO_MSG = 4 , DEBUG_MSG = 5 , VERBOSE_MSG = 6 } |
Public Member Functions | |
Logger () | |
~Logger () | |
const std::string | getName () const |
const std::string | getPath () const |
Level | getLogLevel () const |
void | setLogLevel (Level level) |
Level | getConsoleLevel () const |
void | setConsoleLevel (Level level) |
bool | doesAutoFlush () const |
void | setAutoFlush (bool value) |
void | log (const std::string format,...) |
void | log (const char *format,...) |
void | log (Level level, const std::string format,...) |
void | log (Level level, const char *format,...) |
void | flush () |
Static Public Member Functions | |
static std::shared_ptr< Logger > | open (const std::string channel) |
static std::shared_ptr< Logger > | open (const std::string channel, Level level) |
static std::shared_ptr< Logger > | get (const std::string channel) |
static bool | close (const std::string channel) |
Protected Attributes | |
std::string | _name |
std::string | _path |
Level | _fileLevel |
Level | _consLevel |
std::shared_ptr< TextWriter > | _writer |
int | _category |
char | _timestamp [64] |
char * | _buffer |
size_t | _capacity |
bool | _autof |
bool | _open |
Static Protected Attributes | |
static std::unordered_map< std::string, std::shared_ptr< Logger > > | _channels |
static int | _nextcategory |
This class provides an interface for fine-grained logging.
This class is an alternative to CULog
that provides a lot more features. First of all it provides multiple log channels, each with its own settings. In addition, each channel has its own associated log file. This allows you to keep separate logs for the purposes of analysis and debugging.
Logs are written to the save directory Application#getSaveDirectory
. They are named <channel>.log
where <channel>
is the name of the log channel. Each line of the log file is prefixed by the time up to the nearest microsecond. Messages are only written to the file if they have a log level less than or equal to log level of the channel.
It is possible to log to a file and the output console (e.g. the output stream for CULog) at the same time. The console has its own log level (defined as getConsoleLevel
) and will process messages accordingly. Note that the console uses its own timestamps, and so there will be a few microseconds difference between the log file and the console.
|
strong |
An enum to represent the logging state
Log-levels are used to prioritize messages. Prioriy in this enumeration is assigned higher priority to lower priority. So Level#FATAL_MSG
has highest priority while Level#VERBOSE_MSG
has the lowest priority. Level#NO_MSG
is a special level indicating that no logging should occur.
For any given log channel, you can set it to ignore any messages below a certain priority. For example, if the log channel has a level of Level#INFO_MSG
, then it will ignore any messages of the level Level#DEBUG_MSG
.
cugl::Logger::Logger | ( | ) |
Constructs a new logger object.
This constructor is only initializes the default values, and does not create a usuable logger. It should never be accessed by the user. Use the static method open
instead.
|
inline |
Deletes this logger, releasing all resources.
|
static |
Closes the log for the given channel.
Once called, any references to the given log are invalid. If any shared pointers to the log persist, any attempt to write to them will fail.
channel | The log channel |
|
inline |
Returns true if this logger autoflushes
If a logger does not have autoflush, the messages are not guaranteed to be written to the file until flush
is called. Otherwise, the file is written after every message. To improve performance, you may wish to disable this feature if you are writting a large number of messages per animation frame.
void cugl::Logger::flush | ( | ) |
Flushes any pending messages to the log file.
If a logger has not set doesAutoFlush
, the messages are not guaranteed to be written to the file until this method is called. Otherwise, the file is written after every message. To improve performance, you may wish to disable auto flush if you are writting a large number of messages per animation frame.
|
static |
Returns the logger for the given channel.
If the specified channel is not open, this method returns nullptr.
channel | The log channel |
|
inline |
Returns the log level for the console.
It is possible to simultaneously log to the log file and the console (e.g. the output stream of CULog). However, messages of a lower priority than this log level will not be written to the console. By default, the console has level Level#NO_MSG
, meaning no messages are written to the console.
Note that the console uses its own timestamps, and so there will be a few microseconds difference between the timestamp of a message in the log file and the timestamp in the console.
|
inline |
Returns the log level for the file associate with this logger.
Messages of a lower priority than this log level will not be written to the file. If the level is set to Level#NO_MSG
then no messages will be written to the file.
|
inline |
Returns the channel name for this logger
|
inline |
Returns the absolute path to the file for this logger
void cugl::Logger::log | ( | const char * | format, |
... | |||
) |
Sends a message to this logger.
This method has the same structure as printf and CULog. It consists of a format string followed by zero or more arguments to substititute into the string.
The message will be logged with level getLogLevel
to the file and getConsoleLevel
to the console.
format | The formatting string |
... | The printf-style subsitution arguments |
void cugl::Logger::log | ( | const std::string | format, |
... | |||
) |
Sends a message to this logger.
This method has the same structure as printf and CULog. It consists of a format string followed by zero or more arguments to substititute into the string.
The message will be logged with level getLogLevel
to the file and getConsoleLevel
to the console.
format | The formatting string |
... | The printf-style subsitution arguments |
void cugl::Logger::log | ( | Level | level, |
const char * | format, | ||
... | |||
) |
Sends a message to this logger.
This method has the same structure as printf and CULog. It consists of a format string followed by zero or more arguments to substititute into the string.
The message will be logged with given level to both the file and the console. The message must be of equal priority or higher than getLogLevel
or getConsoleLevel
to appear in the file or on the console, respectively.
level | The priority for this log message |
format | The formatting string |
... | The printf-style subsitution arguments |
void cugl::Logger::log | ( | Level | level, |
const std::string | format, | ||
... | |||
) |
Sends a message to this logger.
This method has the same structure as printf and CULog. It consists of a format string followed by zero or more arguments to substititute into the string.
The message will be logged with given level to both the file and the console. The message must be of equal priority or higher than getLogLevel
or getConsoleLevel
to appear in the file or on the console, respectively.
level | The priority for this log message |
format | The formatting string |
... | The printf-style subsitution arguments |
|
static |
Returns a new logger for the given channel.
If this channel already exists, this method will return the existing logger for that channel. A new logger will always start with level Level#INFO_MSG
for the log file and Level#NO_MSG
for the console.
channel | The log channel |
|
static |
Returns a new logger for the given channel and log level.
The level will apply to the log file. The console will always have level Level#NO_MSG
unless otherwise set by setConsoleLevel
.
If this channel already exists, this method will return the existing logger for that channel. It will also update the log level of that channel to the one specified.
channel | The log channel |
level | The log level |
void cugl::Logger::setAutoFlush | ( | bool | value | ) |
Sets whether this logger should autoflush
If a logger does not have autoflush, the messages are not guaranteed to be written to the file until flush
is called. Otherwise, the file is written after every message. To improve performance, you may wish to disable this feature if you are writting a large number of messages per animation frame.
value | whether this logger should autoflush |
void cugl::Logger::setConsoleLevel | ( | Level | level | ) |
Sets the log level for the console.
It is possible to simultaneously log to the log file and the console (e.g. the output stream of CULog). However, messages of a lower priority than this log level will not be written to the console. By default, the console has level Level#NO_MSG
, meaning no messages are written to the console.
Note that the console uses its own timestamps, and so there will be a few microseconds difference between the timestamp of a message in the log file and the timestamp in the console.
level | the log level for the console. |
void cugl::Logger::setLogLevel | ( | Level | level | ) |
Sets the log level for the file associate with this logger.
Messages of a lower priority than this log level will not be written to the file. If the level is set to Level#NO_MSG
then no messages will be written to the file.
Changing the value always flushes any pending messages to the file.
level | the level for the file associate with this logger |
|
protected |
Whether auto flush is active
|
protected |
The buffer for formatting log messages
|
protected |
The capacity of the formatting buffer
|
protected |
The SDL category for this logger
|
staticprotected |
The list of all active logs
|
protected |
The console log level of this logger
|
protected |
The file log level of this logger
|
protected |
The name of this log channel
|
staticprotected |
The SDL category to assign to the next allocated log
|
protected |
Whether this channel is still open.
|
protected |
The path to the log file
|
protected |
A buffer for writting timestamp information
|
protected |
The text writer for outputing to a file