VTK
Classes | Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions | List of all members
vtkLogger Class Reference

logging framework for use in VTK and in applications based on VTK More...

#include <vtkLogger.h>

Inheritance diagram for vtkLogger:
[legend]
Collaboration diagram for vtkLogger:
[legend]

Classes

class  LogScopeRAII
 
struct  Message
 The message structure that is passed to custom callbacks registered using vtkLogger::AddCallback. More...
 

Public Types

enum  Verbosity {
  VERBOSITY_INVALID = -10, VERBOSITY_OFF = -9, VERBOSITY_ERROR = -2, VERBOSITY_WARNING = -1,
  VERBOSITY_INFO = 0, VERBOSITY_0 = 0, VERBOSITY_1 = +1, VERBOSITY_2 = +2,
  VERBOSITY_3 = +3, VERBOSITY_4 = +4, VERBOSITY_5 = +5, VERBOSITY_6 = +6,
  VERBOSITY_7 = +7, VERBOSITY_8 = +8, VERBOSITY_9 = +9, VERBOSITY_TRACE = +9,
  VERBOSITY_MAX = +9
}
 
enum  FileMode { TRUNCATE, APPEND }
 Support log file modes: TRUNCATE truncates the file clearing any existing contents while APPEND appends to the existing log file contents, if any. More...
 
using LogHandlerCallbackT = void(*)(void *user_data, const Message &message)
 Callback handle types. More...
 
using CloseHandlerCallbackT = void(*)(void *user_data)
 Callback handle types. More...
 
using FlushHandlerCallbackT = void(*)(void *user_data)
 Callback handle types. More...
 

Public Member Functions

 vtkBaseTypeMacro (vtkLogger, vtkObjectBase)
 
void PrintSelf (ostream &os, vtkIndent indent) override
 Methods invoked by print to print information about the object including superclasses. More...
 
- Public Member Functions inherited from vtkObjectBase
const char * GetClassName () const
 Return the class name as a string. More...
 
virtual vtkTypeBool IsA (const char *name)
 Return 1 if this class is the same type of (or a subclass of) the named class. More...
 
virtual void Delete ()
 Delete a VTK object. More...
 
virtual void FastDelete ()
 Delete a reference to this object. More...
 
void InitializeObjectBase ()
 
void Print (ostream &os)
 Print an object to an ostream. More...
 
virtual void Register (vtkObjectBase *o)
 Increase the reference count (mark as used by another object). More...
 
virtual void UnRegister (vtkObjectBase *o)
 Decrease the reference count (release by another object). More...
 
int GetReferenceCount ()
 Return the current reference count of this object. More...
 
void SetReferenceCount (int)
 Sets the reference count. More...
 
void PrintRevisions (ostream &)
 Legacy. More...
 
virtual void PrintHeader (ostream &os, vtkIndent indent)
 Methods invoked by print to print information about the object including superclasses. More...
 
virtual void PrintTrailer (ostream &os, vtkIndent indent)
 Methods invoked by print to print information about the object including superclasses. More...
 

Static Public Member Functions

static void SetStderrVerbosity (Verbosity level)
 Set the verbosity level for the output logged to stderr. More...
 
static void LogToFile (const char *path, FileMode filemode, Verbosity verbosity)
 Enable logging to a file at the given path. More...
 
static void EndLogToFile (const char *path)
 Stop logging to a file at the given path. More...
 
static std::string GetIdentifier (vtkObjectBase *obj)
 Returns a printable string for a vtkObjectBase instance. More...
 
static void AddCallback (const char *id, LogHandlerCallbackT callback, void *user_data, Verbosity verbosity, CloseHandlerCallbackT on_close=nullptr, FlushHandlerCallbackT on_flush=nullptr)
 Add a callback to call on each log message with a verbosity less or equal to the given one. More...
 
static bool RemoveCallback (const char *id)
 Remove a callback using the id specified. More...
 
static bool IsEnabled ()
 Returns true if VTK is built with logging support enabled. More...
 
static Verbosity GetCurrentVerbosityCutoff ()
 Returns the maximum verbosity of all log outputs. More...
 
static Verbosity ConvertToVerbosity (int value)
 Convenience function to convert an integer to matching verbosity level. More...
 
static Verbosity ConvertToVerbosity (const char *value)
 Convenience function to convert a string to matching verbosity level. More...
 
static void Init (int &argc, char *argv[], const char *verbosity_flag="-v")
 Initializes logging. More...
 
static void Init ()
 Initializes logging. More...
 
static void SetThreadName (const std::string &name)
 Get/Set the name to identify the current thread in the log output. More...
 
static std::string GetThreadName ()
 Get/Set the name to identify the current thread in the log output. More...
 
static void Log (Verbosity verbosity, const char *fname, unsigned int lineno, const char *txt)
 
static void StartScope (Verbosity verbosity, const char *id, const char *fname, unsigned int lineno)
 
static void EndScope (const char *id)
 
static void LogF (Verbosity verbosity, const char *fname, unsigned int lineno, VTK_FORMAT_STRING_TYPE format,...) VTK_PRINTF_LIKE(4
 
static void static void StartScopeF (Verbosity verbosity, const char *id, const char *fname, unsigned int lineno, VTK_FORMAT_STRING_TYPE format,...) VTK_PRINTF_LIKE(5
 
- Static Public Member Functions inherited from vtkObjectBase
static vtkTypeBool IsTypeOf (const char *name)
 Return 1 if this class type is the same type of (or a subclass of) the named class. More...
 
static vtkObjectBaseNew ()
 Create an object with Debug turned off, modified time initialized to zero, and reference counting on. More...
 

Protected Member Functions

 vtkLogger ()
 
 ~vtkLogger () override
 
- Protected Member Functions inherited from vtkObjectBase
 vtkObjectBase ()
 
virtual ~vtkObjectBase ()
 
virtual void CollectRevisions (ostream &)
 
virtual void RegisterInternal (vtkObjectBase *, vtkTypeBool check)
 
virtual void UnRegisterInternal (vtkObjectBase *, vtkTypeBool check)
 
virtual void ReportReferences (vtkGarbageCollector *)
 
 vtkObjectBase (const vtkObjectBase &)
 
void operator= (const vtkObjectBase &)
 

Additional Inherited Members

- Protected Attributes inherited from vtkObjectBase
vtkAtomicInt32 ReferenceCount
 
vtkWeakPointerBase ** WeakPointers
 

Detailed Description

logging framework for use in VTK and in applications based on VTK

vtkLogger acts as the entry point to VTK's logging framework. The implementation uses the loguru (https://github.com/emilk/loguru). vtkLogger provides some static API to initialize and configure logging together with a collection of macros that can be used to add items to the generated log.

The logging framework is based on verbosity levels. Level 0-9 are supported in addition to named levels such as ERROR, WARNING, and INFO. When a log for a particular verbosity level is being generated, all log additions issued with verbosity level less than or equal to the requested verbosity level will get logged.

When using any of the logging macros, it must be noted that unless a log output is requesting that verbosity provided (or higher), the call is a no-op and the message stream or printf-style arguments will not be evaluated.

Setup

To initialize logging, in your application's main() you may call vtkLogger::Init(argv, argc). This is totally optional but useful to time-stamp the start of the log. Furthermore, it can optionally detect verbosity level on the command line as -v (or any another string pass as the optional argument to Init) that will be used as the verbosity level for logging on to stderr. By default, it is set to 0 (or INFO) unless changed by calling vtkLogger::SetStderrVerbosity.

In additional to logging to stderr, one can accumulate logs to one or more files using vtkLogger::LogToFile. Each log file can be given its own verbosity level.

For multithreaded applications, you may want to name each of the threads so that the generated log can use human readable names for the threads. For that, use vtkLogger::SetThreadName. Calling vtkLogger::Init will set the name for the main thread.

Logging

vtkLogger provides several macros (again, based on loguru) that can be used to add the log. Both printf-style and stream-style is supported. All printf-style macros are suffixed with F to distinguish them from the stream macros. Another pattern in naming macros is the presence of V e.g. vtkVLog vs vtkLog. A macro with the V prefix takes a fully qualified verbosity enum e.g. vtkLogger::VERBOSITY_INFO or vtkLogger::VERBOSITY_0, while the non-V variant takes the verbosity name e.g. INFO or 0.

Following code snippet provides an overview of the available macros and their usage.

// Optional, but useful to time-stamp the start of the log.
// Will also detect verbosity level on the command line as -v.
vtkLogger::Init(argc, argv);
// Put every log message in "everything.log":
// Only log INFO, WARNING, ERROR to "latest_readable.log":
// Only show most relevant things on stderr:
// add a line to log using the verbosity name.
vtkLogF(INFO, "I'm hungry for some %.3f!", 3.14159);
vtkLogF(0, "same deal");
// add a line to log using the verbosity enum.
vtkVLogF(vtkLogger::VERBOSITY_INFO, "I'm hungry for some %.3f!", 3.14159);
// to add an identifier for a vtkObjectBase or subclass
vtkLogF(INFO, "The object is %s", vtkLogIdentifier(vtkobject));
// add a line conditionally to log if the condition succeeds:
vtkLogIfF(INFO, ptr == nullptr, "ptr is nullptr (some number: %.3f)", * 3.14159);
vtkLogScopeF(INFO, "Will indent all log messages within this scope.");
// in a function, you may use vtkLogScopeFunction(INFO)
// scope can be explicitly started and closed by vtkLogStartScope (or
// vtkLogStartScopef) and vtkLogEndScope
vtkLogStartScope(INFO, "id-used-as-message");
vtkLogStartScopeF(INFO, "id", "message-%d", 1);
vtkLogEndScope("id-used-as-message");
// alternatively, you can use streams instead of printf-style
vtkLog(INFO, "I'm hungry for some " << 3.14159 << "!");
vtkLogIF(INFO, ptr == nullptr, "ptr is " << "nullptr");

Logging and VTK error macros

VTK has long supported multiple macros to report errors, warnings and debug messages through vtkErrorMacro, vtkWarningMacro, vtkDebugMacro, etc. In addition to performing the traditional message reporting via vtkOutputWindow, these macros also log to the logging sub-system with appropriate verbosity levels.

To avoid the vtkLogger and vtkOutputWindow both posting the message to the standard output streams, vtkOutputWindow now supports an ability to specify terminal display mode, via vtkOutputWindow::SetDisplayMode. If display mode is vtkOutputWindow::DEFAULT then the output window will not post messages originating from the standard error/warning/debug macros to the standard output if VTK is built with logging support. If VTK is not built with logging support, then vtkOutputWindow will post the messages to the standard output streams, unless disabled explicitly.

Custom callbacks/handlers for log messages

vtkLogger supports ability to register callbacks to call on each logged message. This is useful to show the messages in application specific viewports, e.g. a special message widget.

To register a callback use vtkLogger::AddCallback and to remove a callback use vtkLogger::RemoveCallback with the id provided when registering the callback.

Definition at line 167 of file vtkLogger.h.

Member Typedef Documentation

using vtkLogger::LogHandlerCallbackT = void (*)(void* user_data, const Message& message)

Callback handle types.

Definition at line 309 of file vtkLogger.h.

using vtkLogger::CloseHandlerCallbackT = void (*)(void* user_data)

Callback handle types.

Definition at line 310 of file vtkLogger.h.

using vtkLogger::FlushHandlerCallbackT = void (*)(void* user_data)

Callback handle types.

Definition at line 311 of file vtkLogger.h.

Member Enumeration Documentation

Enumerator
VERBOSITY_INVALID 
VERBOSITY_OFF 
VERBOSITY_ERROR 
VERBOSITY_WARNING 
VERBOSITY_INFO 
VERBOSITY_0 
VERBOSITY_1 
VERBOSITY_2 
VERBOSITY_3 
VERBOSITY_4 
VERBOSITY_5 
VERBOSITY_6 
VERBOSITY_7 
VERBOSITY_8 
VERBOSITY_9 
VERBOSITY_TRACE 
VERBOSITY_MAX 

Definition at line 173 of file vtkLogger.h.

Support log file modes: TRUNCATE truncates the file clearing any existing contents while APPEND appends to the existing log file contents, if any.

Enumerator
TRUNCATE 
APPEND 

Definition at line 256 of file vtkLogger.h.

Constructor & Destructor Documentation

vtkLogger::vtkLogger ( )
protected
vtkLogger::~vtkLogger ( )
overrideprotected

Member Function Documentation

vtkLogger::vtkBaseTypeMacro ( vtkLogger  ,
vtkObjectBase   
)
void vtkLogger::PrintSelf ( ostream &  os,
vtkIndent  indent 
)
overridevirtual

Methods invoked by print to print information about the object including superclasses.

Typically not called by the user (use Print() instead) but used in the hierarchical print process to combine the output of several classes.

Reimplemented from vtkObjectBase.

static void vtkLogger::Init ( int argc,
char *  argv[],
const char *  verbosity_flag = "-v" 
)
static

Initializes logging.

This should be called from the main thread, if at all. Your application doesn't need to call this, but if you do:

  • signal handlers are installed
  • program arguments are logged
  • working directory is logged
  • optional -v verbosity flag is parsed
  • main thread name is set to "main thread"
  • explanation of the preamble (date, threadname, etc.) is logged.

This method will look for arguments meant for logging subsystem and remove them. Arguments meant for logging subsystem are:

-v n Set stderr logging verbosity. Examples -v 3 Show verbosity level 3 and lower. -v 0 Only show INFO, WARNING, ERROR, FATAL (default). -v INFO Only show INFO, WARNING, ERROR, FATAL (default). -v WARNING Only show WARNING, ERROR, FATAL. -v ERROR Only show ERROR, FATAL. -v FATAL Only show FATAL. -v OFF Turn off logging to stderr.

You can set the default logging verbosity programmatically by calling vtkLogger::SetStderrVerbosity before calling vtkLogger::Init. That way, you can specify a default that the user can override using command line arguments. Note that this does not affect file logging.

You can also use something else instead of '-v' flag by the via verbosity_flag argument. You can also set to nullptr to skip parsing verbosity level from the command line arguments.

static void vtkLogger::Init ( )
static

Initializes logging.

This should be called from the main thread, if at all. Your application doesn't need to call this, but if you do:

  • signal handlers are installed
  • program arguments are logged
  • working directory is logged
  • optional -v verbosity flag is parsed
  • main thread name is set to "main thread"
  • explanation of the preamble (date, threadname, etc.) is logged.

This method will look for arguments meant for logging subsystem and remove them. Arguments meant for logging subsystem are:

-v n Set stderr logging verbosity. Examples -v 3 Show verbosity level 3 and lower. -v 0 Only show INFO, WARNING, ERROR, FATAL (default). -v INFO Only show INFO, WARNING, ERROR, FATAL (default). -v WARNING Only show WARNING, ERROR, FATAL. -v ERROR Only show ERROR, FATAL. -v FATAL Only show FATAL. -v OFF Turn off logging to stderr.

You can set the default logging verbosity programmatically by calling vtkLogger::SetStderrVerbosity before calling vtkLogger::Init. That way, you can specify a default that the user can override using command line arguments. Note that this does not affect file logging.

You can also use something else instead of '-v' flag by the via verbosity_flag argument. You can also set to nullptr to skip parsing verbosity level from the command line arguments.

static void vtkLogger::SetStderrVerbosity ( Verbosity  level)
static

Set the verbosity level for the output logged to stderr.

Everything with a verbosity equal or less than the level specified will be written to stderr. Set to VERBOSITY_OFF to write nothing to stderr. Default is 0.

static void vtkLogger::LogToFile ( const char *  path,
FileMode  filemode,
Verbosity  verbosity 
)
static

Enable logging to a file at the given path.

Any logging message with verbosity lower or equal to the given verbosity will be included. This method will create all directories in the 'path' if needed. To stop the file logging, call EndLogToFile with the same path.

static void vtkLogger::EndLogToFile ( const char *  path)
static

Stop logging to a file at the given path.

static void vtkLogger::SetThreadName ( const std::string &  name)
static

Get/Set the name to identify the current thread in the log output.

static std::string vtkLogger::GetThreadName ( )
static

Get/Set the name to identify the current thread in the log output.

static std::string vtkLogger::GetIdentifier ( vtkObjectBase obj)
static

Returns a printable string for a vtkObjectBase instance.

static void vtkLogger::AddCallback ( const char *  id,
LogHandlerCallbackT  callback,
void *  user_data,
Verbosity  verbosity,
CloseHandlerCallbackT  on_close = nullptr,
FlushHandlerCallbackT  on_flush = nullptr 
)
static

Add a callback to call on each log message with a verbosity less or equal to the given one.

Useful for displaying messages in an application output window, for example. The given on_close is also expected to flush (if desired).

Note that if logging is disabled at compile time, then these callback will never be called.

static bool vtkLogger::RemoveCallback ( const char *  id)
static

Remove a callback using the id specified.

Returns true if and only if the callback was found (and removed).

static bool vtkLogger::IsEnabled ( )
static

Returns true if VTK is built with logging support enabled.

static Verbosity vtkLogger::GetCurrentVerbosityCutoff ( )
static

Returns the maximum verbosity of all log outputs.

A log item for a verbosity higher than this will not be generated in any of the currently active outputs.

static Verbosity vtkLogger::ConvertToVerbosity ( int  value)
static

Convenience function to convert an integer to matching verbosity level.

If val is less than or equal to vtkLogger::VERBOSITY_INVALID, then vtkLogger::VERBOSITY_INVALID is returned. If value is greater than vtkLogger::VERBOSITY_MAX, then vtkLogger::VERBOSITY_MAX is returned.

static Verbosity vtkLogger::ConvertToVerbosity ( const char *  value)
static

Convenience function to convert a string to matching verbosity level.

vtkLogger::VERBOSITY_INVALID will be return for invalid strings. Accepted string values are OFF, ERROR, WARNING, INFO, TRACE, MAX, INVALID or ASCII representation for an integer in the range [-9,9].

static void vtkLogger::Log ( Verbosity  verbosity,
const char *  fname,
unsigned int  lineno,
const char *  txt 
)
static
static void vtkLogger::StartScope ( Verbosity  verbosity,
const char *  id,
const char *  fname,
unsigned int  lineno 
)
static
static void vtkLogger::EndScope ( const char *  id)
static
static void vtkLogger::LogF ( Verbosity  verbosity,
const char *  fname,
unsigned int  lineno,
VTK_FORMAT_STRING_TYPE  format,
  ... 
)
static
static void static void vtkLogger::StartScopeF ( Verbosity  verbosity,
const char *  id,
const char *  fname,
unsigned int  lineno,
VTK_FORMAT_STRING_TYPE  format,
  ... 
)
static

The documentation for this class was generated from the following file: