ofDocsdocumentation utils ofLog

ofLog provides an interface for writing text output from your app. It's basically a more useful version of cout or printf where output can be filtered and written to the console or to a file.

Sometimes you want to be able to see when something has happened inside the code, but don't need to draw something visually. Oftentimes it's more then enough to print out the state of a few variables when debugging. Other times you need to know if a crash happened while your app was running somewhere, so you log messages and variables to a file you can read after the program crashes.

####Log Levels

You can also set the logging level so only messages above a certain priority are shown. This is useful if you want see lots of messages when debugging, but then set a higher level so only warnings and errors appear for users. See ofSetLogLevel(logLevel) for more detail.

Log levels are (in order of priority):

OF_LOG_VERBOSE
OF_LOG_NOTICE
OF_LOG_WARNING
OF_LOG_ERROR
OF_LOG_FATAL_ERROR
OF_LOG_SILENT

Note: OF_LOG_SILENT is a special value which disables all messages.

####Usage

There are 2 ways you can use ofLog:

####Functional: as a function taking a message


// send a single string message, setting the log level
ofLog(OF_LOG_NOTICE, "the number is " + ofToString(10));

// the legacy printf style
ofLog(OF_LOG_NOTICE, "the number is %d", 10); 

See ofLog(logLevel, &message) & ofLog(logLevel, format*, ...) for more details.

####Stream: as a stream using the << stream operator


// the stream style, setting the log level
ofLog(OF_LOG_NOTICE) << "the number is " << 10;

// this is the same as the last line, but only sends at log level notice
ofLog() << "the number is " << 10;

// there are also log level specific stream objects,
// one for each level except OF_LOG_SILENT
ofLogVerbose() << "a verbose print"
ofLogNotice() << "a regular notice print";
ofLogWarning() << "uh oh, a warning";
ofLogError() << "oh no, an error occurred!";
ofLogFatalError() << "accckkk, a fatal error!!";

Note: The log level specific stream objects also take a string argument for the "module". A module is a string that is added to the beginning of the log line and can be used to separate logging messages by setting an independent log level for that module only. This module-specific log level has no effect on other modules. See ofSetLogLevel(module, logLevel) for more detail.


// log to a module called "Hello"
ofLogWarning("Hello") << "a warning print";

Warning: It is important to understand that the log level specific stream objects take the module name as an argument and the log messages via the << operator. Putting your message as a string argument inside the parentheses uses that message as a module and so nothing will be printed:


// this prints a warning message
ofLogWarning() << "a warning print";

// !!! this does not print a message as the string "a warning print" is the module argument !!!
ofLogWarning("a warning print");

// this prints a warning message to the "Hello" module
ofLogWarning("Hello") << "a warning print";

####Log Message Redirection

Last, it's useful to be able to record log messages to a file or send them to a custom destination. For log redirection see ofLogToFile(), ofLogToConsole(), & ofSetLoggerChannel() in the ofLog functions.


_log( ... )

void _log(ofLogLevel level, const string &module, const string &message)

Print a log line.

Parameters:

level The log level.

module The target module.

message The log message.


checkLog( ... )

bool checkLog(ofLogLevel level, const string &module)

Determine if the given module is active at the given log level.

Parameters:

level The log level.

module The target module.

Returns: true if the given module is active at the given log level.


getPadding( )

string & getPadding()

ofLog( ... )

ofLog(const ofLog &)

ofLog( )

ofLog()

Start logging on notice level.

ofLog provides a streaming log interface by accepting variables via the std::ostream operator << similar to std::cout and std::cerr.

It builds a string and logs it when the stream is finished. A newline is printed automatically and all the stream controls (std::endl, std::flush, std::hex, etc) work normally.


// Converts primitive types (int, float, etc) to strings automatically.
ofLog() << "a string " << 100 << 20.234f;

The log level is OF_LOG_NOTICE by default.

ofLog provides a streaming log interface by accepting variables via the ostream operator << similar to cout and cerr.

It builds a string and logs it when the stream is finished. A newline is printed automatically and all the stream controls (endl, flush, hex, etc) work normally.


// converts incoming primitive types (int, float, etc) to strings automatically
ofLog() << "a string" << 100 << 20.234f;

The log level is explicitly OF_LOG_NOTICE.

See ofSetLogLevel(logLevel) for more info on log levels.


ofLog( ... )

ofLog(ofLogLevel level)

Start logging on a specific ofLogLevel.

Example:


// Set the log level.
ofLog(OF_LOG_WARNING) << "a string " << 100 << 20.234f;

You can use the derived convenience classes as an alternative for specific log levels:

ofLogVerbose()
ofLogNotice()
ofLogWarning()
ofLogError()
ofLogFatalError()

// Set the log level.
ofLog(OF_LOG_WARNING) << "a string " << 100 << 20.234f;

// This is the same as above.
ofLogWarning() << "a string " << 100 << 20.234f;

Parameters:

level The ofLogLevel for this log message.

The same as the ofLog() stream interface, except it accepts a log level.


// set the log level
ofLog(OF_LOG_WARNING) << "a string" << 100 << 20.234f;

You can use the derived convenience classes as an alternative for specific log levels:

ofLogVerbose()
ofLogNotice()
ofLogWarning()
ofLogError()
ofLogFatalError()

// set the log level
ofLog(OF_LOG_WARNING) << "a string" << 100 << 20.234f;

// this is the same as above
ofLogWarning() << "a string" << 100 << 20.234f;

See ofSetLogLevel() for more info on log levels.


ofLog( ... )

ofLog(ofLogLevel level, const string &message)

Log a string at a specific log level.

Supply the logging message as a parameter to the function instead of as a stream.

The string message can be concatenated using the ofToString(const T& value) conversion function:


// Build a single string message.
ofLog(OF_LOG_NOTICE, "the number is "
+ ofToString(10) + " and I have a float too " + ofToString(123.45f));

Parameters:

level The ofLogLevel for this log message.

message The log message.

Logs a string at a specific log level.

The string message can be concatenated using the ofToString() conversion function in ofUtils:


// build a single string message
ofLog(OF_LOG_NOTICE, "the number is " 
+ ofToString(10) + " and I have a float too " + ofToString(123.45f));

See ofSetLogLevel(logLevel) for more info on log levels.


operator<<( ... )

ofLog & operator<<(const T &value)

Define flexible stream operator.

This allows the class to use the << std::ostream to read data of almost any type.

\tparam T the data type to be streamed.

Parameters:

value the data to be streamed.

Returns: A reference to itself.


operator<<( ... )

ofLog & operator<<(ostream &(*)(ostream &) func)

Define flexible stream operator.

This allows the class to use the << std::ostream to catch function pointers such as std::endl and std::hex.

Parameters:

func A function pointer that takes a std::ostream as an argument.

Returns: A reference to itself.


operator=( ... )

ofLog & operator=(ofLog &from)

setAutoSpace( ... )

void setAutoSpace(bool autoSpace)

Let the logger automaticly add spaces between messages.

Default is false.

Parameters:

autoSpace Set to true to add spaces between messages


~ofLog( )

~ofLog()

Destroy the ofLog.

This destructor does the actual printing via std::ostream.


Variables

bool bAutoSpace< Should space be added between messages?
bool bPrinted< Has the message been printed in the constructor?
ofLogLevel level< Log level.
std message< Temporary buffer.
string module< The destination module for this message.