Skip to content

[C++] Log from const Member Functions

Simon Spannagel requested to merge p-log-const into main

This is rather an issue than a MR, but since I had the test I thought I could as well push it and solve the issue here.

We currently cannot log from any const member function of classes since the logger's log() would discard the const qualifier:

In file included from ../cxx/tests/test_core_logging.cpp:11:
../cxx/tests/test_core_logging.cpp: In member function ‘void CATCH2_INTERNAL_TEST_6()::LogTest::log_message() const’:
../cxx/constellation/core/logging/log.hpp:45:17: error: passing ‘const constellation::log::Logger’ as ‘this’ argument discards qualifiers [-fpermissive]
   45 |     (logger).log(level)
      |     ~~~~~~~~~~~~^~~~~~~
../cxx/tests/test_core_logging.cpp:54:9: note: in expansion of macro ‘LOG’
   54 |         LOG(logger, CRITICAL) << "const critical"sv;
      |         ^~~
In file included from ../cxx/constellation/core/chirp/Manager.hpp:25,
                 from ../cxx/tests/test_core_logging.cpp:10:
../cxx/constellation/core/logging/Logger.hpp:89:27: note:   in call to ‘constellation::log::Logger::log_stream constellation::log::Logger::log(constellation::log::Level, std::source_location)’
   89 |         inline log_stream log(Level level, std::source_location src_loc = std::source_location::current()) {
      |                           ^~~

Any idea how to solve this? spdlog's log() is also not const, so I presume we have to introduce some mutable somewhere in the Logger class.

Merge request reports