Skip to content

Commit 35db51a

Browse files
committed
minor
1 parent aacb1a7 commit 35db51a

2 files changed

Lines changed: 13 additions & 20 deletions

File tree

src/odr/logger.cpp

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ class StdioLogger final : public Logger {
4545

4646
void log_impl(LogLevel level, const std::string &message,
4747
const std::source_location &location) final {
48-
*m_output << format(m_name, level, message, location, m_format) << "\n";
48+
print_head(*m_output, m_name, level, location, m_format);
49+
*m_output << message << "\n";
4950
}
5051

5152
void flush() final { m_output->flush(); }
@@ -132,17 +133,14 @@ Logger::create_tee(const std::vector<std::shared_ptr<Logger>> &loggers) {
132133
return std::make_unique<TeeLogger>(loggers);
133134
}
134135

135-
std::string Logger::format(const std::string &name, LogLevel level,
136-
const std::string &message,
137-
const std::source_location &location,
138-
const LogFormat &format) {
139-
std::stringstream ss;
140-
136+
void Logger::print_head(std::ostream &out, const std::string &name,
137+
LogLevel level, const std::source_location &location,
138+
const LogFormat &format) {
141139
if (!format.time_format.empty()) {
142140
auto now = std::chrono::system_clock::now();
143141
auto time = std::chrono::system_clock::to_time_t(now);
144-
ss << std::put_time(std::localtime(&time), format.time_format.c_str())
145-
<< " ";
142+
out << std::put_time(std::localtime(&time), format.time_format.c_str())
143+
<< " ";
146144
}
147145

148146
if (format.level_width > 0) {
@@ -152,7 +150,7 @@ std::string Logger::format(const std::string &name, LogLevel level,
152150
level_ss << std::string(
153151
std::max<std::size_t>(0, format.level_width - level_ss.str().size()),
154152
' ');
155-
ss << level_ss.str() << " ";
153+
out << level_ss.str() << " ";
156154
}
157155

158156
if (format.name_width > 0) {
@@ -161,7 +159,7 @@ std::string Logger::format(const std::string &name, LogLevel level,
161159
name_ss << std::string(
162160
std::max<std::size_t>(0, format.name_width - name_ss.str().size()),
163161
' ');
164-
ss << name_ss.str() << " ";
162+
out << name_ss.str() << " ";
165163
}
166164

167165
if (format.location_width > 0) {
@@ -184,12 +182,8 @@ std::string Logger::format(const std::string &name, LogLevel level,
184182
std::max<std::size_t>(0,
185183
format.location_width - location_ss.str().size()),
186184
' ');
187-
ss << location_ss.str() << " ";
185+
out << location_ss.str() << " ";
188186
}
189-
190-
ss << message;
191-
192-
return ss.str();
193187
}
194188

195189
} // namespace odr

src/odr/logger.hpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,9 @@ class Logger {
3434
static std::unique_ptr<Logger>
3535
create_tee(const std::vector<std::shared_ptr<Logger>> &loggers);
3636

37-
static std::string format(const std::string &name, LogLevel level,
38-
const std::string &message,
39-
const std::source_location &location,
40-
const LogFormat &format);
37+
static void print_head(std::ostream &out, const std::string &name,
38+
LogLevel level, const std::source_location &location,
39+
const LogFormat &format);
4140

4241
virtual ~Logger() = default;
4342

0 commit comments

Comments
 (0)