Workspace 6.21.5
Classes | Public Member Functions | Static Public Member Functions | Protected Member Functions | List of all members
LogManager Class Reference

Singleton for managing the logging streams associated with execution threads.

#include <Workspace/Application/logmanager.h>

Classes

class  FilterToken
 Represents the life span of a filter. When it is created, the filter is added to the associated LogManager. When it is deleted, the filter is removed from the LogManager. More...
 
class  MessageCategory
 

Public Member Functions

bool addMessageCategory (const MessageCategory &category)
 
void addStream (DataExecution::LogStream &logStream)
 
bool canCaptureStandardOutput () const
 
void enableStandardOutputStream (bool enabled)
 
std::unique_ptr< FilterTokenfilterMessages (const MessageCategory &categoryToSuppress)
 
std::unique_ptr< FilterTokenfilterMessages (const QSet< MessageCategory > &categoriesToSuppress)
 
QStringList getMessageCategories () const
 
const MessageCategorygetMessageCategory (const QString &category) const
 
QList< const DataExecution::LogStream * > getStreams () const
 
bool hasMessageCategory (const QString &category) const
 
bool isCapturingStandardOutput () const
 
bool isMessageCategoryEnabled (const QString &category) const
 
bool removeStream (DataExecution::LogStream &logStream)
 
void setDefaultMessageCategory (const QString &category)
 
void setGlobalMessagePrefix (const QString &prefix)
 }
 
void setMessageCategoryEnabled (const QString &category, bool enabled)
 

Static Public Member Functions

static void flush ()
 
static void flushAsync ()
 
static LogManagergetInstance ()
 
static void logLine (const MessageCategory &category, const QString &msg)
 
static void logLine (const QString &categoryName, const QString &msg)
 
static void logLine (const QString &msg)
 
static void logLineAsync (const MessageCategory &category, const QString &msg)
 
static void logLineAsync (const QString &categoryName, const QString &msg)
 
static void logLineAsync (const QString &msg)
 
static void logText (const MessageCategory &category, const QString &msg)
 
static void logText (const QString &categoryName, const QString &msg)
 
static void logText (const QString &msg)
 
static void logTextAsync (const MessageCategory &category, const QString &msg)
 
static void logTextAsync (const QString &categoryName, const QString &msg)
 
static void logTextAsync (const QString &msg)
 

Protected Member Functions

void installFilter (const DataExecution::LogStreamFilter &filter)
 
void removeFilter (const DataExecution::LogStreamFilter &filter)
 

Member Function Documentation

◆ addMessageCategory()

bool addMessageCategory ( const MessageCategory category)
Parameters
categoryThe new message category to add to the LogManager
Returns
true if the category was added successfully, false if a category with the same name already exists. An error message will also be logged.

The log manager allows us to classify messages into categories. When using the logText function, we can specify a category name. If this category is exists and is enabled, the message will be logged. In addition, a prefix and suffix will be added to the message if one is specified.

Note
The log manager adds a set of default categories which can be used.

◆ addStream()

void addStream ( DataExecution::LogStream logStream)
Parameters
logStreamThe LogStream object to use as the main stream for the application. It is the caller's responsibility to ensure that logStream survives for the life of the application or until the stream is popped off the main log stream stack through a call to popMainLogStream().
Warning
This function should only be called at a time when it can be guaranteed that nothing will try to write to the main log stream. There are no protections against race conditions when pushing or popping the log stream stack.
See also
removeStream()

◆ canCaptureStandardOutput()

bool canCaptureStandardOutput ( ) const
Returns
True if LogStream subclasses are allowed to capture standard output. In practice, this amounts to testing whether or not the user has defined the WORKSPACE_PREVENT_LOG_CAPTURE environment variable.
See also
isCapturingStandardOutput()

◆ enableStandardOutputStream()

void enableStandardOutputStream ( bool  enabled)
Parameters
enabledEnable a standard output regard less of output stream

By default, log messages always forward to standard output (or error if capturing standard output) When an application would like to disable standard output and attach its own logStream, call this to disable standard output (or error).

Note, if no logStream attached to LogManager, all log text are discarded.

See also
addStream()

◆ filterMessages() [1/2]

std::unique_ptr< LogManager::FilterToken > filterMessages ( const MessageCategory category)
Parameters
categoryThe category of log message to suppress.
Returns
A pointer to a FilterToken object. Once this filter goes out of scope, it will no longer apply.

