Skip to content

Log messages potentially not issued until program termination #2

@astoeckel

Description

@astoeckel

Some log messages which are logged using the legacy Logger interface only get issued once the program terminates, such as

log(Logger::INFO) << "Connected to USB board.";

in Vmoduleusb.cpp. As far as I can see this is caused by the adventurous implementation of the Logger class, which puts a Message instance in a per-thread smart pointer, and the Message instance actually logging the message in its destructor.

I would propose the following patch to log4cxx/logger.h to fix this issue

diff --git a/log4cxx/logger.h b/log4cxx/logger.h
index 1cc1f84..def6cc3 100644
--- a/log4cxx/logger.h
+++ b/log4cxx/logger.h
@@ -56,8 +56,15 @@ struct Message

        ~Message()
        {
-               // do the actual logging
-               get_log4cxx().log(level(), get().str(), LOG4CXX_LOCATION);
+               log();
+       }
+
+       void log() {
+               if (_level != log4cxx::LevelPtr()) {
+                       // do the actual logging. once.
+                       get_log4cxx().log(level(), get().str(), LOG4CXX_LOCATION);
+                       _level = log4cxx::LevelPtr();
+               }
        }

 private:
@@ -179,7 +186,9 @@ public:

        static std::ostream& flush(std::ostream& stream)
        {
-               return std::flush(stream);
+               stream << std::flush;
+               Logger::instance()._buffer->log();
+               return stream;
        }

        template <typename T>

however, this requires flush to be actually called, which happens almost nowhere in the code base.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions