CTL  0.6.1
Computed Tomography Library
Public Slots | Signals | Public Member Functions | Static Public Member Functions | Private Member Functions | Static Private Member Functions | Private Attributes | List of all members
CTL::MessageHandler Class Reference

The MessageHandler class provides a means to control the message output of the CTL and allows saving log files. More...

#include <messagehandler.h>

Inheritance diagram for CTL::MessageHandler:
Inheritance graph
[legend]
Collaboration diagram for CTL::MessageHandler:
Collaboration graph
[legend]

Public Slots

void messageFromSignal (QString msg)
 

Signals

void newLogEntry () const
 
void messagePrinted () const
 

Public Member Functions

 MessageHandler (const MessageHandler &)=delete
 
 MessageHandler (MessageHandler &&)=delete
 
MessageHandleroperator= (const MessageHandler &)=delete
 
MessageHandleroperator= (MessageHandler &&)=delete
 
const QStringList & log () const
 
QString lastMessage () const
 
void blacklistAll (bool blacklist=true)
 
template<class ... QString>
void blacklistClassOrFunction (const QString &... names)
 
template<class ... QString>
void blacklistFile (const QString &... names)
 
void blacklistMessageType (QtMsgType type)
 
void clearAllBlacklists ()
 
void clearAllWhitelists ()
 
void enforceLoggingOfBlacklistMsg (bool enabled=true)
 
void setLogFileName (const QString &fileName)
 
void squelch (bool enabled=true)
 
void setQuiet (bool enabled=true)
 
void toggleDateTag (bool show)
 
void toggleMessageOriginTag (bool show)
 
void toggleTimeTag (bool show)
 
void toggleTypeTag (bool show)
 
void toggleAllTags (bool show)
 
template<class ... QString>
void whitelistClassOrFunction (const QString &... names)
 
template<class ... QString>
void whitelistFile (const QString &... names)
 
bool writeLogFile () const
 
bool writeLogFileAndClear ()
 
void processMessage (QtMsgType type, const QMessageLogContext &context, const QString &msg)
 

Static Public Member Functions

static MessageHandlerinstance ()
 
static void qInstaller (QtMsgType type, const QMessageLogContext &context, const QString &msg)
 

Private Member Functions

bool isBlacklisted (QtMsgType type, const QMessageLogContext &context) const
 
bool isBlacklistedClassOrFct (const QMessageLogContext &context) const
 
bool isBlacklistedFile (const QMessageLogContext &context) const
 
bool isBlacklistedMessageType (QtMsgType type) const
 
bool isWhitelisted (const QMessageLogContext &context) const
 
bool isWhitelistedClassOrFct (const QMessageLogContext &context) const
 
bool isWhitelistedFile (const QMessageLogContext &context) const
 
QString dateTimeTag () const
 
void printMessage (const QString &finalMsg, QtMsgType type) const
 

Static Private Member Functions

static QString messageOriginString (const QMessageLogContext &context)
 
static QString typeTag (QtMsgType type)
 
static bool screenList (const QStringList &list, const char *str)
 

Private Attributes

QStringList _theLog
 
QString _logfileName = QStringLiteral("ctllog.txt")
 
bool _blacklistAll = false
 
bool _blacklistMsgType [5] = {}
 
bool _logBlacklistedMsg = false
 
bool _showDateTag = false
 
bool _showMsgOrig = false
 
bool _showTimeTag = true
 
bool _showTypeTag = true
 
bool _squelched = false
 
QStringList _blacklistClassFct
 
QStringList _blacklistFiles
 
QStringList _whitelistClassFct
 
QStringList _whitelistFiles
 

Detailed Description

The MessageHandler class provides a means to control the message output of the CTL and allows saving log files.

To use the MessageHandler in an application, install it using:

qInstallMessageHandler(MessageHandler::qInstaller);

The output can be configured using black- and whitelisting. For more details, see the corresponding methods:

To change settings, get the MessageHandler instance and call the corresponding methods on that object. Example:

