December 20, 2010

Bobs Bobs
Lab Rat
32 posts

Logger: simple, convinient and thread safe logger for Qt applications

Page  
1

Hello in there.

I want to proudly present the simple logging component for the Qt applications we’re internally using for some of our projects. This component is designed concerning the K.I.S.S. principle and proved itself to be very stable and convinient to use. And yes, it’s thread safe and ready for using in multi-threaded applications. I really hope it would be useful for you.

Here’s a simple code example using it:

  1. #include <QCoreApplication>
  2. #include <QDebug>
  3.  
  4. #include <Logger.h>
  5. #include <ConsoleAppender.h>
  6.  
  7. int main(int argc, char* argv[])
  8. {
  9.   QCoreApplication app(argc, argv);
  10.   ...
  11.   ConsoleAppender* consoleAppender = new ConsoleAppender();
  12.   consoleAppender->setFormat("[%-7l] <%C> %m\n");
  13.   Logger::registerAppender(consoleAppender);
  14.   ...
  15.   LOG_INFO("Starting the application");
  16.   int result = app.exec();
  17.   ...
  18.   if (result)
  19.     LOG_WARNING() << "Something went wrong." << "Result code is" << result;
  20.  
  21.   return result;
  22. }

Please note, that I haven’t checked it on all the platforms that the Qt supports, just on the linux/gcc4 (x86 and x86-64), it would be fine if anyone tests it on other platforms or compilers. Also you may wish to know, that it’s not meant to be a separate component, but it’s prepared for including to your application sources. Buildsystem used is CMake (checked with 2.8, but 2.6 may be quite enough), but you may easily build it using qmake or something :)

It’s distributed free of charge according to the terms of LGPL 2.1 license and can be downloaded on the gitorious [gitorious.org]

Rather full documentation on it is embedded into a header files and may be generated using the doxygen (Doxyfile supplied).

Waiting for your comments and suggestions :)

 Signature 

Nokia Certified Qt Specialist.

35 replies

December 21, 2010

Andre Andre
Area 51 Engineer
6031 posts

At first sight (just from your sample above), it looks a little bit like the Qxt logger framework. You have different “appenders”, which would in the Qxt lib be QxtLoggerEngines. You have different logging levels, also supported in Qxt. Instead of qxtLog->info, you use LOG_INFO. Looks pretty similar. Yours is LGPL, Qxt is CPL and LGPL and moving to BSD, I believe.

What is the unique feature, would you say, of your framework above existing ones?

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

December 21, 2010

Bobs Bobs
Lab Rat
32 posts

I really don’t pretend to make an ultimate and unique logging engine for all of the applications you can imagine. There’s plenty of libraries for this (QxlLogger you mentioned, log4cxx and much more).

Just a little list of (IMHO) advantages:

  • Default set of macros (LOG_DEBUG, LOG_INFO etc) collects source file name, source file line and function name (using the FILE, LINE and Q_FUNC_INFO macros accoringly) in addition to the traditional timestamp, log level and message. It gives some more flexibility when debugging a medium sized or large application, without need to seek manually, what part of code sent this message. For example, you can make a little plugin for your favourite IDE (I’ll try to publish the plugin for Qt Creator later) and a little appender that sends the log messages over IPC to move to the line of your source that emitted a log message with just a mouse click.
  • It uses the Qt standart QDebug class instead of the custom QxtLogStream class which gives you an opportunity to use the already overloaded operator<<() for your custom types instead of writing the new ones.
  • It haves an AbstractStringAppender class, which gives a flexible way to customize the look of the simple stream log without any modifications to the library code. I’ve looked through the QxtLogger documentation and haven’t found any similiar functionality. For example, you may use “[l] %m\n” format, which will give you messages like “[Info] Some message text”, or something more compicated like “%t{HH:mm:ss.zzz} [-7l] <%C> %m\n” which will transform to “12:18:11.485 [Debug ] <void Foo::bar(int) const> Some debug message text” :)

To use it or not is just a matter of taste. Just wanted to publish the little utility which i actively use, hoping that it would be useful for someone else. Also, it will be very interesting for me to add some features to it, according to other developers wishes and comments :)

If you really think that this library must not be published here, I think this forum thread could be simply removed.

Upd: just to mention, if the LGPL 2.1 restrictions are unacceptable for someone here, it may be discussed privately (no problems with it).

 Signature 

Nokia Certified Qt Specialist.

December 21, 2010

Andre Andre
Area 51 Engineer
6031 posts

Please don’t view my question as an attack. It was mend as an honest question. The features you mention are interesting. It does look like a valuable tool, and some competition to existing solutions can’t hurt (though contributing ideas and code to those instead of starting your own also doesn’t hurt).

