Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. General talk
  3. Showcase
  4. Logger: simple, convinient and thread safe logger for Qt applications
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved Showcase
loggingqdebug
43 Posts 14 Posters 62.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    cyberbobs
    wrote on last edited by cyberbobs
    #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:

    #include <QCoreApplication>
    
    #include <Logger.h>
    #include <ConsoleAppender.h>
    
    int main(int argc, char* argv[])
    {
      QCoreApplication app(argc, argv);
      ...
      ConsoleAppender* consoleAppender = new ConsoleAppender();
      consoleAppender->setFormat("[%{type}] <%{function}> %{message}\n");
      logger->registerAppender(consoleAppender);
      ...
      LOG_INFO("Starting the application");
      int result = app.exec();
      ...
      if (result)
        LOG_WARNING() << "Something went wrong." << "Result code is" << result;
    
      return result;
    }
    

    Library is Qt 4 and Qt 5 compatible, tested on Windows, Linux and Android (LogCat support included out-of-the-box). 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, for example, using the git submodule approach. Build scripts to use the Logger in qmake-, CMake- or qbs-based projects are included. Logger also supports using many so-called appenders at once (targeting the log stream to different places), logging categories and many other features: just see the docs :)

    It's distributed free of charge according to the terms of LGPL 2.1 license and can be downloaded on the project GitHub page: https://github.com/dept2/CuteLogger

    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 :)

    AlexorleonA 1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

      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?

      1 Reply Last reply
      0
      • C Offline
        C Offline
        cyberbobs
        wrote on last edited by
        #3

        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).

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • C Offline
            C Offline
            cyberbobs
            wrote on last edited by
            #5

            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).

            1 Reply Last reply
            0
            • I Offline
              I Offline
              Immii
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              • A Offline
                A Offline
                andre
                wrote on last edited by
                #7

                [quote author="Immii" date="1292997872"]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[/quote]
                Look at the gitorious link in the original posting. The code is right there!

                1 Reply Last reply
                0
                • J Offline
                  J Offline
                  jgoluch
                  wrote on last edited by
                  #8

                  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?

                  1 Reply Last reply
                  0
                  • 0 Offline
                    0 Offline
                    01amit
                    wrote on last edited by
                    #9

                    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.

                    1 Reply Last reply
                    0
                    • A Offline
                      A Offline
                      andre
                      wrote on last edited by
                      #10

                      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
                      [quote author="01amit" date="1330493543"]If I want use the FileAppender in QPushButton what I need to do.[/quote]
                      Using FileAppender in QPushButton? Do you mean you want to trigger logging something if you click on some push button?

                      1 Reply Last reply
                      0
                      • 0 Offline
                        0 Offline
                        01amit
                        wrote on last edited by
                        #11

                        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.
                        @
                        int main(int argc, char* argv[])
                        {
                        QCoreApplication app(argc, argv);

                        ConsoleAppender* consoleAppender = new ConsoleAppender();
                        consoleAppender->setFormat("[%-7l] <%C> %m\n");
                        Logger::registerAppender(consoleAppender);

                        LOG_INFO("Starting the application");
                        int result = app.exec();

                        if (result)
                        LOG_WARNING() << "Something went wrong." << "Result code is" << result;

                        return result;
                        }
                        @

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

                        1 Reply Last reply
                        0
                        • A Offline
                          A Offline
                          andre
                          wrote on last edited by
                          #12

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

                          1 Reply Last reply
                          0
                          • 0 Offline
                            0 Offline
                            01amit
                            wrote on last edited by
                            #13

                            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.

                            1 Reply Last reply
                            0
                            • A Offline
                              A Offline
                              andre
                              wrote on last edited by
                              #14

                              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.

                              1 Reply Last reply
                              0
                              • C Offline
                                C Offline
                                cyberbobs
                                wrote on last edited by
                                #15

                                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.

                                1 Reply Last reply
                                0
                                • N Offline
                                  N Offline
                                  nomiz
                                  wrote on last edited by
                                  #16

                                  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!

                                  1 Reply Last reply
                                  0
                                  • C Offline
                                    C Offline
                                    cyberbobs
                                    wrote on last edited by
                                    #17

                                    Please update to the current version of Logger. As of commit "c7d9a27":https://gitorious.org/cutelogger/cutelogger/commit/c7d9a27110b110298fdf42b417b6e0e246fa96af QDebug header is included into Logger.h.

                                    1 Reply Last reply
                                    0
                                    • N Offline
                                      N Offline
                                      nomiz
                                      wrote on last edited by
                                      #18

                                      Nice, works!

                                      1 Reply Last reply
                                      0
                                      • 0 Offline
                                        0 Offline
                                        01amit
                                        wrote on last edited by
                                        #19

                                        How to I destroy this logger object and reinstantiate it without existing the application. Say I have three push buttons (1, 2, 3). I want to instantiate when button 1 is pressed. and use LOG_INFO macro when button 2 is pressed. Flush the data to the file when button 3 is pressed and destroy the object. Re-instantiate the class when button 1 is pressed again without closing the application.
                                        In my application we never exit the application it contoneously runs. I am looking for a way to instantiate the class, use log_info, detroy the object and then reinstantiate.

                                        1 Reply Last reply
                                        0
                                        • C Offline
                                          C Offline
                                          cyberbobs
                                          wrote on last edited by
                                          #20

                                          Well, I definitely need to ask a question here: why do you need to manually instantiate and destroy Logger?

                                          This library is created as singleton without any visible initialization by design. Its main object is automatically created on the first Logger::* static function or macro call. As you can see in "Logger.cpp":https://gitorious.org/cutelogger/cutelogger/blobs/master/src/Logger.cpp the object is automatically destroyed after the application have been closed (using the qAddPostRoutine function).

                                          So, once again: why do you want to do manually the thing that is done automatically? :)

                                          1 Reply Last reply
                                          0

                                          • Login

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved