NuriaProject Framework  0.1
The NuriaProject Framework
Public Types | Public Member Functions | Static Public Member Functions | List of all members
Nuria::Debug Class Reference

Debugging and logging class of the Nuria Framework. More...

#include <debug.hpp>

Inheritance diagram for Nuria::Debug:

Public Types

enum  Type {
  DebugMsg = 0, LogMsg = 1, WarnMsg = 2, ErrorMsg = 3,
  CriticalMsg = 4, AllLevels = CriticalMsg + 1, DefaultLowestMsgLevel = DebugMsg
}
 

Public Member Functions

 Debug (Type type, const char *module, const char *fileName, int line, const char *className, const char *methodName)
 
 ~Debug ()
 

Static Public Member Functions

static void installMessageHandler ()
 
static void installOutputHandler (const Callback &callback)
 
static bool isModuleDisabled (const char *module, Type level)
 
static bool isModuleDisabled (uint32_t module, Type level)
 
static bool isOutputDisabled ()
 
static void qtMessageHandler (QtMsgType type, const QMessageLogContext &context, const QString &message)
 
static void setDestination (FILE *handle)
 
static void setDestination (QIODevice *device)
 
static void setModuleLevel (const char *module, Type leastLevel)
 
static void setOutputDisabled (bool disabled)
 
static void setOutputFormat (const char *format)
 
static void uninstallOutputHandler (const Callback &callback)
 

Detailed Description

Debugging and logging class of the Nuria Framework.

This class provides an easy way for outputting useful logging messages. You can use this as a drop-in replacement for QDebug.

Defining NURIA_DEBUG_NO_{DEBUG,LOG,WARN,ERROR,CRITICAL} will disable the level at compile-time, causing the corresponding logging macro to create code the compiler can optimize out.

Redirecting output
By default all output is sent to stdout.

You can redirect it using setDestination(). Use installOutputHandler() to redirect logging data to a method of yours instead.

QDebug and Nuria::Debug
Nuria::Debug is meant as drop-in replacement for QDebug. You can still use qDebug(), qWarning(), ... directly but still let Nuria::Debug do the work.
See also
qtMessageHandler
Module
Nuria::Debug knows the concept of "modules". A message can be in a single module, which is printed next to the message itself. Additionally, you can use enableModule and disableModule to change the logging behaviour at run-time.

The module name of a translation unit can be set by #defining NURIA_MODULE. The value of it is expected to be a literal. The line containing the #define must appear before the line where debug.hpp is #include'd.

#define NURIA_MODULE "ModuleName"
#include <nuria/debug.hpp>
Note
The default module name is "" (Empty string) which is used if you don't provide a value on your own. No warning is issued.
Example
// Before including debug.hpp
#define NURIA_MODULE "Example"
//
nDebug() << "Hello, this is a debug message";
nLog() << "Something worth logging happend!";
nWarn() << "I'm a warning";
nError() << "Some error occured :(";
nCritical() << "Some unrecoverable error occured.";
Outputting custom types
If you want to output custom types you simply overload operator<< for QDebug:
QDebug operator<< (QDebug out, const MyType &instance) {
out.nospace () << "(" << instance.name () << ")";
}
Note
For a more complete example read this

Member Enumeration Documentation

Type enumeration.

Constructor & Destructor Documentation

Nuria::Debug::Debug ( Type  type,
const char *  module,
const char *  fileName,
int  line,
const char *  className,
const char *  methodName 
)

Constructor. You usually don't use this directly. Use nDebug, nWarn, nError, nCritical or nLog instead.

Nuria::Debug::~Debug ( )

Destructor. Writes the debug data to the output stream.

Member Function Documentation

static void Nuria::Debug::installMessageHandler ( )
static

Install the QDebug message handler.

static void Nuria::Debug::installOutputHandler ( const Callback callback)
static

Installs an output handler. A output handler is called each time the logging interface is used. You can use this if you need more control over logging in your application, for example if a single output file isn't enough for you.

The prototype for the callback looks like this:

void (Nuria::Debug::Type type, QByteArray typeName,
QByteArray moduleName, QByteArray file, int line,
QByteArray className, QByteArray methodName,
QString message);
Parameters
typeThe type of this message
typeNameA human-readable representation of type
moduleNameThe module name
fileThe file name (As generated by the FILE macro)
lineThe line number (As generated by the LINE macro)
classNameThe name of the class that sent the message
methodNameThe method name which sent the message
messageThe message itself
See also
uninstallOutputHandler
static bool Nuria::Debug::isModuleDisabled ( const char *  module,
Type  level 
)
static

Returns true if module at level is disabled.

static bool Nuria::Debug::isModuleDisabled ( uint32_t  module,
Type  level 
)
inlinestatic

Fast access for logging macros.

static bool Nuria::Debug::isOutputDisabled ( )
static

Returns true if the default output is disabled (as in not used).

static void Nuria::Debug::qtMessageHandler ( QtMsgType  type,
const QMessageLogContext &  context,
const QString &  message 
)
static

Use this function in combination with qInstallMsgHandler to tunnel all QDebug data through Debug. This way legacy code which uses qDebug() etc. will still use the log paths you defined.

With Qt5 this message handler also supports output of file name, line numbers and method names in debug mode. These will not be available in release mode.

See also
installMessageHandler
static void Nuria::Debug::setDestination ( FILE *  handle)
static

Use this method to redirect the logging output to some other destination. By default stdout is used.

Note
Debug takes ownership of handle.
static void Nuria::Debug::setDestination ( QIODevice *  device)
static

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Uses device for output.

Note
Debug takes ownership of device.
static void Nuria::Debug::setModuleLevel ( const char *  module,
Type  leastLevel 
)
static

Sets the logging level of a certain module. leastLevel is non-inclusive, which means that passing LogMsg will output for everything below that in module. To completely disable logging, pass AllLevels.

Passing nullptr for module acts as a wildcard, affecting all modules.

See also
enableCategory
static void Nuria::Debug::setOutputDisabled ( bool  disabled)
static

Use this method to disable or enable the default output method. By default the default output method is enabled.

Note
This only affects the usage of the default output which you can manipulate through setDestination. It does not disable your output handlers.
static void Nuria::Debug::setOutputFormat ( const char *  format)
static

Sets the format which is used to write a message into the output stream. If format is 0 the default format will be used.

Note
The default output format is: "[%TIME%] %TYPE%/%MODULE%: %FILE%:%LINE% - %CLASS%::%METHOD%: %BODY%"
Identifiers
  • DATE% The current date (MM/DD/YYYY)
  • TIME% The current time (HH:MM:SS)
  • TYPE% The message type ("Debug", "Warning", ...)
  • MODULE% The module name
  • FILE% The source file name
  • LINE% The line number inside FILE%
  • CLASS% Name of the class
  • METHOD% Method which sent the message
  • BODY% The message body
Note
Identifiers are case-sensitive.

A new-line character is automatically appended.

static void Nuria::Debug::uninstallOutputHandler ( const Callback callback)
static

Removes an output handler.


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