The KDE library includes a macro kDebug() which accepts an integer that specifies the "area" of a debug message. An area is a context that can be used to discriminate messages that will appear in the debug logs. Is does suffice to call the kDebug() macro with the same integer value each time to make all the messages to belong to the same area, but it is also possible to do something more: in order to make all messages belonging to the same area immediately recognizable, it is possible to define a function as the following

int MyDebugArea(){
  // define a method-static variable, so that it will not be
  // re-initialized each time this method will be called
  // and register an "area", that is a string context
  // for the debug messages
  static int kde_debug_area = KDebug::registerArea( "[MyKDE debug]" );
  // return the value returned by the KDebug::registerArea method call
  return kde_debug_area;

As readers can see, the above defines a static integer value so that it will not be re-initialized each time the method will be called. The integer value returned is the value of a registerArea method call that defines a string message that will be prepended to each log message automatically.
It does then suffice to call the above method into a piece of code as follows:

 kDebug(MyDebugArea()) <<  "A debug message";

to have the above message be formatted as follows in the logs (e.g., ~/.xsession-erros):

[MyKDE debug] A debug message

Of course it is possible (and encouraged) to define different areas to use in different pieces of code.

The article KDebug area has been posted by Luca Ferrari on May 10, 2012

Tags: c++ , kde