From f489a179a6d2cc8984eb5956768d4bcfb1e2f0bf Mon Sep 17 00:00:00 2001 From: Vibhav Pant Date: Sat, 9 Sep 2023 21:42:30 +0530 Subject: [PATCH] Remove org.freedesktop.LogControl implementation. The final application is in charge of logging, not the library. --- .../implementation/linux/log_message.cc | 90 ++++--------------- .../implementation/linux/log_message.h | 87 +----------------- 2 files changed, 18 insertions(+), 159 deletions(-) diff --git a/internal/platform/implementation/linux/log_message.cc b/internal/platform/implementation/linux/log_message.cc index 0f2bc3b5..bb29d634 100644 --- a/internal/platform/implementation/linux/log_message.cc +++ b/internal/platform/implementation/linux/log_message.cc @@ -30,36 +30,12 @@ #include "internal/platform/implementation/linux/log_message.h" namespace nearby { -static std::unique_ptr global_log_control_; -static absl::once_flag log_control_init_; - -static void cleanup_log_control() { - global_log_control_ = nullptr; -} - -static void init_log_control(std::nullptr_t) { - global_log_control_ = - std::make_unique(linux::getDefaultBusConnection()); - atexit(cleanup_log_control); -} - -namespace api { -void LogMessage::SetMinLogSeverity(Severity severity) { - absl::call_once(log_control_init_, init_log_control, nullptr); - assert(global_log_control_ != nullptr); - global_log_control_->LogLevel(severity); -} - -bool LogMessage::ShouldCreateLogMessage(Severity severity) { - absl::call_once(log_control_init_, init_log_control, nullptr); - assert(global_log_control_ != nullptr); - return severity >= global_log_control_->GetLogLevel(); -} - -} // namespace api namespace linux { -static inline google::LogSeverity ConvertSeverity( - api::LogMessage::Severity severity) { + +std::atomic min_log_severity_ = + api::LogMessage::Severity::kInfo; + +inline google::LogSeverity ConvertSeverity(api::LogMessage::Severity severity) { switch (severity) { case api::LogMessage::Severity::kWarning: return google::GLOG_WARNING; @@ -73,52 +49,9 @@ static inline google::LogSeverity ConvertSeverity( return google::GLOG_INFO; } } -static inline int ConvertSeverityToSyslog(google::LogSeverity severity) { - switch (severity) { - case google::GLOG_WARNING: - return LOG_WARNING; - case google::GLOG_ERROR: - return LOG_ERR; - case google::GLOG_FATAL: - return LOG_EMERG; - case google::GLOG_INFO: - default: - return LOG_INFO; - } -} -// TODO: Set a LogSink depending on the target set by LogControl LogMessage::LogMessage(const char *file, int line, Severity severity) - : log_streamer_(file, line, ConvertSeverity(severity), - global_log_control_.get(), false) {} - -static absl::Mutex cout_mutex; - -void LogControl::send(google::LogSeverity severity, const char *full_filename, - const char *base_filename, int line, - const struct ::tm *tm_time, const char *message, - size_t message_len) { - switch (log_target_) { - case kJournal: - sd_journal_send("MESSAGE=%s", message, "PRIORITY=%d", - ConvertSeverityToSyslog(severity), "CODE_FILE=%s", - base_filename, "CODE_LINE=%d", line, NULL); - break; - case kSyslog: { - auto str = LogSink::ToString(severity, base_filename, line, tm_time, - message, message_len); - syslog(ConvertSeverityToSyslog(severity), "%s", str.c_str()); - break; - } - case kConsole: - default: - absl::MutexLock l(&cout_mutex); - std::cout << LogSink::ToString(severity, base_filename, line, tm_time, - message, message_len) - << "\n"; - break; - } -} + : log_streamer_(file, line, ConvertSeverity(severity)) {} void LogMessage::Print(const char *format, ...) { char *buf = nullptr; @@ -136,4 +69,15 @@ void LogMessage::Print(const char *format, ...) { std::ostream &LogMessage::Stream() { return log_streamer_.stream(); } } // namespace linux + +namespace api { + +void LogMessage::SetMinLogSeverity(Severity severity) { + linux::min_log_severity_ = severity; +} + +bool LogMessage::ShouldCreateLogMessage(Severity severity) { + return severity >= linux::min_log_severity_; +} +} // namespace api } // namespace nearby diff --git a/internal/platform/implementation/linux/log_message.h b/internal/platform/implementation/linux/log_message.h index 07ef773c..c11b5d60 100644 --- a/internal/platform/implementation/linux/log_message.h +++ b/internal/platform/implementation/linux/log_message.h @@ -17,9 +17,9 @@ #include #include +#include #include "glog/logging.h" -#include "internal/platform/implementation/linux/generated/dbus/logcontrol/logcontrol_server.h" #include "internal/platform/implementation/log_message.h" namespace nearby { @@ -38,91 +38,6 @@ class LogMessage : public api::LogMessage { private: google::LogMessage log_streamer_; - static api::LogMessage::Severity min_log_severity_; -}; - -class LogControl - : public sdbus::AdaptorInterfaces, - public google::LogSink { - public: - LogControl(sdbus::IConnection &system_bus) - : AdaptorInterfaces(system_bus, "/org/freedesktop/LogControl1"), - severity_(api::LogMessage::LogMessage::Severity::kVerbose), - log_target_(kConsole) { - registerAdaptor(); - } - ~LogControl() { unregisterAdaptor(); } - - void LogLevel(const LogMessage::Severity &severity) { severity_ = severity; } - - LogMessage::Severity GetLogLevel() { return severity_; } - - protected: - std::string LogLevel() override { - switch (severity_) { - case api::LogMessage::Severity::kInfo: - return "info"; - case api::LogMessage::Severity::kWarning: - return "warning"; - case api::LogMessage::Severity::kError: - return "err"; - case api::LogMessage::Severity::kFatal: - return "emerg"; - case api::LogMessage::Severity::kVerbose: - default: - return "debug"; - } - } - - void LogLevel(const std::string &value) override { - if (value == "debug") - severity_ = api::LogMessage::Severity::kVerbose; - else if (value == "info") - severity_ = api::LogMessage::Severity::kInfo; - else if (value == "warning") - severity_ = api::LogMessage::Severity::kWarning; - else if (value == "err") - severity_ = api::LogMessage::Severity::kError; - else if (value == "crit" || value == "alert" || value == "emerg") - severity_ = api::LogMessage::Severity::kFatal; - } - - enum LogTarget { kConsole, kKernel, kJournal, kSyslog }; - - std::string LogTarget() override { - switch (log_target_) { - case kKernel: - return "kmsg"; - case kJournal: - return "journal"; - case kSyslog: - return "syslog"; - case kConsole: - default: - return "console"; - } - } - - void LogTarget(const std::string &value) override { - if (value == "console") - log_target_ = kConsole; - else if (value == "kmsg") - log_target_ = kKernel; - else if (value == "journal") - log_target_ = kJournal; - else if (value == "syslog") - log_target_ = kSyslog; - } - - std::string SyslogIdentifier() override { return "com.google.nearby"; } - - void send(google::LogSeverity severity, const char *full_filename, - const char *base_filename, int line, const struct ::tm *tm_time, - const char *message, size_t message_len) override; - - private: - std::atomic severity_; - std::atomic log_target_; }; } // namespace linux } // namespace nearby