rocketmq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ifplu...@apache.org
Subject [rocketmq-client-cpp] 25/29: refactor: logger
Date Tue, 29 Dec 2020 03:36:42 GMT
This is an automated email from the ASF dual-hosted git repository.

ifplusor pushed a commit to branch re_dev
in repository https://gitbox.apache.org/repos/asf/rocketmq-client-cpp.git

commit 2af15ead43fbc6e2c801fb572f0ea8d88d76d51b
Author: James Yin <ywhjames@hotmail.com>
AuthorDate: Thu Sep 24 11:09:51 2020 +0800

    refactor: logger
---
 src/common/UtilAll.cpp       | 14 +++----
 src/extern/CProducer.cpp     |  4 +-
 src/extern/CPushConsumer.cpp |  4 +-
 src/log/Logging.cpp          | 87 ++++++++++++++++++++++++--------------------
 src/log/Logging.h            | 33 ++++++++++-------
 5 files changed, 78 insertions(+), 64 deletions(-)

diff --git a/src/common/UtilAll.cpp b/src/common/UtilAll.cpp
index 90dec5b..7da66f2 100644
--- a/src/common/UtilAll.cpp
+++ b/src/common/UtilAll.cpp
@@ -278,17 +278,17 @@ uint32_t UtilAll::getIP() {
 
 std::string UtilAll::getHomeDirectory() {
 #ifndef WIN32
-  char* homeEnv = std::getenv("HOME");
-  std::string homeDir;
-  if (homeEnv == NULL) {
-    homeDir.append(getpwuid(getuid())->pw_dir);
+  char* home_env = std::getenv("HOME");
+  std::string home_dir;
+  if (home_env == NULL) {
+    home_dir.append(getpwuid(getuid())->pw_dir);
   } else {
-    homeDir.append(homeEnv);
+    home_dir.append(home_env);
   }
 #else
-  std::string homeDir(std::getenv("USERPROFILE"));
+  std::string home_dir(std::getenv("USERPROFILE"));
 #endif
-  return homeDir;
+  return home_dir;
 }
 
 static bool createDirectoryInner(const char* dir) {
diff --git a/src/extern/CProducer.cpp b/src/extern/CProducer.cpp
index edf6072..7629594 100644
--- a/src/extern/CProducer.cpp
+++ b/src/extern/CProducer.cpp
@@ -511,7 +511,7 @@ int SetProducerLogFileNumAndSize(CProducer* producer, int fileNum, long
fileSize
   if (producer == NULL) {
     return NULL_POINTER;
   }
-  DEFAULT_LOG_ADAPTER->setLogFileNumAndSize(fileNum, fileSize);
+  DEFAULT_LOGGER_INSTANCE->setLogFileNumAndSize(fileNum, fileSize);
   return OK;
 }
 
@@ -519,7 +519,7 @@ int SetProducerLogLevel(CProducer* producer, CLogLevel level) {
   if (producer == NULL) {
     return NULL_POINTER;
   }
-  DEFAULT_LOG_ADAPTER->set_log_level((LogLevel)level);
+  DEFAULT_LOGGER_INSTANCE->set_log_level((LogLevel)level);
   return OK;
 }
 
diff --git a/src/extern/CPushConsumer.cpp b/src/extern/CPushConsumer.cpp
index 0ba977e..f90802e 100644
--- a/src/extern/CPushConsumer.cpp
+++ b/src/extern/CPushConsumer.cpp
@@ -258,7 +258,7 @@ int SetPushConsumerLogFileNumAndSize(CPushConsumer* consumer, int fileNum,
long
   if (consumer == NULL) {
     return NULL_POINTER;
   }
-  DEFAULT_LOG_ADAPTER->setLogFileNumAndSize(fileNum, fileSize);
+  DEFAULT_LOGGER_INSTANCE->setLogFileNumAndSize(fileNum, fileSize);
   return OK;
 }
 
@@ -266,6 +266,6 @@ int SetPushConsumerLogLevel(CPushConsumer* consumer, CLogLevel level)
{
   if (consumer == NULL) {
     return NULL_POINTER;
   }
-  DEFAULT_LOG_ADAPTER->set_log_level((LogLevel)level);
+  DEFAULT_LOGGER_INSTANCE->set_log_level((LogLevel)level);
   return OK;
 }
diff --git a/src/log/Logging.cpp b/src/log/Logging.cpp
index 8b77528..3fc2b9c 100644
--- a/src/log/Logging.cpp
+++ b/src/log/Logging.cpp
@@ -29,12 +29,30 @@
 
 namespace rocketmq {
 
-LogAdapter* LogAdapter::getLogInstance() {
-  static LogAdapter singleton_;
+Logger* Logger::getLoggerInstance() {
+  static Logger singleton_("default");
   return &singleton_;
 }
 
-LogAdapter::LogAdapter() : log_level_(LOG_LEVEL_INFO) {
+Logger::Logger(const std::string& name) : log_level_(LOG_LEVEL_INFO) {
+  try {
+    init_log_dir_();
+    init_spdlog_env_();
+    logger_ = create_rotating_logger_(name, log_file_, 1024 * 1024 * 100, 3);
+    set_log_level_(log_level_);
+  } catch (std::exception& e) {
+    std::cerr << "initialite logger failed" << std::endl;
+    exit(-1);
+  }
+}
+
+Logger::~Logger() {
+  if (logger_ != nullptr) {
+    spdlog::drop(logger_->name());
+  }
+}
+
+void Logger::init_log_dir_() {
   std::string log_dir;
   const char* dir = std::getenv(ROCKETMQ_CPP_LOG_DIR_ENV.c_str());
   if (dir != nullptr && dir[0] != '\0') {
@@ -47,51 +65,34 @@ LogAdapter::LogAdapter() : log_level_(LOG_LEVEL_INFO) {
   if (log_dir[log_dir.size() - 1] != '/') {
     log_dir.append("/");
   }
+  std::string log_file_name = UtilAll::to_string(UtilAll::getProcessId()) + "_" + "rocketmq-cpp.log";
+  log_file_ = log_dir + log_file_name;
+}
 
-  if (!UtilAll::existDirectory(log_dir)) {
-    UtilAll::createDirectory(log_dir);
-  }
-  if (!UtilAll::existDirectory(log_dir)) {
-    std::cerr << "create log dir error, will exit" << std::endl;
-    exit(1);
-  }
-
-  std::string fileName = UtilAll::to_string(UtilAll::getProcessId()) + "_" + "rocketmq-cpp.log";
-  log_file_ = log_dir + fileName;
-
-#if SPDLOG_VER_MAJOR >= 1
-  spdlog::init_thread_pool(8192, 1);
-
-  auto rotating_sink = std::make_shared<spdlog::sinks::rotating_file_sink_mt>(log_file_,
1024 * 1024 * 100, 3);
-  rotating_sink->set_level(spdlog::level::debug);
-  rotating_sink->set_pattern("[%Y-%m-%d %H:%M:%S.%e] [%l] [thread@%t] - %v");
-  log_sinks_.push_back(rotating_sink);
-
-  logger_ = std::make_shared<spdlog::async_logger>("default", log_sinks_.begin(), log_sinks_.end(),
-                                                   spdlog::thread_pool(), spdlog::async_overflow_policy::block);
-
-  // register it if you need to access it globally
-  spdlog::register_logger(logger_);
-
+void Logger::init_spdlog_env_() {
+  spdlog::set_pattern("[%Y-%m-%d %H:%M:%S.%e] [%l] [thread@%t] - %v");
   // when an error occurred, flush disk immediately
-  logger_->flush_on(spdlog::level::err);
-
+  spdlog::flush_on(spdlog::level::err);
+#if SPDLOG_VER_MAJOR >= 1
+  spdlog::init_thread_pool(spdlog::details::default_async_q_size, 1);
   spdlog::flush_every(std::chrono::seconds(3));
 #else
-  size_t q_size = 4096;
-  spdlog::set_async_mode(q_size);
-  logger_ = spdlog::rotating_logger_mt("default", log_file_, 1024 * 1024 * 100, 3);
-  logger_->set_pattern("[%Y-%m-%d %H:%M:%S.%e] [%l] [thread@%t] - %v");
+  spdlog::set_async_mode(8192, async_overflow_policy::block_retry, nullptr, std::chrono::milliseconds(3000),
nullptr);
 #endif
-
-  setLogLevelInner(log_level_);
 }
 
-LogAdapter::~LogAdapter() {
-  spdlog::drop("default");
+std::shared_ptr<spdlog::logger> Logger::create_rotating_logger_(const std::string&
name,
+                                                                const std::string& filepath,
+                                                                std::size_t max_size,
+                                                                std::size_t max_files) {
+#if SPDLOG_VER_MAJOR >= 1
+  return spdlog::create_async<spdlog::sinks::rotating_file_sink_mt>(name, filepath,
max_size, max_files);
+#else
+  return spdlog::rotating_logger_mt(name, filepath, max_size, max_files);
+#endif
 }
 
-void LogAdapter::setLogLevelInner(LogLevel log_level) {
+void Logger::set_log_level_(LogLevel log_level) {
   switch (log_level) {
     case LOG_LEVEL_FATAL:
       logger_->set_level(spdlog::level::critical);
@@ -117,6 +118,12 @@ void LogAdapter::setLogLevelInner(LogLevel log_level) {
   }
 }
 
-void LogAdapter::setLogFileNumAndSize(int logNum, int sizeOfPerFile) {}
+void Logger::setLogFileNumAndSize(int logNum, int sizeOfPerFile) {
+  // FIXME: set after clients started
+  auto name = logger_->name();
+  spdlog::drop(name);
+  logger_ = create_rotating_logger_(name, log_file_, sizeOfPerFile, logNum);
+  set_log_level_(log_level_);
+}
 
 }  // namespace rocketmq
diff --git a/src/log/Logging.h b/src/log/Logging.h
index 5afcc56..42b1f3b 100644
--- a/src/log/Logging.h
+++ b/src/log/Logging.h
@@ -42,38 +42,45 @@ enum LogLevel {
   LOG_LEVEL_LEVEL_NUM = 7
 };
 
-class LogAdapter {
+class Logger {
  public:
-  virtual ~LogAdapter();
+  static Logger* getLoggerInstance();
 
-  static LogAdapter* getLogInstance();
+ public:
+  virtual ~Logger();
 
   inline spdlog::logger* getSeverityLogger() { return logger_.get(); }
 
   void setLogFileNumAndSize(int logNum, int sizeOfPerFile);
 
   inline LogLevel log_level() const { return log_level_; }
-  inline void set_log_level(LogLevel logLevel) {
-    log_level_ = logLevel;
-    setLogLevelInner(logLevel);
+  inline void set_log_level(LogLevel log_level) {
+    log_level_ = log_level;
+    set_log_level_(log_level);
   }
 
  private:
-  LogAdapter();
-  void setLogLevelInner(LogLevel logLevel);
+  Logger(const std::string& name);
+
+  void init_log_dir_();
+  void init_spdlog_env_();
+
+  std::shared_ptr<spdlog::logger> create_rotating_logger_(const std::string& name,
+                                                          const std::string& filepath,
+                                                          std::size_t max_size,
+                                                          std::size_t max_files);
+
+  void set_log_level_(LogLevel log_level);
 
  private:
   LogLevel log_level_;
   std::string log_file_;
 
   std::shared_ptr<spdlog::logger> logger_;
-#if SPDLOG_VER_MAJOR >= 1
-  std::vector<spdlog::sink_ptr> log_sinks_;
-#endif
 };
 
-#define DEFAULT_LOG_ADAPTER LogAdapter::getLogInstance()
-#define DEFAULT_LOGGER DEFAULT_LOG_ADAPTER->getSeverityLogger()
+#define DEFAULT_LOGGER_INSTANCE Logger::getLoggerInstance()
+#define DEFAULT_LOGGER DEFAULT_LOGGER_INSTANCE->getSeverityLogger()
 
 #define SPDLOG_PRINTF(logger, level, format, ...)                        \
   do {                                                                   \


Mime
View raw message