Skip to content

Commit 4137dc1

Browse files
committed
address comments and final design decisions
Signed-off-by: Santiago Figueroa Manrique <figueroa1395@gmail.com>
1 parent 5ad5bc6 commit 4137dc1

7 files changed

Lines changed: 131 additions & 106 deletions

File tree

power_grid_model_c/power_grid_model/include/power_grid_model/common/calculation_info.hpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,15 @@ class CalculationInfo : public Logger {
3838
}
3939
void log(LogEvent tag, double value) override { log_impl(tag, value); }
4040
void log(LogEvent tag, Idx value) override { log_impl(tag, static_cast<double>(value)); }
41+
void log(std::string_view /*message*/) {
42+
// no logging
43+
}
44+
template <LazyLoggingFn Fn> void log(LogEvent /*tag*/, Fn&& /*fn*/) {
45+
// no logging
46+
}
47+
template <LazyLoggingFn Fn> void log(Fn&& /*fn*/) {
48+
// no logging
49+
}
4150

4251
private:
4352
Data data_;

power_grid_model_c/power_grid_model/include/power_grid_model/common/dummy_logging.hpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@ class NoLogger : public Logger {
2525
void log(LogEvent /*tag*/, Idx /*value*/) override {
2626
// no logging
2727
}
28+
void log(std::string_view /*message*/) {
29+
// no logging
30+
}
31+
template <LazyLoggingFn Fn> void log(LogEvent /*tag*/, Fn&& /*fn*/) {
32+
// no logging
33+
}
34+
template <LazyLoggingFn Fn> void log(Fn&& /*fn*/) {
35+
// no logging
36+
}
2837

2938
template <std::derived_from<Logger> T> T& merge_into(T& destination) const { return destination; }
3039
};

power_grid_model_c/power_grid_model/include/power_grid_model/common/logging.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,21 @@ enum class LogEvent : int16_t {
4444
max_num_iter = 2248, // TODO(mgovers): find other error code
4545
};
4646

47+
template <typename Fn>
48+
concept LazyLoggingFn = std::invocable<Fn> && std::convertible_to<std::invoke_result_t<Fn>, std::string> &&
49+
(!std::convertible_to<Fn, std::string_view>);
50+
4751
class Logger {
4852
public:
4953
virtual void log(LogEvent tag) = 0;
5054
virtual void log(LogEvent tag, std::string_view message) = 0;
5155
virtual void log(LogEvent tag, double value) = 0;
5256
virtual void log(LogEvent tag, Idx value) = 0;
5357

58+
void log(std::string_view message) { log(LogEvent::unknown, message); }
59+
template <LazyLoggingFn Fn> void log(LogEvent tag, Fn&& fn) { log(tag, std::invoke(std::forward<Fn>(fn))); }
60+
template <LazyLoggingFn Fn> void log(Fn&& fn) { log(LogEvent::unknown, std::invoke(std::forward<Fn>(fn))); }
61+
5462
Logger(Logger&&) noexcept = default;
5563
Logger& operator=(Logger&&) noexcept = default;
5664
virtual ~Logger() = default;

power_grid_model_c/power_grid_model/include/power_grid_model/common/multi_threaded_logging.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ class MultiThreadedLoggerImpl : public MultiThreadedLogger {
7373
void log(LogEvent tag, double value) override { log_.log(tag, value); }
7474
void log(LogEvent tag, Idx value) override { log_.log(tag, value); }
7575

76+
using MultiThreadedLogger::log;
77+
7678
private:
7779
friend class ThreadLogger;
7880

power_grid_model_c/power_grid_model/include/power_grid_model/common/text_logger.hpp

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,13 @@
2020

2121
namespace power_grid_model {
2222
namespace common::logging {
23-
template <typename Fn>
24-
concept LazyLoggingFn = std::invocable<Fn> && std::convertible_to<std::invoke_result_t<Fn>, std::string> &&
25-
(!std::convertible_to<Fn, std::string_view>);
23+
// template <typename Fn>
24+
// concept LazyLoggingFn = std::invocable<Fn> && std::convertible_to<std::invoke_result_t<Fn>, std::string> &&
25+
// (!std::convertible_to<Fn, std::string_view>);
2626

2727
class TextLogger : public Logger {
28-
using Clock = std::chrono::high_resolution_clock;
2928
using FlushHandler = std::function<void(std::string)>;
3029

31-
Clock::time_point start_time_{Clock::now()};
3230
std::stringstream data_;
3331
FlushHandler flush_handler_;
3432

@@ -38,14 +36,11 @@ class TextLogger : public Logger {
3836

3937
TextLogger(TextLogger const&) = delete;
4038
TextLogger(TextLogger&& other) noexcept
41-
: start_time_(other.start_time_),
42-
data_(std::exchange(other.data_, {})),
43-
flush_handler_(std::exchange(other.flush_handler_, {})) {}
39+
: data_(std::exchange(other.data_, {})), flush_handler_(std::exchange(other.flush_handler_, {})) {}
4440

4541
TextLogger& operator=(TextLogger const&) = delete;
4642
TextLogger& operator=(TextLogger&& other) noexcept {
4743
if (this != &other) {
48-
start_time_ = other.start_time_;
4944
data_ = std::exchange(other.data_, {});
5045
flush_handler_ = std::exchange(other.flush_handler_, {});
5146
}
@@ -69,15 +64,23 @@ class TextLogger : public Logger {
6964
void log(LogEvent tag, double value) override { log_impl(tag, std::to_string(value)); }
7065
void log(LogEvent tag, Idx value) override { log_impl(tag, std::to_string(value)); }
7166
void log(LogEvent tag, std::string_view message) override { log_impl(tag, message); }
72-
void log(std::string_view message) { log_impl(LogEvent::unknown, message); }
73-
template <LazyLoggingFn Fn> void log(LogEvent tag, Fn&& fn) { log_impl(tag, std::invoke(std::forward<Fn>(fn))); }
74-
template <LazyLoggingFn Fn> void log(Fn&& fn) { log_impl(LogEvent::unknown, std::invoke(std::forward<Fn>(fn))); }
67+
68+
using Logger::log;
7569

7670
private:
71+
auto timestamp() const {
72+
using namespace std::chrono;
73+
const auto now = system_clock::now();
74+
const auto sec = floor<seconds>(now);
75+
const auto ms = duration_cast<milliseconds>(now - sec).count();
76+
77+
// Z stands for UTC time zone
78+
// format example: 2026-04-25 14:00:00.000Z
79+
return std::format("{:%F %T}.{:03}Z", sec, ms);
80+
}
81+
7782
void log_impl(LogEvent tag, std::string_view message) {
78-
data_ << std::format("[{} ns] Tag:{}: {}\n",
79-
std::chrono::duration_cast<std::chrono::nanoseconds>(Clock::now() - start_time_).count(),
80-
std::to_underlying(tag), message);
83+
data_ << std::format("[{}] Tag:{}: {}\n", timestamp(), std::to_underlying(tag), message);
8184
}
8285

8386
public:

tests/benchmark_cpp/benchmark.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ struct PowerGridBenchmark {
211211
run(single_scenario);
212212
}
213213
print_info(info);
214+
info.clear();
214215

215216
if (batch_size > 0) {
216217
info.clear();
@@ -219,6 +220,7 @@ struct PowerGridBenchmark {
219220
run(batch_size);
220221
}
221222
print_info(info);
223+
info.clear();
222224

223225
std::cout << "\n\n";
224226
}

0 commit comments

Comments
 (0)