You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@rocketmq.apache.org by if...@apache.org on 2021/03/26 09:01:41 UTC

[rocketmq-client-cpp] branch re_dev updated: feat: expose logger config api

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


The following commit(s) were added to refs/heads/re_dev by this push:
     new 6d60f06  feat: expose logger config api
6d60f06 is described below

commit 6d60f068ecf92a8cbb4da571907fa851c006e2c7
Author: James Yin <yw...@hotmail.com>
AuthorDate: Fri Mar 26 16:56:48 2021 +0800

    feat: expose logger config api
---
 include/LoggerConfig.h | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++
 src/log/Logging.cpp    | 21 ++++++---------
 src/log/Logging.h      | 47 ++------------------------------
 3 files changed, 83 insertions(+), 58 deletions(-)

diff --git a/include/LoggerConfig.h b/include/LoggerConfig.h
new file mode 100644
index 0000000..6050dff
--- /dev/null
+++ b/include/LoggerConfig.h
@@ -0,0 +1,73 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#ifndef ROCKETMQ_LOGGERCONFIG_H_
+#define ROCKETMQ_LOGGERCONFIG_H_
+
+#include <string>
+
+namespace rocketmq {
+
+enum LogLevel {
+  LOG_LEVEL_FATAL = 1,
+  LOG_LEVEL_ERROR = 2,
+  LOG_LEVEL_WARN = 3,
+  LOG_LEVEL_INFO = 4,
+  LOG_LEVEL_DEBUG = 5,
+  LOG_LEVEL_TRACE = 6,
+  LOG_LEVEL_LEVEL_NUM = 7
+};
+
+class LoggerConfig {
+ public:
+  LoggerConfig(const std::string& name, const std::string& path)
+      : LoggerConfig(name, LOG_LEVEL_INFO, path, 1024 * 1024 * 100, 3) {}
+  LoggerConfig(const std::string& name, LogLevel level, const std::string& path, int file_size, int file_count)
+      : name_(name), level_(level), path_(path), file_size_(file_size), file_count_(file_count) {}
+
+ public:
+  inline const std::string& name() const { return name_; }
+  inline void set_name(const std::string& name) { name_ = name; }
+
+  inline LogLevel level() const { return level_; }
+  inline void set_level(LogLevel level) { level_ = level; }
+
+  inline const std::string& path() const { return path_; }
+  inline void set_path(const std::string& path) { path_ = path; }
+
+  inline int file_size() const { return file_size_; }
+  inline void set_file_size(int file_size) { file_size_ = file_size; }
+
+  inline int file_count() const { return file_count_; }
+  inline void set_file_count(int file_count) { file_count_ = file_count; }
+
+  inline bool config_spdlog() const { return config_spdlog_; }
+  inline void set_config_spdlog(bool config_spdlog) { config_spdlog_ = config_spdlog; }
+
+ private:
+  std::string name_;
+  LogLevel level_;
+  std::string path_;
+  int file_size_;
+  int file_count_;
+  bool config_spdlog_{true};
+};
+
+LoggerConfig& GetDefaultLoggerConfig();
+
+}  // namespace rocketmq
+
+#endif  // ROCKETMQ_LOGGERCONFIG_H_
diff --git a/src/log/Logging.cpp b/src/log/Logging.cpp
index 6eb958e..6370096 100644
--- a/src/log/Logging.cpp
+++ b/src/log/Logging.cpp
@@ -81,20 +81,15 @@ LoggerConfig& GetDefaultLoggerConfig() {
   return default_logger_config;
 }
 
-static std::once_flag default_logger_init_flag;
-static std::unique_ptr<Logger> default_logger;
-
 Logger& GetDefaultLogger() {
-  if (default_logger == nullptr) {
-    std::call_once(default_logger_init_flag, [] {
-      auto& default_logger_config = GetDefaultLoggerConfig();
-      if (default_logger_config.config_spdlog()) {
-        ConfigSpdlog();
-      }
-      default_logger.reset(new Logger(default_logger_config));
-    });
-  }
-  return *default_logger;
+  static Logger default_logger = []() {
+    auto& default_logger_config = GetDefaultLoggerConfig();
+    if (default_logger_config.config_spdlog()) {
+      ConfigSpdlog();
+    }
+    return Logger(default_logger_config);
+  }();
+  return default_logger;
 }
 
 Logger::Logger(const LoggerConfig& config) {
diff --git a/src/log/Logging.h b/src/log/Logging.h
index 24bbd95..0678c9e 100644
--- a/src/log/Logging.h
+++ b/src/log/Logging.h
@@ -31,52 +31,9 @@
 #endif
 // clang-format on
 
-namespace rocketmq {
-
-enum LogLevel {
-  LOG_LEVEL_FATAL = 1,
-  LOG_LEVEL_ERROR = 2,
-  LOG_LEVEL_WARN = 3,
-  LOG_LEVEL_INFO = 4,
-  LOG_LEVEL_DEBUG = 5,
-  LOG_LEVEL_TRACE = 6,
-  LOG_LEVEL_LEVEL_NUM = 7
-};
-
-class LoggerConfig {
- public:
-  LoggerConfig(const std::string& name, const std::string& path)
-      : LoggerConfig(name, LOG_LEVEL_INFO, path, 1024 * 1024 * 100, 3) {}
-  LoggerConfig(const std::string& name, LogLevel level, const std::string& path, int file_size, int file_count)
-      : name_(name), level_(level), path_(path), file_size_(file_size), file_count_(file_count) {}
-
- public:
-  inline const std::string& name() const { return name_; }
-  inline void set_name(const std::string& name) { name_ = name; }
-
-  inline LogLevel level() const { return level_; }
-  inline void set_level(LogLevel level) { level_ = level; }
+#include "LoggerConfig.h"
 
-  inline const std::string& path() const { return path_; }
-  inline void set_path(const std::string& path) { path_ = path; }
-
-  inline int file_size() const { return file_size_; }
-  inline void set_file_size(int file_size) { file_size_ = file_size; }
-
-  inline int file_count() const { return file_count_; }
-  inline void set_file_count(int file_count) { file_count_ = file_count; }
-
-  inline bool config_spdlog() const { return config_spdlog_; }
-  inline void set_config_spdlog(bool config_spdlog) { config_spdlog_ = config_spdlog; }
-
- private:
-  std::string name_;
-  LogLevel level_;
-  std::string path_;
-  int file_size_;
-  int file_count_;
-  bool config_spdlog_{true};
-};
+namespace rocketmq {
 
 class Logger {
  public: