Skip to content

Commit

Permalink
common: use fmt::format for stderr logging
Browse files Browse the repository at this point in the history
Reduce the number of syscalls to improve performance.

Also reduce the probability of https://tracker.ceph.com/issues/49551,
since conmon is less likely to read partial log line.

Test shows that `fmt::print(cerr, ...)` is as performant as calling
`writev` directly. And it is portable and safe in handling short write.

fmt::print(stderr, ...) is not used because it can throw exception.

Fixes: https://tracker.ceph.com/issues/53682
Signed-off-by: 胡玮文 <huww98@outlook.com>
  • Loading branch information
huww98 committed Aug 6, 2022
1 parent 877f778 commit cef04bc
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/log/Log.cc
Expand Up @@ -26,6 +26,7 @@
#include <set>

#include <fmt/format.h>
#include <fmt/ostream.h>

#define MAX_LOG_BUF 65536

Expand Down Expand Up @@ -312,13 +313,13 @@ void Log::_flush(EntryVector& t, bool crash)
syslog(LOG_USER|LOG_INFO, "%s", pos);
}

if (do_stderr) {
std::cerr << m_log_stderr_prefix << std::string_view(pos, used) << std::endl;
}

/* now add newline */
pos[used++] = '\n';

if (do_stderr) {
fmt::print(std::cerr, "{}{}", m_log_stderr_prefix, std::string_view(pos, used));
}

if (do_fd) {
m_log_buf.resize(cur + used);
} else {
Expand Down

0 comments on commit cef04bc

Please sign in to comment.