Writing a log file can be initiated by writeLogFile(). The log file name can be set through setLogFileName().

Member Function Documentation

◆ blacklistAll()

void CTL::MessageHandler::blacklistAll ( bool  blacklist = true)

Blacklists all messages. Can be used in combination with whitelisting to filter individual messages.

Note that the effect of this cannot be reverted using clearAllBlacklists(), as no actual blacklist entries are created. Instead, call blacklistAll(false) to disable full blacklisting.

Example: suppress all message output except for messages from the CarmGantry class

// blacklist everything
// whitelist CarmGantry
See also
whitelistClassOrFunction(), whitelistFile().

◆ blacklistClassOrFunction()

template<class ... QString>
void CTL::MessageHandler::blacklistClassOrFunction ( const QString &...  names)

Adds all passed class and function names to the blacklist. You can pass an arbitrary number of names. There is no differentiation between class and function names, ie. any class or function name that matches a string passed to names will be blacklisted. Note that blacklist strings do not need to be complete; partial matches already lead to blacklisting of all corresponding entries.

Example:

// suppresses all messages coming from the CarmGantry class
MessageHandler::instance().blacklistClassOrFunction("CarmGantry", "GenericGantry", "TubularGantry");
// suppresses all messages coming from the classes CarmGantry, GenericGantry, and TubularGantry
// suppresses all messages coming from any class (and function) that contains "Gantry" in its name

Blacklisted messages will not be printed as console output. However, you can enforce logging of these messages to the log file by enabling enforceLoggingOfBlacklistMsg(), if necessary.

◆ blacklistFile()

template<class ... QString>
void CTL::MessageHandler::blacklistFile ( const QString &...  names)

Adds all passed file names to the blacklist. You can pass an arbitrary number of names. Note that blacklist strings do not need to be complete; partial matches already lead to blacklisting of all corresponding entries.

Example:

// suppresses all messages coming from anywhere within trajectories.cpp
// same as above (would also suppress files with other suffix)
// multiple input strings
MessageHandler::instance().blacklistFile("preparesteps.cpp", "preparationprotocols.cpp")
// suppresses all messages coming from the preparesteps.cpp and preparationprotocols.cpp files
// partial matching
// suppresses all messages coming from files that contain the "prep" substring;
// therefore, also suppresses preparesteps.cpp and preparationprotocols.cpp (like previous example)

Blacklisted messages will not be printed as console output. However, you can enforce logging of these messages to the log file by enabling enforceLoggingOfBlacklistMsg(), if necessary.

◆ blacklistMessageType()

void CTL::MessageHandler::blacklistMessageType ( QtMsgType  type)

Blacklists all messages of type type.

The following types are available:

  • QtDebugMsg
  • QtInfoMsg
  • QtWarningMsg
  • QtCriticalMsg
  • QtFatalMsg

◆ clearAllBlacklists()

void CTL::MessageHandler::clearAllBlacklists ( )

Removes all entries from the blacklists.

Note that this does not revert the effect of blacklistAll().

◆ clearAllWhitelists()

void CTL::MessageHandler::clearAllWhitelists ( )

Removes all entries from the whitelists.

◆ enforceLoggingOfBlacklistMsg()

void CTL::MessageHandler::enforceLoggingOfBlacklistMsg ( bool  enabled = true)

Enforces that messages from blacklisted origins will still be added to the log. Thus, they will be written to the log file and can also be retrieved using log() and lastMessage(), if required.

◆ instance()

MessageHandler & CTL::MessageHandler::instance ( )
static

Returns a reference to the singleton instance.

Use this method whenever you want to interact with the message handler.

◆ lastMessage()

QString CTL::MessageHandler::lastMessage ( ) const

Returns the last message as a QString.

Note that if logging of blacklisted messages is enabled (see enforceLoggingOfBlacklistMsg()), this can also return a blacklisted message (ie. a message that has not been printed to console output).

◆ log()

const QStringList & CTL::MessageHandler::log ( ) const

