Skip to content

Commit

Permalink
Update Logger construction arguments
Browse files Browse the repository at this point in the history
The Logger constructor now takes a shared pointer to a std::ostream.
In this way we do not need the error prone handling of manual stream
descruction any more.
Instead the default shared pointer argument for std::cout takes now
the no-op deleter.

Signed-off-by: Christoph Niethammer <[email protected]>
  • Loading branch information
cniethammer committed Dec 11, 2024
1 parent f99d5cb commit 50225ab
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 10 deletions.
8 changes: 2 additions & 6 deletions src/utils/Logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,10 @@ namespace Log {
std::unique_ptr<Logger> global_log;

// Write to stream
Logger::Logger(logLevel level, std::ostream *os) :
Logger::Logger(logLevel level, std::shared_ptr<std::ostream> os) :
_log_level(level), _msg_log_level(Log::Error),
_do_output(true),
// std::cout is managed globally,
// so do nothing when _log_stream goes out of scope
// --> any passed ostream other than std::cout needs to be
// deleted manually!
_log_stream(os, [](std::ostream*){/* no-op deleter */}),
_log_stream(os),
logLevelNames(), _starttime(), _rank(0)
{
init_starting_time();
Expand Down
7 changes: 3 additions & 4 deletions src/utils/Logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,10 @@ class Logger {
*
* Initializes the log level, log stream and the list of log level names.
* If ENABLE_MPI is enabled by default, all process perform logging output.
* Note: Due to the default argument (std::cout), the passed ostream pointer
* will not be deleted automatically! Any passed ostream pointer other than
* std::cout must be deleted manually!
* Note: The default stream used (std::cout) cannot be deleted. Therefore the
* passed shared pointer to it uses a no-op deleter function.
*/
Logger(logLevel level = Log::Error, std::ostream *os = &(std::cout));
Logger(logLevel level = Log::Error, std::shared_ptr<std::ostream> os = std::shared_ptr<std::ostream>(&std::cout, [](void*){ /* no-op */}));

~Logger() = default;

Expand Down

0 comments on commit 50225ab

Please sign in to comment.