Skip to content

Commit

Permalink
refactor: Move LoggerWrapper methods to inline (#1121)
Browse files Browse the repository at this point in the history
We call LoggerWrapper::operator()() and LoggerWrapper::doPrint a lot, so I think it makes sense to have them in the header so they can be inlined.

Also changes the way the singleton DummyWrapper works: now it uses a safer idiom with static initialization right inside the function.
  • Loading branch information
paulgessinger committed Jan 5, 2022
1 parent 486d6fe commit 6414195
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 16 deletions.
11 changes: 9 additions & 2 deletions Core/include/Acts/Utilities/Logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#pragma once
// STL include(s)
#include <cassert>
#include <ctime>
#include <exception>
#include <functional>
Expand Down Expand Up @@ -498,7 +499,10 @@ class LoggerWrapper {
///
/// @param lvl The level to check
/// @return Whether to print at this level or not.
bool doPrint(const Logging::Level& lvl) const;
bool doPrint(const Logging::Level& lvl) const {
assert(m_logger != nullptr);
return m_logger->doPrint(lvl);
}

/// Add a logging message at a given level
/// @param lvl The level to print at
Expand All @@ -509,7 +513,10 @@ class LoggerWrapper {
/// Enables using the logging macros `ACTS_*` when an instance of this class
/// is assigned to a local variable `logger`.
/// @return Reference to the logger instance.
const Logger& operator()() const;
const Logger& operator()() const {
assert(m_logger != nullptr);
return *m_logger;
}

private:
const Logger* m_logger;
Expand Down
19 changes: 5 additions & 14 deletions Core/src/Utilities/Logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,12 @@ namespace Acts {

LoggerWrapper::LoggerWrapper(const Logger& logger) : m_logger(&logger) {}

bool LoggerWrapper::doPrint(const Logging::Level& lvl) const {
assert(m_logger != nullptr);
return m_logger->doPrint(lvl);
}

void LoggerWrapper::log(const Logging::Level& lvl,
const std::string& input) const {
assert(m_logger != nullptr);
return m_logger->log(lvl, input);
}

const Logger& LoggerWrapper::operator()() const {
assert(m_logger != nullptr);
return *m_logger;
}

namespace Logging {

namespace {
Expand All @@ -47,9 +37,6 @@ std::unique_ptr<const Logger> makeDummyLogger() {
return std::make_unique<const Logger>(std::move(output), std::move(print));
}

std::unique_ptr<const Logger> s_dummyLogger{makeDummyLogger()};
LoggerWrapper s_dummyLoggerWrapper{*s_dummyLogger};

} // namespace
} // namespace Logging

Expand All @@ -67,6 +54,10 @@ std::unique_ptr<const Logger> getDefaultLogger(const std::string& name,
}

LoggerWrapper getDummyLogger() {
return Logging::s_dummyLoggerWrapper;
static const std::unique_ptr<const Logger> logger =
Logging::makeDummyLogger();
static const LoggerWrapper loggerWrapper{*logger};

return loggerWrapper;
}
} // namespace Acts

0 comments on commit 6414195

Please sign in to comment.