This class is a log utility.
The aims of the log in CamiTK are:
- to simplify and give a flexible way to log message in any application
- to be minimally invasive in the source code
- to help bug tracking and report
- to help new development by providing automatically generated debugging information
- to suppress any need to use qDebug macro or std::cout / std::cerr "printf"
There are four types of message (
- See also
- InterfaceLogger):
- ERROR error messages should be used only when a problem occurs that
- cannot be taken care of by the source code
- imply a preventive crash action
- might result in unfinished process / undefined behaviour
- WARNING warning messages should be used when something went wrong but is not critical to the application state but might require some corrective or alternative action by the user
- INFO: information messages should be used to show normal processing, stage or intermediate step or display some useful data values
- TRACE: trace messages should be used to trace anything useful for debugging or log post-processing
These messages are written using the corresponding CAMITK log macros: CAMITK_ERROR, CAMITK_WARNING, CAMITK_INFO and CAMITK_TRACE.
For static methods and non QObject class, use the ALT CAMITK log macros: CAMITK_ERROR_ALT, CAMITK_WARNING_ALT, CAMITK_INFO_ALT and CAMITK_TRACE_ALT.
There are also specific IF CAMITK macros use to log only if a given boolean expression is true: CAMITK_ERROR_IF, CAMITK_WARNING_IF, CAMITK_INFO_IF and CAMITK_TRACE_IF.
And their corresponding counterparts for static methods or non QObject class: CAMITK_ERROR_IF_ALT, CAMITK_WARNING_IF_ALT, CAMITK_INFO_IF_ALT and CAMITK_TRACE_IF_ALT.
Depending on the current application logger log level, not all the messages are displayed.
A default logger is instantiated for all application using the CamiTKLogger class
- See also
- CamiTKLogger
The current log level can be set to
- NONE in this case, no message are logged. Logger is completely silent
- ERROR only error messages are logged
- WARNING only WARNING and ERROR messages are logged
- INFO INFO, WARNING and ERROR messages are logged
- TRACE all types of messages are logged
A message box level is also available: any message equals or above the message box level will be shown in a modal QMessageBox dialog.
Debug information are also available: it will automatically print the method name, class or file name and line where the log message originated. This is very useful during debug session.
Debug information are optional.
To log message, use the macro defined in this class header.
The log message time stamp can be turned on and off as well.
- Note
- How to use the macro in the source:
- Make sure that the file Log.h is correctly included in the current source code
- Determine the level of your log message: ERROR, WARNING, INFO and TRACE
- If the log message should be written only if a corresponding boolean expression is true, use the _IF version
- If the log message is in a static method or a non QObject derived class (most CamiTK class derived from QObject), use the ALT version
- Start the log message with an uppercase
- Do not add the file, class, or method name, this is taken care of automatically by the debug information
- The log message should be a QString:
- use QString::number(..) to convert primitive numeric type to QString
- use QString::fromStdString(..) to convert std::string
-
completely disabling log To void any log message, add the following line at the beginning of the CMakeLists.txt corresponding to the project you want to silence:
add_definitions(-DCAMITK_DISABLE_LOG)
# you can also also add "-DCAMITK_DISABLE_LOG" to the camitk_extension macro DEFINES option
This will transform all CAMITK log macro written in your code to null function. Note that it will not void the camitkcore log entry (to have absolutely no log at all, you need to compile CamiTK SDK with "-DCAMITK_DISABLE_LOG"
Some simple examples:
unsigned int value = 4;
CAMITK_INFO(tr(
"Current value: %1").arg(QString::number(value)))
#define CAMITK_INFO(MSG)
Log for info verbosity (the second most verbose one) The msg will appear only if the user asked for I...
Definition: Log.h:238
#define CAMITK_TRACE_IF(COND, MSG)
The LOG_VERBOSITY_IF Macros are triggered only if the given condition is true.
Definition: Log.h:262
- Note
- if the source code is compiled with CAMITK_DISABLE_LOG defined, then any log macro results into a NOOP. This does not mean that there can't be any logger. In this case to add debug information you will explicitely have to call Log::getLogger()->log(...)
-
The default logger can be used/started at any time, even without any camitk application instance. In order to define the default log levels and parameter, you need to set the values before the camitk::Application is instantiated. If the default settings are modified by the user, they will automatically replace the default log values. Here is an example:
...
...
camitk::Application
a(
"camitk-myapp", argc, argv,
true,
true);
#define CAMITK_INFO_ALT(MSG)
Definition: Log.h:239
virtual void setMessageBoxLevel(LogLevel level)=0
Set the lowest log level that will open modal message box for messages instead of (silently/undisrupt...
virtual bool setLogToFile(bool writeToFile)=0
Ask the logger to write to the log file.
@ WARNING
Only Warning and Error messages are logged.
Definition: InterfaceLogger.h:63
@ INFO
information, warning and error messages are logged
Definition: InterfaceLogger.h:64
static InterfaceLogger * getLogger()
get the current application logger
Definition: Log.cpp:50
-
In order to force log settings and ignore user settings, you need modify the Application property object directly. For a CamiTK application, you can do that in the constructor of the camitk::Application inherited class (as the first lines for instance, if you don't want to miss anything):
MyCamiTKApplication::MyCamiTKApplication(int& argc, char** argv, QString inputFileName, QString outputDirectory) : Application("camitk-myapplication", argc, argv) {
setProperty("Log to File", true);
setProperty("Logger Level",InterfaceLogger::TRACE);
...
Or you can do that from outside (e.g. in the main.cpp), using the application property object:
...
main(..) {
MyCamiTKApplication myapp(argc, argv, ...);
...
Available application property names linked to log settings:
- "Logger Level"
- "Message Box Level"
- "Log to Standard Output"
- "Log to File"
- "Display Debug Information to Log Message"
- "Display Time Stamp Information to Log Message"
- Note
- for MSVC users A (legacy) MS Windows header defines ERROR as a preprocessor macro. This header is unfortunately sometimes included in low-level library code. This will clash with the CAMITK_ERROR macro. Hints: if you use the CAMITK_ERROR or CAMITK_ERROR_ALT macro in your code and get these compilation errors:
- error C2589: 'constant': illegal token on right side of '::'
- error C2059: syntax error: '::' Move your include>Log.h> line at the end of your include lines. Therefore include >Log.h> should be the last include directive.