From 8c3e41cb09ea910ff25695e37d486bb29ee4d24b Mon Sep 17 00:00:00 2001 From: Tomoki Nakagawa Date: Thu, 18 Jan 2024 06:37:01 +0000 Subject: [PATCH] Reimplement vlog related logic in base:vlog. PiperOrigin-RevId: 599395746 --- src/base/BUILD.bazel | 7 ++++- src/base/base.gyp | 1 + src/base/logging.cc | 35 --------------------- src/base/logging.h | 22 -------------- src/base/logging_test.cc | 13 -------- src/base/vlog.cc | 54 +++++++++++++++++++++++++++++++++ src/base/vlog.h | 21 +++++++++++-- src/config/config_handler.cc | 2 +- src/renderer/qt/qt_server.cc | 2 +- src/renderer/renderer_server.cc | 2 +- src/unix/ibus/main.cc | 10 ------ 11 files changed, 82 insertions(+), 87 deletions(-) create mode 100644 src/base/vlog.cc diff --git a/src/base/BUILD.bazel b/src/base/BUILD.bazel index 4413f9463..a222b499b 100644 --- a/src/base/BUILD.bazel +++ b/src/base/BUILD.bazel @@ -174,8 +174,13 @@ mozc_cc_test( mozc_cc_library( name = "vlog", + srcs = ["vlog.cc"], hdrs = ["vlog.h"], - deps = [":logging"], + deps = [ + ":logging", + "@com_google_absl//absl/base:core_headers", + "@com_google_absl//absl/flags:flag", + ], ) mozc_cc_library( diff --git a/src/base/base.gyp b/src/base/base.gyp index d99373f51..d283846ce 100644 --- a/src/base/base.gyp +++ b/src/base/base.gyp @@ -110,6 +110,7 @@ 'system_util.cc', 'text_normalizer.cc', 'util.cc', + 'vlog.cc', ], 'dependencies': [ 'clock', diff --git a/src/base/logging.cc b/src/base/logging.cc index a1a51072b..15079d9a0 100644 --- a/src/base/logging.cc +++ b/src/base/logging.cc @@ -72,7 +72,6 @@ ABSL_FLAG(bool, logtostderr, false, .OnUpdate([] { mozc::Logging::SetLogToStderr(absl::GetFlag(FLAGS_logtostderr)); }); -ABSL_FLAG(int32_t, v, 0, "verbose level"); namespace mozc { @@ -162,12 +161,6 @@ const char *Logging::GetBeginColorEscapeSequence(LogSeverity severity) { const char *Logging::GetEndColorEscapeSequence() { return ""; } -int Logging::GetVerboseLevel() { return 0; } - -void Logging::SetVerboseLevel(int verboselevel) {} - -void Logging::SetConfigVerboseLevel(int verboselevel) {} - void Logging::SetLogToStderr(bool log_to_stderr) {} #else // MOZC_NO_LOGGING @@ -182,20 +175,6 @@ class LogStreamImpl { void Init(const std::string &log_file_path); void Reset(); - int verbose_level() const { - return std::max(absl::GetFlag(FLAGS_v), config_verbose_level_); - } - - void set_verbose_level(int level) { - absl::MutexLock l(&mutex_); - absl::SetFlag(&FLAGS_v, level); - } - - void set_config_verbose_level(int level) { - absl::MutexLock l(&mutex_); - config_verbose_level_ = level; - } - bool support_color() const { return support_color_; } void Write(LogSeverity, const std::string &log); @@ -216,7 +195,6 @@ class LogStreamImpl { // This is not thread-safe so must be guarded. // If std::cerr is real log stream, this is empty. std::unique_ptr real_log_stream_; - int config_verbose_level_; bool support_color_ = false; bool use_cerr_ = false; absl::Mutex mutex_; @@ -287,7 +265,6 @@ void LogStreamImpl::Reset() { void LogStreamImpl::ResetUnlocked() { real_log_stream_.reset(); - config_verbose_level_ = 0; #if defined(__ANDROID__) || defined(_WIN32) // On Android, the standard log library is used. // On Windows, coloring is disabled @@ -381,18 +358,6 @@ const char *Logging::GetEndColorEscapeSequence() { return ""; } -int Logging::GetVerboseLevel() { - return Singleton::get()->verbose_level(); -} - -void Logging::SetVerboseLevel(int verboselevel) { - Singleton::get()->set_verbose_level(verboselevel); -} - -void Logging::SetConfigVerboseLevel(int verboselevel) { - Singleton::get()->set_config_verbose_level(verboselevel); -} - void Logging::SetLogToStderr(bool log_to_stderr) { Singleton::get()->set_log_to_stderr(log_to_stderr); } diff --git a/src/base/logging.h b/src/base/logging.h index 0a7710003..d274a3cb9 100644 --- a/src/base/logging.h +++ b/src/base/logging.h @@ -112,18 +112,6 @@ class Logging { // Returns "YYYY-MM-DD HH:MM:SS PID TID", e.g. "2008 11-16 19:40:21 100 20" static std::string GetLogMessageHeader(); - // Returns FLAGS_v - static int GetVerboseLevel(); - - // Sets FLAGS_v - static void SetVerboseLevel(int verboselevel); - - // Sets Verbose Level for Config. - // Since Config dialog will overwrite -v option, we separate - // config_verbose_level and FLAGS_v. - // real_config_level = max(FLAGS_v, config_verbose_level); - static void SetConfigVerboseLevel(int verboselevel); - // Gets an escape sequence to colorize log messages on tty devices. static const char *GetBeginColorEscapeSequence(LogSeverity severity); static const char *GetEndColorEscapeSequence(); @@ -295,14 +283,6 @@ class NullLogFinalizer { << " [" << #condition << "] " #endif // !MOZC_NO_LOGGING -#define VLOG_IS_ON(verboselevel) \ - (mozc::Logging::GetVerboseLevel() >= verboselevel) - -#define VLOG(verboselevel) LOG_IF(INFO, VLOG_IS_ON(verboselevel)) - -#define VLOG_IF(verboselevel, condition) \ - LOG_IF(INFO, ((condition) && VLOG_IS_ON(verboselevel))) - #define CHECK_EQ(a, b) CHECK((a) == (b)) #define CHECK_NE(a, b) CHECK((a) != (b)) #define CHECK_GE(a, b) CHECK((a) >= (b)) @@ -351,8 +331,6 @@ class NullLogFinalizer { #endif // opt build -#define DVLOG(verboselevel) DLOG_IF(INFO, VLOG_IS_ON(verboselevel)) - #ifndef MOZC_LOG_PROTOBUF #define MOZC_LOG_PROTOBUF(message) ((message).DebugString()) #endif // MOZC_LOG_PROTOBUF diff --git a/src/base/logging_test.cc b/src/base/logging_test.cc index 9993d8dbe..89c125abb 100644 --- a/src/base/logging_test.cc +++ b/src/base/logging_test.cc @@ -44,20 +44,12 @@ TEST(LoggingTest, CompileTest) { LOG(ERROR) << ""; LOG(DFATAL) << ""; LOG(FATAL) << ""; - VLOG(0) << ""; - VLOG(1) << ""; - VLOG(2) << ""; - VLOG(3) << ""; } if (false) { DLOG(INFO) << ""; DLOG(WARNING) << ""; DLOG(ERROR) << ""; DLOG(FATAL) << ""; - DVLOG(0) << ""; - DVLOG(1) << ""; - DVLOG(2) << ""; - DVLOG(3) << ""; } LOG_IF(INFO, false) << ""; @@ -71,11 +63,6 @@ TEST(LoggingTest, CompileTest) { DLOG_IF(ERROR, false) << ""; DLOG_IF(FATAL, false) << ""; - VLOG_IF(0, false) << ""; - VLOG_IF(1, false) << ""; - VLOG_IF(2, false) << ""; - VLOG_IF(3, false) << ""; - CHECK(true) << ""; CHECK_EQ(true, true) << ""; CHECK_NE(true, false) << ""; diff --git a/src/base/vlog.cc b/src/base/vlog.cc new file mode 100644 index 000000000..72aeb7223 --- /dev/null +++ b/src/base/vlog.cc @@ -0,0 +1,54 @@ +// Copyright 2010-2021, Google Inc. +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +#include "base/vlog.h" + +#include +#include + +#include "absl/base/attributes.h" +#include "absl/flags/flag.h" + + +ABSL_FLAG(int, v, 0, "Show all VLOG(m) messages for m <= this."); + +namespace mozc::internal { + +ABSL_CONST_INIT std::atomic config_vlog_level = 0; + +int GetVLogLevel() { + return std::max(absl::GetFlag(FLAGS_v), + config_vlog_level.load(std::memory_order_acquire)); +} + +void SetConfigVLogLevel(int v) { + config_vlog_level.store(v, std::memory_order_release); +} + +} // namespace mozc::internal diff --git a/src/base/vlog.h b/src/base/vlog.h index 100de878e..7541cc2cb 100644 --- a/src/base/vlog.h +++ b/src/base/vlog.h @@ -32,10 +32,25 @@ #include "base/logging.h" -#define MOZC_VLOG_IS_ON(severity) VLOG_IS_ON(severity) +namespace mozc::internal { -#define MOZC_VLOG(severity) VLOG(severity) +// Returns the current verbose log level, which is the maximum of --v flag and +// `verbose_level` in the config. +int GetVLogLevel(); -#define MOZC_DVLOG(severity) DVLOG(severity) +// Updates the (mirror of) `verbose_level` in the config. +// +// To avoid dependency on the config from logging library, vlog holds a copy of +// the `verbose_level` internally, and config handlers are expected to call this +// setter accordingly. +void SetConfigVLogLevel(int v); + +} // namespace mozc::internal + +#define MOZC_VLOG_IS_ON(severity) (mozc::internal::GetVLogLevel() >= severity) + +#define MOZC_VLOG(severity) LOG_IF(INFO, MOZC_VLOG_IS_ON(severity)) + +#define MOZC_DVLOG(severity) DLOG_IF(INFO, MOZC_VLOG_IS_ON(severity)) #endif // MOZC_BASE_VLOG_H_ diff --git a/src/config/config_handler.cc b/src/config/config_handler.cc index 1f051a43b..6c0e40059 100644 --- a/src/config/config_handler.cc +++ b/src/config/config_handler.cc @@ -142,7 +142,7 @@ void ConfigHandlerImpl::SetConfigInternal(Config config) { } #endif // MOZC_NO_LOGGING - Logging::SetConfigVerboseLevel(config_.verbose_level()); + mozc::internal::SetConfigVLogLevel(config_.verbose_level()); // Initialize platform specific configuration. if (config_.session_keymap() == Config::NONE) { diff --git a/src/renderer/qt/qt_server.cc b/src/renderer/qt/qt_server.cc index 57284cac7..fcabd7551 100644 --- a/src/renderer/qt/qt_server.cc +++ b/src/renderer/qt/qt_server.cc @@ -86,7 +86,7 @@ QtServer::QtServer() #ifndef MOZC_NO_LOGGING config::Config config; config::ConfigHandler::GetConfig(&config); - Logging::SetConfigVerboseLevel(config.verbose_level()); + mozc::internal::SetConfigVLogLevel(config.verbose_level()); #endif // MOZC_NO_LOGGING } diff --git a/src/renderer/renderer_server.cc b/src/renderer/renderer_server.cc index 3082dac13..4ed0f0d82 100644 --- a/src/renderer/renderer_server.cc +++ b/src/renderer/renderer_server.cc @@ -170,7 +170,7 @@ RendererServer::RendererServer() #ifndef MOZC_NO_LOGGING config::Config config; config::ConfigHandler::GetConfig(&config); - Logging::SetConfigVerboseLevel(config.verbose_level()); + mozc::internal::SetConfigVLogLevel(config.verbose_level()); #endif // MOZC_NO_LOGGING } diff --git a/src/unix/ibus/main.cc b/src/unix/ibus/main.cc index 17aae7264..85ed27f73 100644 --- a/src/unix/ibus/main.cc +++ b/src/unix/ibus/main.cc @@ -50,15 +50,6 @@ namespace mozc { namespace ibus { namespace { -void EnableVerboseLog() { -#ifndef MOZC_NO_LOGGING - constexpr int kDefaultVerboseLevel = 1; - if (mozc::Logging::GetVerboseLevel() < kDefaultVerboseLevel) { - mozc::Logging::SetVerboseLevel(kDefaultVerboseLevel); - } -#endif // MOZC_NO_LOGGING -} - void IgnoreSigChild() { // Don't wait() child process termination. struct sigaction sa; @@ -121,7 +112,6 @@ void RunIbus() { IbusBusWrapper bus; MozcEngine engine; InitIbusComponent(&bus, &engine, absl::GetFlag(FLAGS_ibus)); - EnableVerboseLog(); // Do nothing if MOZC_NO_LOGGING is defined. IgnoreSigChild(); IbusWrapper::Main(); }