Enables message filtering while the FilterToken object that it returns remains alive. Log messages that match the specified category will be suppressed (ignored) - they will not appear in any log sinks.

Note
any threads of execution that are spawned from the same thread that the filter exists in will have the same filter applied to them; the assumption here being that any filtering applies to anything going on in the code within this scope.

See test_logstreamfilter for examples of how the filtering works with respect to threads of execution.

◆ filterMessages() [2/2]

std::unique_ptr< LogManager::FilterToken > filterMessages ( const QSet< MessageCategory > &  categories)
Parameters
categoriesThe categories of log message to suppress.
Returns
A pointer to a FilterToken object. Once this filter goes out of scope, it will no longer apply.

Enables message filtering while the FilterToken object that it returns remains alive. Log messages that match the specified categories will be suppressed (ignored) - they will not appear in any log sinks.

Note
any threads of execution that are spawned from the same thread that the filter exists in will have the same filter applied to them; the assumption here being that any filtering applies to anything going on in the code within this scope.

See test_logstreamfilter for examples of how the filtering works with respect to threads of execution.

◆ flush()

void flush ( )
static

Causes the current log stream to be flushed.

◆ flushAsync()

void flushAsync ( )
static

Causes the current log stream to be flushed.

◆ getInstance()

LogManager & getInstance ( )
static
Returns
The LogManager singleton instance used by the application.

◆ getMessageCategories()

QStringList getMessageCategories ( ) const
Returns
A list of all the categories registered with the log manager.

◆ getMessageCategory()

const LogManager::MessageCategory * getMessageCategory ( const QString &  category) const
Parameters
categoryThe name of the category to retrieve.
Returns
A pointer to the category with the specified name, 0 if a category with this name does not exist.

◆ getStreams()

QList< const DataExecution::LogStream * > getStreams ( ) const
Returns
Read only list of current log streams

◆ hasMessageCategory()

bool hasMessageCategory ( const QString &  category) const
Parameters
categoryThe category we are testing.
Returns
true if the category exists, false otherwise

Checks whether the log manager has knowledge of a specific category.

See also
logText()

◆ installFilter()

void installFilter ( const DataExecution::LogStreamFilter filter)
protected
Parameters
filterThe LogStreamFilter object that should be installed in the LogManager.

Installs a LogStreamFilter in the LogManager. Once a LogStreamFilter is installed, any messages that match the categories of the filter will be suppressed from the log. If, while the filter still lives, additional threads of execution are spawned from scope in which the filter was created, those 'child' threads will also be affected by this filter.

Only FilterToken objects are capable of installing a LogStreamFilter to a LogManager.

◆ isCapturingStandardOutput()

bool isCapturingStandardOutput ( ) const
Returns
True if all output is being captured. Note that once this has been turned on, it cannot be turned off for the rest of the application because the original output streams cannot be restored.
See also
canCaptureStdOutput()

◆ isMessageCategoryEnabled()

bool isMessageCategoryEnabled ( const QString &  category) const
Parameters
categoryThe category to check
See also
logText() setMessageCategoryEnabled()

◆ logLine() [1/3]

void logLine ( const MessageCategory category,
const QString &  msg 
)
static
Parameters
categoryThe category associated with this message. If the category name is not registered with the LogManager (see setMessageCategoryEnabled) or the category is disabled, the message will not be logged.
msgThe content of the message to log.

The same as logText but a newline character is appended to the message.

◆ logLine() [2/3]

void logLine ( const QString &  categoryName,
const QString &  msg 
)
static
Parameters
categoryNameThe category name associated with this message. If the category name is not registered with the LogManager (see setMessageCategoryEnabled) or the category is disabled, the message will not be logged.
msgThe content of the message to log.

The same as logText but a newline character is appended to the message.

◆ logLine() [3/3]

void logLine ( const QString &  msg)
static
Parameters
msgThe text message to send to the appropriate log stream.

The same as logText but a newline character is appended to the message.

◆ logLineAsync() [1/3]

void logLineAsync ( const MessageCategory category,
const QString &  msg 
)
static
Parameters
categoryThe category associated with this message. If the category name is not registered with the LogManager (see setMessageCategoryEnabled) or the category is disabled, the message will not be logged.
msgThe content of the message to log.

The same as logText but a newline character is appended to the message.

◆ logLineAsync() [2/3]

void logLineAsync ( const QString &  categoryName,
const QString &  msg 
)
static
Parameters
categoryNameThe category name associated with this message. If the category name is not registered with the LogManager (see setMessageCategoryEnabled) or the category is disabled, the message will not be logged.
msgThe content of the message to log.

The same as logText but a newline character is appended to the message.

◆ logLineAsync() [3/3]

void logLineAsync ( const QString &  msg)
static
Parameters
msgThe text message to send to the appropriate log stream.

The same as logText but a newline character is appended to the message.

◆ logText() [1/3]

void logText ( const MessageCategory category,
const QString &  msg 
)
static
Parameters
categoryThe category associated with this message. If the category name is not registered with the LogManager (see setMessageCategoryEnabled) or the category is disabled, the message will not be logged.
msgThe content of the message to log.

◆ logText() [2/3]

void logText ( const QString &  categoryName,
const QString &  msg 
)
static
Parameters
categoryNameThe category name associated with this message. If the category name is not registered with the LogManager (see setMessageCategoryEnabled) or the category is disabled, the message will not be logged.
msgThe content of the message to log.

◆ logText() [3/3]

void logText ( const QString &  msg)
static
Parameters
msgThe text message to send to the appropriate log stream.

This function is a convenience. Client code can call it with text to be logged and it will be sent to the appropriate log stream for the calling thread. It has one main advantage over simply using std::cout and that is that it will send msg to the appropriate place potentially more quickly because it bypasses the capturing of stdout (which happens in a separate thread). It is recommended that you only call logText() when you really want the text logged as early as possible. Its use can potentially result in out-of-order text appearing in the log if an earlier output to std::cout is yet to be delivered.

◆ logTextAsync() [1/3]

void logTextAsync ( const MessageCategory category,
const QString &  msg 
)
static
Parameters
categoryThe category associated with this message. If the category name is not registered with the LogManager (see setMessageCategoryEnabled) or the category is disabled, the message will not be logged.
msgThe content of the message to log.

◆ logTextAsync() [2/3]

void logTextAsync ( const QString &  categoryName,
const QString &  msg 
)
static
Parameters
categoryNameThe category name associated with this message. If the category name is not registered with the LogManager (see setMessageCategoryEnabled) or the category is disabled, the message will not be logged.
msgThe content of the message to log.

Writes to the log and doesn't block the calling thread while waiting for it to happen.

◆ logTextAsync() [3/3]

void logTextAsync ( const QString &  msg)
static

{ Set of async versions of the log text for when we don't care about the results being in order and so don't care to block the calling thread.

Parameters
msgThe text message to send to the appropriate log stream. This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Writes to the log and doesn't block the calling thread while waiting for it to happen.

◆ removeFilter()

void removeFilter ( const DataExecution::LogStreamFilter filter)
protected
Parameters
filterThe LogStreamFilter object to be removed from the LogManager.

Removes a LogStreamFilter from the LogManager. Once the filter is removed, it will no longer suppress any log messages on that thread of execution (or any 'child' threads that were spawned after the filter was originally created).

Only FilterToken objects are capable of removing a LogStreamFilter from a LogManager.

◆ removeStream()

bool removeStream ( DataExecution::LogStream logStream)
Parameters
logStreamThe log stream to pop off the stack. Normally, it will be a null pointer, which means pop the last item off the stack, but in some cases a specific item needs to be removed from the stack which might not be at the top. It is okay to remove an item that is not on the top, but be aware that the main log is then increasingly likely to be getting redirected to unexpected places.
Warning
This function should only be called at a time when it can be guaranteed that nothing will try to write to the main log stream. There are no protections against race conditions when pushing or popping the log stream stack.
Returns
True if the logStream exisited in the list and has been removed.
See also
addStream()

◆ setDefaultMessageCategory()

void setDefaultMessageCategory ( const QString &  category)
Parameters
categoryThe message category to set as a default. By default, this is not used, so all messages will be logged with no category.

◆ setGlobalMessagePrefix()

void setGlobalMessagePrefix ( const QString &  prefix)

◆ setMessageCategoryEnabled()

void setMessageCategoryEnabled ( const QString &  category,
bool  enabled 
)
Parameters
categoryThe category to enable / disable
enabledThe desired enabled state of the category

Enables / disables a specific category name in the LogManager. The LogManager uses categories with the LogText call to "tag" specific text with a certain category. By disabling category, log messages associated with that category will not be displayed.

See also
logText() isMessageCategoryEnabled()