Returns the entire log as a QStringList (each list entry refers to a single message).

Note that if logging of blacklisted messages is enabled (see enforceLoggingOfBlacklistMsg()), this also contains all blacklisted messages.

◆ messageFromSignal

void CTL::MessageHandler::messageFromSignal ( QString  msg)
slot

Slot that can be used to connect signals from external classes that emit a QString to process it as a QtInfoMsg.

◆ qInstaller()

void CTL::MessageHandler::qInstaller ( QtMsgType  type,
const QMessageLogContext &  context,
const QString &  msg 
)
static

Function that needs to be passed to qInstallMessageHandler() in order to let this message handler taking care of the Qt message streams.

Usage:

qInstallMessageHandler(MessageHandler::qInstaller);

◆ setLogFileName()

void CTL::MessageHandler::setLogFileName ( const QString &  fileName)

Sets the file name used for saving the log file to fileName.

Note that the file needs to be writable in order to successfully create the log file.

◆ setQuiet()

void CTL::MessageHandler::setQuiet ( bool  enabled = true)

Sets use of quiet mode to enabled. In quiet mode, no messages are printed to console output.

◆ squelch()

void CTL::MessageHandler::squelch ( bool  enabled = true)

Sets use of quiet mode to enabled. In quiet mode, no messages are printed to console output.

Same as setQuiet().

◆ toggleAllTags()

void CTL::MessageHandler::toggleAllTags ( bool  show)

Sets the inclusion of all message tags into message strings to show.

See also
toggleDateTag(), toggleTimeTag(), toggleTypeTag(), toggleMessageOriginTag().

◆ toggleDateTag()

void CTL::MessageHandler::toggleDateTag ( bool  show)

Sets the inclusion of the date tag into message strings to show.

◆ toggleMessageOriginTag()

void CTL::MessageHandler::toggleMessageOriginTag ( bool  show)

Sets the inclusion of the message origin tag into message strings to show.

◆ toggleTimeTag()

void CTL::MessageHandler::toggleTimeTag ( bool  show)

Sets the inclusion of the time tag into message strings to show.

◆ toggleTypeTag()

void CTL::MessageHandler::toggleTypeTag ( bool  show)

Sets the inclusion of the type tag into message strings to show.

◆ whitelistClassOrFunction()

template<class ... QString>
void CTL::MessageHandler::whitelistClassOrFunction ( const QString &...  names)

Adds all passed class and function names to the whitelist. You can pass an arbitrary number of names. There is no differentiation between class and function names, ie. any class or function name that matches a string passed to names will be whitelisted. Note that whitelist strings do not need to be complete; partial matches already lead to whitelisting of all corresponding entries.

Whitelisted messages ignore all blacklist entries, meaning they will always be printed as console output.

Whitelisting is only meaningful in combination with previous blacklisting.

Example: suppress all message output except for messages from the CarmGantry class

// blacklist everything
// whitelist CarmGantry

Example 2: suppress all message output except for messages from any "Gantry" class

// blacklist everything
// whitelist all classes containing the "Gantry" substring

◆ whitelistFile()

template<class ... QString>
void CTL::MessageHandler::whitelistFile ( const QString &...  names)

Adds all passed file names to the whitelist. You can pass an arbitrary number of names. Note that whitelist strings do not need to be complete; partial matches already lead to whitelisting of all corresponding entries.

Whitelisted messages ignore all blacklist entries, meaning they will always be printed as console output.

Whitelisting is only meaningful in combination with previous blacklisting.

Example: suppress all message output except for messages from the trajectories.cpp file

// blacklist everything
// whitelist file "trajectories.cpp"

◆ writeLogFile()

bool CTL::MessageHandler::writeLogFile ( ) const

Initiates writing of the log file to the file name specified by setLogFileName().

Returns true on a successful write.

◆ writeLogFileAndClear()

bool CTL::MessageHandler::writeLogFileAndClear ( )

Same as writeLogFile() but also clears the current log after a successful write.


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