-
Notifications
You must be signed in to change notification settings - Fork 42
Clean-up main model: Add logger interface #1093
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 21 commits
e0b3f57
57088c8
adc6651
70cf27c
5f4ec7c
83a4459
3f8c22e
72ec561
c1d6b99
cf7534e
d3d1ae0
30057cb
644d8d9
4432cfc
9fc3c39
008e980
485bad7
3cc60aa
126b1c4
69b737a
97cf176
babf8b1
aac61a1
6bdea5f
e6faf8a
f4ba5a7
b700a08
bbc901e
ff561f1
474a313
720984d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// SPDX-FileCopyrightText: Contributors to the Power Grid Model project <powergridmodel@lfenergy.org> | ||
// | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
#pragma once | ||
|
||
#include "logging.hpp" | ||
|
||
#include <string_view> | ||
#include <vector> | ||
|
||
namespace power_grid_model::common::logging { | ||
class NoLogger : public Logger { | ||
void log(LogEvent /*tag*/) override {} | ||
void log(LogEvent /*tag*/, std::string_view /*message*/) override {} | ||
void log(LogEvent /*tag*/, double /*value*/) override {} | ||
void log(LogEvent /*tag*/, Idx /*value*/) override {} | ||
std::unique_ptr<Logger> clone() const override { return std::make_unique<NoLogger>(); } | ||
}; | ||
|
||
class LogDispatcher final : public LogDispatch { | ||
mgovers marked this conversation as resolved.
Show resolved
Hide resolved
|
||
public: | ||
void log(LogEvent tag) override { log_impl(tag); } | ||
void log(LogEvent tag, std::string_view message) override { log_impl(tag, message); } | ||
void log(LogEvent tag, double value) override { log_impl(tag, value); } | ||
void log(LogEvent tag, Idx value) override { log_impl(tag, value); } | ||
void registrar(Logger* logger) override { | ||
if (logger != nullptr && std::ranges::find(loggers_, logger) == loggers_.end()) { | ||
loggers_.push_back(logger); | ||
} | ||
} | ||
void deregistrar(Logger* logger) override { | ||
if (logger != nullptr) { | ||
std::erase(loggers_, logger); | ||
} | ||
} | ||
std::unique_ptr<Logger> clone() const override { return std::make_unique<LogDispatcher>(); } | ||
|
||
LogDispatcher() = default; | ||
LogDispatcher(const LogDispatcher&) = delete; | ||
LogDispatcher& operator=(const LogDispatcher&) = delete; | ||
LogDispatcher(LogDispatcher&&) = default; | ||
LogDispatcher& operator=(LogDispatcher&&) = default; | ||
~LogDispatcher() override = default; | ||
|
||
private: | ||
std::vector<Logger*> loggers_; | ||
mgovers marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
template <typename... T> | ||
constexpr void log_impl(LogEvent tag, T&&... values) | ||
requires requires(Logger log_) { | ||
{ log_.log(tag, values...) }; | ||
} | ||
{ | ||
if (loggers_.size() == 1 && loggers_.front() != nullptr) { // allows perfect forwarding to log messages | ||
loggers_.front()->log(tag, std::forward<T>(values)...); | ||
} else { | ||
for (auto& logger : loggers_) { | ||
if (logger != nullptr) { | ||
logger->log(tag, values...); | ||
} | ||
} | ||
capturing::into_the_void(std::forward<T>(values)...); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. values can be any count of arguments and maybe not all get used in logging and hence unused ones get sent to void? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no this is about how to do perfect forwarding (in particular about using a moved object). an alternative would be if (auto const first_non_null = std::ranges::find_if(loggers_, [] (auto& logger) { return logger != nullptr; });
first_non_null != loggers_.end()) {
for (auto& logger : std::ranges::subrange(first_non_null + 1, loggers_.end());) {
if (logger != nullptr) {
logger->log(tag, values...);
}
}
(*first_non_null)->std::forward<T>(values)...);
} else {
capturing::into_the_void(std::forward<T>(values)...)
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. by C++ guidelines: every object that was moved into the current function shall be moved again (for performance reasons). Forwarding means that the object could be passed by
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. cfr. offline discussion: removed the |
||
} | ||
} | ||
}; | ||
|
||
} // namespace power_grid_model::common::logging |
Uh oh!
There was an error while loading. Please reload this page.