Skip to content

Commit 17cfb5d

Browse files
committed
try calm msvc; elevate time
1 parent 35db51a commit 17cfb5d

3 files changed

Lines changed: 21 additions & 18 deletions

File tree

src/odr/logger.cpp

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#include <odr/logger.hpp>
22

33
#include <algorithm>
4-
#include <chrono>
54
#include <filesystem>
65
#include <iomanip>
76
#include <iostream>
@@ -16,7 +15,7 @@ class NullLogger final : public Logger {
1615
public:
1716
[[nodiscard]] bool will_log(LogLevel) const final { return false; }
1817

19-
void log_impl(LogLevel, const std::string &,
18+
void log_impl(Time, LogLevel, const std::string &,
2019
const std::source_location &) final {
2120
// Do nothing
2221
}
@@ -43,9 +42,9 @@ class StdioLogger final : public Logger {
4342
return level >= m_level;
4443
}
4544

46-
void log_impl(LogLevel level, const std::string &message,
45+
void log_impl(Time time, LogLevel level, const std::string &message,
4746
const std::source_location &location) final {
48-
print_head(*m_output, m_name, level, location, m_format);
47+
print_head(*m_output, time, level, m_name, location, m_format);
4948
*m_output << message << "\n";
5049
}
5150

@@ -75,13 +74,13 @@ class TeeLogger final : public Logger {
7574
});
7675
}
7776

78-
void log_impl(LogLevel level, const std::string &message,
77+
void log_impl(Time time, LogLevel level, const std::string &message,
7978
const std::source_location &location) final {
8079
for (const auto &logger : m_loggers) {
8180
if (!logger->will_log(level)) {
8281
continue;
8382
}
84-
logger->log(level, message, location);
83+
logger->log(level, message, time, location);
8584
}
8685
}
8786

@@ -133,14 +132,13 @@ Logger::create_tee(const std::vector<std::shared_ptr<Logger>> &loggers) {
133132
return std::make_unique<TeeLogger>(loggers);
134133
}
135134

136-
void Logger::print_head(std::ostream &out, const std::string &name,
137-
LogLevel level, const std::source_location &location,
135+
void Logger::print_head(std::ostream &out, Time time, LogLevel level,
136+
const std::string &name,
137+
const std::source_location &location,
138138
const LogFormat &format) {
139139
if (!format.time_format.empty()) {
140-
auto now = std::chrono::system_clock::now();
141-
auto time = std::chrono::system_clock::to_time_t(now);
142-
out << std::put_time(std::localtime(&time), format.time_format.c_str())
143-
<< " ";
140+
auto t = Clock::to_time_t(time);
141+
out << std::put_time(std::localtime(&t), format.time_format.c_str()) << " ";
144142
}
145143

146144
if (format.level_width > 0) {

src/odr/logger.hpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
22

3+
#include <chrono>
34
#include <memory>
45
#include <source_location>
56
#include <sstream>
@@ -26,6 +27,9 @@ struct LogFormat {
2627

2728
class Logger {
2829
public:
30+
using Clock = std::chrono::system_clock;
31+
using Time = Clock::time_point;
32+
2933
static Logger &null();
3034
static std::unique_ptr<Logger>
3135
create_stdio(const std::string &name, LogLevel level,
@@ -34,26 +38,27 @@ class Logger {
3438
static std::unique_ptr<Logger>
3539
create_tee(const std::vector<std::shared_ptr<Logger>> &loggers);
3640

37-
static void print_head(std::ostream &out, const std::string &name,
38-
LogLevel level, const std::source_location &location,
41+
static void print_head(std::ostream &out, Time time, LogLevel level,
42+
const std::string &name,
43+
const std::source_location &location,
3944
const LogFormat &format);
4045

4146
virtual ~Logger() = default;
4247

4348
[[nodiscard]] virtual bool will_log(LogLevel level) const = 0;
4449

4550
inline void
46-
log(LogLevel level, const std::string &message,
51+
log(LogLevel level, const std::string &message, Time time = Clock::now(),
4752
const std::source_location &location = std::source_location::current()) {
4853
if (will_log(level)) {
49-
log_impl(level, message, location);
54+
log_impl(time, level, message, location);
5055
}
5156
}
5257

5358
virtual void flush() = 0;
5459

5560
protected:
56-
virtual void log_impl(LogLevel level, const std::string &message,
61+
virtual void log_impl(Time time, LogLevel level, const std::string &message,
5762
const std::source_location &location) = 0;
5863
};
5964

test/src/logger_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
using namespace odr;
66

7-
TEST(Logger, stdout) {
7+
TEST(Logger, stdio) {
88
auto logger = Logger::create_stdio("test", LogLevel::verbose);
99

1010
logger->log(LogLevel::verbose, "Test message with log function");

0 commit comments

Comments
 (0)