reference

This documentation is automatically generated from the openFrameworks source code using doxygen and refers to the most recent release, version 0.12.0.

ofLog Class Reference

A C++ stream-style logging interface. More...

#include <ofLog.h>

Inheritance diagram for ofLog:
ofLogError ofLogFatalError ofLogNotice ofLogVerbose ofLogWarning

Public Member Functions

Logging
 ofLog ()
 Start logging on notice level.
 
 ofLog (ofLogLevel level)
 Start logging on a specific ofLogLevel.
 
 ofLog (ofLogLevel level, const std::string &message)
 Log a string at a specific log level.
 
template<typename ... Args>
 ofLog (ofLogLevel level, const char *format, Args &&... args)
 Logs a message at a specific log level using the printf interface.
 

Static Public Member Functions

Logging configuration
static void setAutoSpace (bool autoSpace)
 Let the logger automaticly add spaces between messages.
 
static void setChannel (std::shared_ptr< ofBaseLoggerChannel > channel)
 Set the logging channel destinations for messages.
 
static std::shared_ptr< ofBaseLoggerChannel > getChannel ()
 Get the current logging channel.
 

Detailed Description

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;
ofLog()
Start logging on notice level.
Definition ofLog.cpp:80

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

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

Constructor & Destructor Documentation

◆ ofLog() [1/4]

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() [2/4]

ofLog::ofLog ( ofLogLevel  level)

Start logging on a specific ofLogLevel.

Example:

// Set the log level.
ofLog(OF_LOG_WARNING) << "a string " << 100 << 20.234f;
@ OF_LOG_WARNING
Definition ofLog.h:119

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;
Derived log class for easy warning logging.
Definition ofLog.h:585
Parameters
levelThe ofLogLevel for this log message.

◆ ofLog() [3/4]

ofLog::ofLog ( ofLogLevel  level,
const std::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));
std::string ofToString(const T &)
Convert a value to a string.
Definition ofUtils.h:657
@ OF_LOG_NOTICE
Definition ofLog.h:118
Parameters
levelThe ofLogLevel for this log message.
messageThe log message.

◆ ofLog() [4/4]

template<typename ... Args>
ofLog::ofLog ( ofLogLevel  level,
const char *  format,
Args &&...  args 
)
inline

Logs a message at a specific log level using the printf interface.

The message is built using the formatting from the C printf function and can be used as a direct replacement. Essentially, the second argument is a string with special formatting specifiers starting with '' that specify where the following variables go in the message. You can have as many variables as you want following the logLevel and format string, but there must be a % specifier for each subsequent variable.

For quick reference, here are a few of the most useful formatting specifiers:

  • d: integer number, 123
  • f: floating point number, 123.45
  • s: a C string (null terminated); this is not a C++ string, use string::c_str() to get a C string from a C++ string
  • c: a single character
  • x: unsigned integer as a hexidecimal number; x uses lower-case letters and X uses upper-case
  • %%: prints a % character

The specifier should match the variable type as it is used to tell the function how to convert that primitive type (int, float, character, etc) into a string.

For instance, let's say we want to print two messages, a salutation and the value of an int, a float, and a string variable:

// Print a simple message with no variables.
ofLog(OF_LOG_WARNING, "Welcome to the jungle.");
// Our variables.
float fun = 11.11;
int games = 100;
string theNames = "Dan, Kyle, & Golan";
// Print a message with variables, sets the message format in the
// format string.
ofLog(OF_LOG_NOTICE, "we've got %d & %f, we got everything you want honey, we know %s", fun, games, theNames.c_str());

Note: theNames.c_str() returns a C string from theNames which is a C++ string object.

There are other formatting options such as setting the decimal precision of float objects and the forward padding of numbers (i.e. 0001 instead of 1). See the Wikipedia printf format string article for more detailed information.

Parameters
levelThe ofLogLevel for this log message.
formatThe printf-style format string.

Member Function Documentation

◆ getChannel()

shared_ptr< ofBaseLoggerChannel > ofLog::getChannel ( )
static

Get the current logging channel.

◆ setAutoSpace()

void ofLog::setAutoSpace ( bool  autoSpace)
static

Let the logger automaticly add spaces between messages.

Default is false.

Parameters
autoSpaceSet to true to add spaces between messages

◆ setChannel()

void ofLog::setChannel ( std::shared_ptr< ofBaseLoggerChannel >  channel)
static

Set the logging channel destinations for messages.

This can be used to output to files instead of stdout.

See also
ofFileLoggerChannel ofConsoleLoggerChannel
Parameters
channelThe channel to log to.

The documentation for this class was generated from the following files:
  • /Users/icq4ever/Desktop/oF0120/libs/openFrameworks/utils/ofLog.h
  • /Users/icq4ever/Desktop/oF0120/libs/openFrameworks/utils/ofLog.cpp