VTK  9.1.0
Classes | Public Types | Public Member Functions | Static Public Member Functions | Static Public Attributes | 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 vtkIdType GetNumberOfGenerationsFromBase (const char *name)
 Given the name of a base class of this class type, return the distance of inheritance between this class type and the named class (how many generations of inheritance are there between this class and 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...
 
bool GetIsInMemkind () const
 A local state flag that remembers whether this object lives in the normal or extended memory space. 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 SetInternalVerbosityLevel (Verbosity level)
 Set internal messages verbosity level. 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 *text)
 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, VTK_FILEPATH const char *fname, unsigned int lineno, const char *txt)
 
static void StartScope (Verbosity verbosity, const char *id, VTK_FILEPATH const char *fname, unsigned int lineno)
 
static void EndScope (const char *id)
 
static void LogF (Verbosity verbosity, VTK_FILEPATH 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, VTK_FILEPATH 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 vtkIdType GetNumberOfGenerationsFromBaseType (const char *name)
 Given a the name of a base class of this class type, return the distance of inheritance between this class type and the named class (how many generations of inheritance are there between this class and the named class). More...
 
static vtkObjectBaseNew ()
 Create an object with Debug turned off, modified time initialized to zero, and reference counting on. More...
 
static void SetMemkindDirectory (const char *directoryname)
 The name of a directory, ideally mounted -o dax, to memory map an extended memory space within. More...
 
static bool GetUsingMemkind ()
 A global state flag that controls whether vtkObjects are constructed in the usual way (the default) or within the extended memory space. More...
 

Static Public Attributes

static bool EnableUnsafeSignalHandler
 Flag to enable/disable the logging frameworks printing of a stack trace when catching signals, which could lead to crashes and deadlocks in certain circumstances. More...
 

Protected Member Functions

 vtkLogger ()
 
 ~vtkLogger () override
 
- Protected Member Functions inherited from vtkObjectBase
 vtkObjectBase ()
 
virtual ~vtkObjectBase ()
 
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

- Static Protected Member Functions inherited from vtkObjectBase
static vtkMallocingFunction GetCurrentMallocFunction ()
 
static vtkReallocingFunction GetCurrentReallocFunction ()
 
static vtkFreeingFunction GetCurrentFreeFunction ()
 
static vtkFreeingFunction GetAlternateFreeFunction ()
 
- Protected Attributes inherited from vtkObjectBase
std::atomic< int32_t > 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.

To prevent the logging framework from intercepting signals from your application, you can set the static variable vtkLogger::EnableUnsafeSignalHandler to false prior to calling vtkLogger::Init(argc, argv) or vtkLogger::Init().

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, leaving this as the default value `true` will let the logging
// framework log signals such as segmentation faults.
// 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");
static bool EnableUnsafeSignalHandler
Flag to enable/disable the logging frameworks printing of a stack trace when catching signals,...
Definition: vtkLogger.h:452
@ VERBOSITY_0
Definition: vtkLogger.h:217
@ VERBOSITY_1
Definition: vtkLogger.h:220
@ VERBOSITY_MAX
Definition: vtkLogger.h:234
@ VERBOSITY_INFO
Definition: vtkLogger.h:214
static void Init()
Initializes logging.
static void SetStderrVerbosity(Verbosity level)
Set the verbosity level for the output logged to stderr.
static void LogToFile(const char *path, FileMode filemode, Verbosity verbosity)
Enable logging to a file at the given path.
#define vtkLogF(verbosity_name,...)
Add to log given the verbosity level.
Definition: vtkLogger.h:484
#define vtkLogEndScope(id)
Explicitly mark start and end of log scope.
Definition: vtkLogger.h:554
#define vtkLog(verbosity_name, x)
Add to log given the verbosity level.
Definition: vtkLogger.h:495
#define vtkLogScopeF(verbosity_name,...)
Definition: vtkLogger.h:541
#define vtkLogStartScopeF(verbosity_name, id,...)
Explicitly mark start and end of log scope.
Definition: vtkLogger.h:556
#define vtkLogStartScope(verbosity_name, id)
Explicitly mark start and end of log scope.
Definition: vtkLogger.h:552
#define vtkLogIfF(verbosity_name, cond,...)
Add to log only when the cond passes.
Definition: vtkLogger.h:516
#define vtkLogIdentifier(vtkobject)
Convenience macro to generate an identifier string for any vtkObjectBase subclass.
Definition: vtkLogger.h:569
#define vtkVLogF(level,...)
Add to log given the verbosity level.
Definition: vtkLogger.h:480

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.

Online Examples:

Definition at line 196 of file vtkLogger.h.

Member Typedef Documentation

◆ LogHandlerCallbackT

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

Callback handle types.

Definition at line 351 of file vtkLogger.h.

◆ CloseHandlerCallbackT

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

Callback handle types.

Definition at line 352 of file vtkLogger.h.

◆ FlushHandlerCallbackT

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

Callback handle types.

Definition at line 353 of file vtkLogger.h.

Member Enumeration Documentation

◆ Verbosity

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 202 of file vtkLogger.h.

◆ FileMode

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 298 of file vtkLogger.h.

Constructor & Destructor Documentation

◆ vtkLogger()

vtkLogger::vtkLogger ( )
protected

◆ ~vtkLogger()

vtkLogger::~vtkLogger ( )
overrideprotected

Member Function Documentation

◆ vtkBaseTypeMacro()

vtkLogger::vtkBaseTypeMacro ( vtkLogger  ,
vtkObjectBase   
)

◆ PrintSelf()

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.

◆ Init() [1/2]

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.

For applications that do not want loguru to handle any signals, i.e., print a stack trace when a signal is intercepted, the vtkLogger::EnableUnsafeSignalHandler static member variable should be set to false.

◆ Init() [2/2]

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.

For applications that do not want loguru to handle any signals, i.e., print a stack trace when a signal is intercepted, the vtkLogger::EnableUnsafeSignalHandler static member variable should be set to false.

◆ SetStderrVerbosity()

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.

◆ SetInternalVerbosityLevel()

static void vtkLogger::SetInternalVerbosityLevel ( Verbosity  level)
static

Set internal messages verbosity level.

The library used by VTK, loguru generates log messages during initialization and at exit. These are logged as log level VERBOSITY_1, by default. One can change that using this method. Typically, you want to call this before vtkLogger::Init.

◆ LogToFile()

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.

◆ EndLogToFile()

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

Stop logging to a file at the given path.

◆ SetThreadName()

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

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

◆ GetThreadName()

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

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

◆ GetIdentifier()

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

Returns a printable string for a vtkObjectBase instance.

◆ AddCallback()

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.

◆ RemoveCallback()

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

◆ IsEnabled()

static bool vtkLogger::IsEnabled ( )
static

Returns true if VTK is built with logging support enabled.

◆ GetCurrentVerbosityCutoff()

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.

◆ ConvertToVerbosity() [1/2]

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.

◆ ConvertToVerbosity() [2/2]

static Verbosity vtkLogger::ConvertToVerbosity ( const char *  text)
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].

◆ Log()

static void vtkLogger::Log ( Verbosity  verbosity,
VTK_FILEPATH const char *  fname,
unsigned int  lineno,
const char *  txt 
)
static

◆ StartScope()

static void vtkLogger::StartScope ( Verbosity  verbosity,
const char *  id,
VTK_FILEPATH const char *  fname,
unsigned int  lineno 
)
static

◆ EndScope()

static void vtkLogger::EndScope ( const char *  id)
static

◆ LogF()

static void vtkLogger::LogF ( Verbosity  verbosity,
VTK_FILEPATH const char *  fname,
unsigned int  lineno,
VTK_FORMAT_STRING_TYPE  format,
  ... 
)
static

◆ StartScopeF()

static void static void vtkLogger::StartScopeF ( Verbosity  verbosity,
const char *  id,
VTK_FILEPATH const char *  fname,
unsigned int  lineno,
VTK_FORMAT_STRING_TYPE  format,
  ... 
)
static

Member Data Documentation

◆ EnableUnsafeSignalHandler

bool vtkLogger::EnableUnsafeSignalHandler
static

Flag to enable/disable the logging frameworks printing of a stack trace when catching signals, which could lead to crashes and deadlocks in certain circumstances.

Definition at line 452 of file vtkLogger.h.


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