Thanks for sharing your code, and thanks for clarifying what the strongpoints of your solution are! Note that you can use the standard qDebug() << stuff with QxtLogger too, as you can easily re-route qDebug to QxtLogger either with custom code or simply with QxtLogger::installAsMessageHandler(). Still, you mention some nice features, such as automagically logging the place of the event in the code, and the flexible formatting.

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

December 21, 2010

Bobs Bobs
Lab Rat
32 posts

Thanks for your question. To be honest, it was my fault: this features (and comparation to QxtLogger) must have been published here at the starting of this topic. But the hard work of documenting the library in english (it was documented poorly and in russian) haven’t left place for any good ideas in my mind :)

The main point of writing this library was to collect the places of code in addition to logging messages. It was quite useful for our small team and rather big codebase.

I forgot to say, that Logger also installs itself as the Qt message handler, but it cannot be configured in a way that QxtLogger does (haven’t any need for this).

 Signature 

Nokia Certified Qt Specialist.

December 22, 2010

Immii Immii
Ant Farmer
233 posts

Can you please let me know where can I download it, it looks good with your texts and I have never used any logger before for Qt

December 22, 2010

Andre Andre
Area 51 Engineer
6031 posts

Immii wrote:
Can you please let me know where can I download it, it looks good with your texts and I have never used any logger before for Qt

Look at the gitorious link in the original posting. The code is right there!

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

February 13, 2012

jgoluch jgoluch
Lab Rat
4 posts

This looks like exactly what my application needs – being still new to Qt, can you provide some instructions for how to build this or where to place the files?

February 29, 2012

01amit 01amit
Lab Rat
5 posts

The above code explaing how to use ConsoleAppender. I am more interested in learning about how to use FileAppender. If I want use the FileAppender in QPushButton what I need to do.

February 29, 2012

Andre Andre
Area 51 Engineer
6031 posts

Why do you think there is a difference, other than switching out FileAppender for ConsoleAppender and making sure it has a file name?

And what do you mean by

01amit wrote:
If I want use the FileAppender in QPushButton what I need to do.

Using FileAppender in QPushButton? Do you mean you want to trigger logging something if you click on some push button?

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

February 29, 2012

01amit 01amit
Lab Rat
5 posts

Yes, whenever i click on push button it should write something to text file. How do I change the following code if I want to use FileAppender. I am very new to this. Where do I specify file name.

  1.  int main(int argc, char* argv[])
  2.  {
  3.     QCoreApplication app(argc, argv);
  4.  
  5.   ConsoleAppender* consoleAppender = new ConsoleAppender();
  6.    consoleAppender->setFormat("[%-7l] <%C> %m\n");
  7.     Logger::registerAppender(consoleAppender);
  8.  
  9.    LOG_INFO("Starting the application");
  10.    int result = app.exec();
  11.  
  12.   if (result)
  13.     LOG_WARNING() << "Something went wrong." << "Result code is" << result;
  14.  
  15.    return result;
  16.  }

Edit: please use @ tags around your code sections; Andre

February 29, 2012

Andre Andre
Area 51 Engineer
6031 posts

Take a look at the header file of FileAppender, especially the constructor. It takes one additional argument…

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

March 4, 2012

01amit 01amit
Lab Rat
5 posts

Thanks. now I understood how to use file appender and log data into txt file. I want use SQL database instead of txt file how can i do that.

March 5, 2012

Andre Andre
Area 51 Engineer
6031 posts

Write a new appender that does just that. You can start by subclassing AbstractStringAppender. Instead of outputting to file, you can use Qt’s SQL classes to output to a database.

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

March 15, 2012

Bobs Bobs
Lab Rat
32 posts

Andre, thanks for your answers in this topic :)

CuteLogger have been updated about a day ago (thanks goes to Karl-Heinz Reichel). We finally got rid of symlinks in repository, added the Win32 builds support and a qmake file, so currently most of the Qt developers can build it without installing cmake.

 Signature 

Nokia Certified Qt Specialist.

March 30, 2012

nomiz nomiz
Lab Rat
2 posts

Hi there,

I’m starting the logger in main(). When I’m including <Logger.h> in some class of my application the compiler states:

error: invalid use of incomplete type ‘struct QDebug’
error: forward declaration of ‘struct QDebug’

and I still need to incude <QDebug> as well. Is that normal behaviour?

Thanks!

Page  
1

  ‹‹ Cumulus - Flight computer for glider pilots      VDK 1.1 released ››

You must log in to post a reply. Not a member yet? Register here!