ofDoc > utils > ofLog

ofLogfile

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

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 set the logging level so only messages above a certain level 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(ofLogLevel level) for more details.

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);

Stream: as a stream using the << stream operator

// The stream style, setting the log level to OF_LOG_WARNING.
ofLog(OF_LOG_WARNING) << "the number is " << 10;
// This is the same as the last line, except it uses the default OF_LOG_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 message."
ofLogNotice() << "A regular notice message.";
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(string module, ofLogLevel level) for more details.

Example of logging to a specific module:

// log to a module called "Hello"
ofLogWarning("Hello") << "A warning message.";

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 message.";
// !!! This does not print a message because 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 message.";

Log Message Redirection

It's useful to be able to record log messages to a file or send them to a custom destination.

For log redirection see

Global logging level

ofLogLevel enum

Global logger channel

ofLogclass

A C++ stream-style logging interface.

ofLog accepts variables via the std::ostream operator << and builds a string and logs it when the stream is finished (via the destructor). A newline is printed automatically and all the stream controls (std::endl, std::flush, std::hex, etc) work normally. The default log level is OF_LOG_NOTICE.

Basic usage:

ofLog() << "My integer is " << 100 << " and my float is " << 20.234f;

It also accepts the legacy ofLog interface: ofLog(ofLogLevel level, string message):

ofLog(OF_LOG_ERROR, "Another string.");
Author
Dan Wilcox danom.nosp@m.atik.nosp@m.a@gma.nosp@m.il.c.nosp@m.om danomatika.com

Logging

ofLog ()

Logging configuration

ofLogVerboseclass

Derived log class for easy verbose logging.

Example: ofLogVerbose("Log message").

Logging configuration

Functions

ofLogNoticeclass

Derived log class for easy notice logging.

Example: ofLogNotice("Log message").

Logging configuration

Functions

ofLogWarningclass

Derived log class for easy warning logging.

Example: ofLogWarning("Log message").

Logging configuration

Functions

ofLogErrorclass

Derived log class for easy error logging.

Example: ofLogError("Log message").

Logging configuration

Functions

ofLogFatalErrorclass

Derived log class for easy fatal error logging.

Example: ofLogFatalError("Log message").

Logging configuration

Functions