Skip to content

Commit

Permalink
Reimplement vlog related logic in base:vlog.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 599395746
  • Loading branch information
tomokinat authored and hiroyuki-komatsu committed Jan 18, 2024
1 parent 88f8c14 commit 8c3e41c
Show file tree
Hide file tree
Showing 11 changed files with 82 additions and 87 deletions.
7 changes: 6 additions & 1 deletion src/base/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
1 change: 1 addition & 0 deletions src/base/base.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@
'system_util.cc',
'text_normalizer.cc',
'util.cc',
'vlog.cc',
],
'dependencies': [
'clock',
Expand Down
35 changes: 0 additions & 35 deletions src/base/logging.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand All @@ -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<std::ostream> real_log_stream_;
int config_verbose_level_;
bool support_color_ = false;
bool use_cerr_ = false;
absl::Mutex mutex_;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -381,18 +358,6 @@ const char *Logging::GetEndColorEscapeSequence() {
return "";
}

int Logging::GetVerboseLevel() {
return Singleton<LogStreamImpl>::get()->verbose_level();
}

void Logging::SetVerboseLevel(int verboselevel) {
Singleton<LogStreamImpl>::get()->set_verbose_level(verboselevel);
}

void Logging::SetConfigVerboseLevel(int verboselevel) {
Singleton<LogStreamImpl>::get()->set_config_verbose_level(verboselevel);
}

void Logging::SetLogToStderr(bool log_to_stderr) {
Singleton<LogStreamImpl>::get()->set_log_to_stderr(log_to_stderr);
}
Expand Down
22 changes: 0 additions & 22 deletions src/base/logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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
Expand Down
13 changes: 0 additions & 13 deletions src/base/logging_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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) << "";
Expand All @@ -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) << "";
Expand Down
54 changes: 54 additions & 0 deletions src/base/vlog.cc
Original file line number Diff line number Diff line change
@@ -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 <algorithm>
#include <atomic>

#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<int> 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
21 changes: 18 additions & 3 deletions src/base/vlog.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_
2 changes: 1 addition & 1 deletion src/config/config_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/qt/qt_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion src/renderer/renderer_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
10 changes: 0 additions & 10 deletions src/unix/ibus/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
Expand Down

0 comments on commit 8c3e41c

Please sign in to comment.