Patch-Source: https://github.com/ceph/ceph/commit/c0a3d46029757863e6c4b7c317ed31236b8c1345 From c0a3d46029757863e6c4b7c317ed31236b8c1345 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=83=A1=E7=8E=AE=E6=96=87?= Date: Wed, 22 Dec 2021 01:46:32 +0800 Subject: [PATCH] mon: use fmt::format for stderr cluster logging MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Just like 8e78ca5f958 but for mon cluster log. Signed-off-by: 胡玮文 --- src/mon/LogMonitor.cc | 26 ++++++++++++++------------ src/mon/LogMonitor.h | 2 +- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/src/mon/LogMonitor.cc b/src/mon/LogMonitor.cc index 094ea54b65247..e6d1029a1f7b3 100644 --- a/src/mon/LogMonitor.cc +++ b/src/mon/LogMonitor.cc @@ -42,6 +42,7 @@ #include +#include #include #include @@ -354,10 +355,6 @@ void LogMonitor::log_external(const LogEntry& le) channel = CLOG_CHANNEL_CLUSTER; } - if (g_conf().get_val("mon_cluster_log_to_stderr")) { - cerr << channel << " " << le << std::endl; - } - if (channels.do_log_to_syslog(channel)) { string level = channels.get_level(channel); string facility = channels.get_facility(channel); @@ -386,21 +383,19 @@ void LogMonitor::log_external(const LogEntry& le) dout(7) << "journald: " << channel << dendl; } + bool do_stderr = g_conf().get_val("mon_cluster_log_to_stderr"); + int fd = -1; if (g_conf()->mon_cluster_log_to_file) { if (this->log_rotated.exchange(false)) { this->log_external_close_fds(); } auto p = channel_fds.find(channel); - int fd; if (p == channel_fds.end()) { string log_file = channels.get_log_file(channel); dout(20) << __func__ << " logging for channel '" << channel << "' to file '" << log_file << "'" << dendl; - if (log_file.empty()) { - // do not log this channel - fd = -1; - } else { + if (!log_file.empty()) { fd = ::open(log_file.c_str(), O_WRONLY|O_APPEND|O_CREAT|O_CLOEXEC, 0600); if (fd < 0) { int err = -errno; @@ -413,11 +408,12 @@ void LogMonitor::log_external(const LogEntry& le) } else { fd = p->second; } + } + if (do_stderr || fd >= 0) { + fmt::format_to(std::back_inserter(log_buffer), "{}\n", le); if (fd >= 0) { - fmt::format_to(std::back_inserter(file_log_buffer), "{}\n", le); - int err = safe_write(fd, file_log_buffer.data(), file_log_buffer.size()); - file_log_buffer.clear(); + int err = safe_write(fd, log_buffer.data(), log_buffer.size()); if (err < 0) { dout(1) << "error writing to '" << channels.get_log_file(channel) << "' for channel '" << channel @@ -426,6 +422,12 @@ void LogMonitor::log_external(const LogEntry& le) channel_fds.erase(channel); } } + + if (do_stderr) { + fmt::print(std::cerr, "{} {}", channel, std::string_view(log_buffer.data(), log_buffer.size())); + } + + log_buffer.clear(); } } diff --git a/src/mon/LogMonitor.h b/src/mon/LogMonitor.h index f6b433096e45a..1eccaa5c20379 100644 --- a/src/mon/LogMonitor.h +++ b/src/mon/LogMonitor.h @@ -51,7 +51,7 @@ class LogMonitor : public PaxosService, version_t external_log_to = 0; std::map channel_fds; - fmt::memory_buffer file_log_buffer; + fmt::memory_buffer log_buffer; std::atomic log_rotated = false; struct log_channel_info {