Skip to content

Commit

Permalink
Optimize: Modify log module (#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
raychen911 authored Nov 20, 2023
1 parent 8bcb9c5 commit 1f475f3
Show file tree
Hide file tree
Showing 16 changed files with 149 additions and 170 deletions.
5 changes: 3 additions & 2 deletions trpc/common/config/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,16 @@ cc_library(
deps = [
":config_helper",
":default_log_conf_h",
":default_log_conf_parse",
":default_log_conf_parser",
],
)

cc_library(
name = "default_log_conf_parse",
name = "default_log_conf_parser",
srcs = ["default_log_conf_parser.cc"],
hdrs = ["default_log_conf_parser.h"],
deps = [
":default_value",
":config_helper",
":default_log_conf_h",
"@com_github_jbeder_yaml_cpp//:yaml-cpp",
Expand Down
15 changes: 0 additions & 15 deletions trpc/common/config/default_log_conf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,6 @@ void DefaultLogConfig::Display() const {
}
}

template <>
bool GetLoggerConfig<trpc::DefaultLogConfig::LoggerInstance>(std::string_view logger_name,
trpc::DefaultLogConfig::LoggerInstance& config) {
YAML::Node logger_node;
// The node is obtained by yaml parsing a single Logger
if (!GetLoggerNode(logger_name, logger_node)) {
std::cerr << "Get DefaultLogNode err or logger not found, " << " logger_name: " << logger_name << std::endl;
return false;
}
// Convert node into a LoggerInstance config object
YAML::convert<DefaultLogConfig::LoggerInstance> c;
c.decode(logger_node, config);
return true;
}

bool GetDefaultLogConfig(trpc::DefaultLogConfig& config) {
YAML::Node default_log_node;
// yaml gets node by parsing DefaultLog
Expand Down
12 changes: 4 additions & 8 deletions trpc/common/config/default_log_conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ struct DefaultLogConfig {

/// @brief Print out the logger configuration.
void Display() const {
std::cout << "name:" << name << std::endl;
std::cout << "min_level:" << min_level << std::endl;
std::cout << "format:" << format << std::endl;
std::cout << "mode:" << mode << std::endl;
std::cout << "name: " << name << std::endl;
std::cout << "min_level: " << min_level << std::endl;
std::cout << "format: " << format << std::endl;
std::cout << "mode: " << mode << " ===> 1: sync 2: async 3: overrun_oldest" << std::endl;
}
};

Expand All @@ -50,10 +50,6 @@ struct DefaultLogConfig {
void Display() const;
};

/// @brief Get the configuration for a single logger from the default plugin configuration
template <typename Config>
bool GetLoggerConfig(std::string_view logger_name, Config& config);

/// @brief Get node config for all loggers under default plugin
bool GetDefaultLogConfig(trpc::DefaultLogConfig& config);

Expand Down
26 changes: 25 additions & 1 deletion trpc/common/config/default_log_conf_parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
namespace YAML {

bool GetDefaultLogNode(YAML::Node& default_log_node) {
if (!trpc::ConfigHelper::GetInstance()->GetConfig({"plugins", "log", "default"}, default_log_node)) return false;
if (!trpc::ConfigHelper::GetInstance()->GetConfig({"plugins", "log", trpc::kTrpcLogCacheStringDefault},
default_log_node)) {
return false;
}
return true;
}

Expand All @@ -34,4 +37,25 @@ bool GetLoggerNode(std::string_view logger_name, YAML::Node& logger_node) {
return true;
}

bool GetDefaultLoggerSinkNode(std::string_view logger_name, std::string_view sink_type, std::string_view sink_name,
YAML::Node& sink_log_node) {
YAML::Node logger_node, sinks_node;
// Check if logger is configured
if (!GetLoggerNode(logger_name, logger_node)) {
std::cerr << "Get loggerNode err, logger_name: " << logger_name << std::endl;
return false;
}
// Check if sinks are configured
if (!trpc::ConfigHelper::GetNode(logger_node, {sink_type.data()}, sinks_node)) {
std::cerr << logger_name << " -> " << sink_type << " not found!" << std::endl;
return false;
}
// Check if sink_log is configured
if (!trpc::ConfigHelper::GetNode(sinks_node, {sink_name.data()}, sink_log_node)) {
std::cerr << logger_name << " -> " << sink_type << " -> " << sink_name << " not found!" << std::endl;
return false;
}
return true;
}

} // namespace YAML
22 changes: 21 additions & 1 deletion trpc/common/config/default_log_conf_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,35 @@

#include "trpc/common/config/config_helper.h"
#include "trpc/common/config/default_log_conf.h"
#include "trpc/common/config/default_value.h"

namespace YAML {

/// @brief Get yaml configuration under defalut plugin.
/// @brief Get yaml configuration under default plugin.
bool GetDefaultLogNode(YAML::Node& default_log_node);

/// @brief Get the yaml configuration under the logger.
bool GetLoggerNode(std::string_view logger_name, YAML::Node& logger_node);

/// @brief Get the yaml of sinks configuration under the default logger.
bool GetDefaultLoggerSinkNode(std::string_view logger_name, std::string_view sink_type, std::string_view sink_name,
YAML::Node& sink_log_node);

/// @brief Get the configuration for the logger sink based on the logger name
template <typename SinkConfig>
bool GetDefaultLoggerSinkConfig(std::string_view logger_name, std::string_view sink_type, std::string_view sink_name,
SinkConfig& config) {
YAML::Node sink_log_node;
// Parse a single Logger through yaml to get nodes in local_file
if (!GetDefaultLoggerSinkNode(logger_name, sink_type, sink_name, sink_log_node)) {
return false;
}
// Convert node to sink config
YAML::convert<SinkConfig> sink_conf;
sink_conf.decode(sink_log_node, config);
return true;
}

template <>
struct convert<trpc::DefaultLogConfig::LoggerInstance> {
static YAML::Node encode(const trpc::DefaultLogConfig::LoggerInstance& config) {
Expand Down
41 changes: 14 additions & 27 deletions trpc/common/config/local_file_sink_conf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,39 +14,26 @@
#include <any>
#include <iostream>

#include "trpc/common/config/default_log_conf_parser.h"
#include "trpc/common/config/local_file_sink_conf.h"
#include "trpc/common/config/local_file_sink_conf_parser.h"

#include "yaml-cpp/yaml.h"

namespace trpc {

/// @brief Get the configuration for the logger local file based on the logger name
template <>
bool GetLoggerConfig<LocalFileSinkConfig>(std::string_view logger_name, LocalFileSinkConfig& config) {
YAML::Node local_file_node;
// Parse a single Logger through yaml to get nodes in local_file
if (!YAML::GetLocalFileNode(logger_name, local_file_node)) {
return false;
}
// Convert node to LocalFileSinkConfig
YAML::convert<LocalFileSinkConfig> c;
c.decode(local_file_node, config);
return true;
}

/// @brief Print the configuration
void LocalFileSinkConfig::Display() const {
std::cout << "format:" << format << std::endl;
std::cout << "eol:" << format << std::endl;
std::cout << "filename:" << format << std::endl;
std::cout << "roll_type:" << format << std::endl;
std::cout << "reserve_count:" << format << std::endl;
std::cout << "roll_size by size:" << format << std::endl;
std::cout << "rotation_hour by day:" << format << std::endl;
std::cout << "rotation_minute by day:" << format << std::endl;
std::cout << "remove_timout_file_switch by day:" << format << std::endl;
std::cout << "hour_interval by hour:" << format << std::endl;
}
/// @brief Print the configuration
void LocalFileSinkConfig::Display() const {
std::cout << "format: " << format << std::endl;
std::cout << "eol: " << format << std::endl;
std::cout << "filename: " << format << std::endl;
std::cout << "roll_type: " << format << std::endl;
std::cout << "reserve_count: " << format << std::endl;
std::cout << "roll_size by size: " << format << std::endl;
std::cout << "rotation_hour by day: " << format << std::endl;
std::cout << "rotation_minute by day: " << format << std::endl;
std::cout << "remove_timeout_file_switch by day: " << format << std::endl;
std::cout << "hour_interval by hour: " << format << std::endl;
}

} // namespace trpc
4 changes: 2 additions & 2 deletions trpc/common/config/local_file_sink_conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ struct LocalFileSinkConfig {
unsigned int reserve_count{10};
/// @brief Scroll log size
unsigned int roll_size{1024 * 1024 * 100};
/// @brief represents the time of day, as specified by rotation_hour:rotation_minute
/// @brief Represents the time of day, as specified by rotation_hour:rotation_minute
unsigned int rotation_hour{0};
/// @brief Cut by minutes
unsigned int rotation_minute{0};
/// @brief In split by day mode, remove the identity of obsolete files, not by default
bool remove_timout_file_switch{false};
bool remove_timeout_file_switch{false};

/// @brief Represents an hourly interval in hours, default interval is one hour
unsigned int hour_interval{1};
Expand Down
24 changes: 3 additions & 21 deletions trpc/common/config/local_file_sink_conf_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,6 @@

namespace YAML {

inline bool GetLocalFileNode(std::string_view logger_name, YAML::Node& local_file_node) {
YAML::Node logger_node, sinks_node;
// Check if logger is configured
if (!GetLoggerNode(logger_name, logger_node)) {
return false;
}
// Check if sinks are configured
if (!trpc::ConfigHelper::GetNode(logger_node, {"sinks"}, sinks_node)) {
std::cerr<< "sink not found!" <<std::endl;
return false;
}
// Check if local_file is configured
if (!trpc::ConfigHelper::GetNode(sinks_node, {"local_file"}, local_file_node)) {
return false;
}
return true;
}

template <>
struct convert<trpc::LocalFileSinkConfig> {
static YAML::Node encode(const trpc::LocalFileSinkConfig& config) {
Expand All @@ -49,7 +31,7 @@ struct convert<trpc::LocalFileSinkConfig> {
node["roll_size"] = config.roll_size;
node["rotation_hour"] = config.rotation_hour;
node["rotation_minute"] = config.rotation_minute;
node["remove_timout_file_switch"] = config.remove_timout_file_switch;
node["remove_timeout_file_switch"] = config.remove_timeout_file_switch;
node["hour_interval"] = config.hour_interval;
return node;
}
Expand Down Expand Up @@ -79,8 +61,8 @@ struct convert<trpc::LocalFileSinkConfig> {
if (node["rotation_minute"]) {
config.rotation_minute = node["rotation_minute"].as<unsigned int>();
}
if (node["remove_timout_file_switch"]) {
config.remove_timout_file_switch = node["remove_timout_file_switch"].as<bool>();
if (node["remove_timeout_file_switch"]) {
config.remove_timeout_file_switch = node["remove_timeout_file_switch"].as<bool>();
}
if (node["hour_interval"]) {
config.hour_interval = node["hour_interval"].as<unsigned int>();
Expand Down
27 changes: 7 additions & 20 deletions trpc/common/config/stdout_sink_conf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,19 @@
#include <any>
#include <iostream>

#include "trpc/common/config/default_log_conf_parser.h"
#include "trpc/common/config/stdout_sink_conf.h"
#include "trpc/common/config/stdout_sink_conf_parser.h"

#include "yaml-cpp/yaml.h"

namespace trpc {

/// @brief Get the configuration for the logger local file based on the logger name
template <>
bool GetLoggerConfig<StdoutSinkConfig>(std::string_view logger_name, StdoutSinkConfig& config) {
YAML::Node stdout_node;
// Parse a single Logger through yaml to get nodes in local_file
if (!YAML::GetStdoutNode(logger_name, stdout_node)) {
return false;
}
// Convert node to LocalFileConfig
YAML::convert<StdoutSinkConfig> c;
c.decode(stdout_node, config);
return true;
}

/// @brief Print the configuration
void StdoutSinkConfig::Display() const {
std::cout << "format:" << format << std::endl;
std::cout << "eol:" << eol << std::endl;
std::cout << "stderr_level:" << stderr_level << std::endl;
}
/// @brief Print the configuration
void StdoutSinkConfig::Display() const {
std::cout << "format: " << format << std::endl;
std::cout << "eol: " << eol << std::endl;
std::cout << "stderr_level: " << stderr_level << std::endl;
}

} // namespace trpc
19 changes: 0 additions & 19 deletions trpc/common/config/stdout_sink_conf_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,6 @@

namespace YAML {

bool GetStdoutNode(std::string_view logger_name, YAML::Node& stdout_node) {
YAML::Node logger_node, sinks_node;
// Check if logger is configured
if (!GetLoggerNode(logger_name, logger_node)) {
std::cerr << "Get loggerNode err: " << "logger_name" << logger_name << std::endl;
return false;
}
// Check if sinks are configured
if (!trpc::ConfigHelper::GetNode(logger_node, {"sinks"}, sinks_node)) {
std::cerr << "sink not found!" << std::endl;
return false;
}
// Check if local_file is configured
if (!trpc::ConfigHelper::GetNode(sinks_node, {"stdout"}, stdout_node)) {
return false;
}
return true;
}

template <>
struct convert<trpc::StdoutSinkConfig> {
static YAML::Node encode(const trpc::StdoutSinkConfig& config) {
Expand Down
7 changes: 6 additions & 1 deletion trpc/log/logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

#pragma once

#include "spdlog/pattern_formatter.h"

#include "trpc/common/plugin.h"
#include "trpc/util/log/log.h"

Expand All @@ -22,7 +24,7 @@ class Logging : public Plugin {
public:
/// @brief Gets the logger name.
/// @return Under which logger the remote logging plugin is mounted.
virtual std::string LoggerName() const = 0;
virtual const std::string& LoggerName() const = 0;

/// @brief Gets the plugin type
/// @return plugin type
Expand All @@ -36,6 +38,9 @@ class Logging : public Plugin {
/// @param msg Log message
virtual void Log(const Log::Level level, const char* filename_in, int line_in, const char* funcname_in,
std::string_view msg, const std::unordered_map<uint32_t, std::any>& extend_fields_msg) = 0;

/// @brief Get the pointer of sink instance of spdlog
virtual spdlog::sink_ptr SpdSink() const { return nullptr; }
};

using LoggingPtr = RefPtr<Logging>;
Expand Down
1 change: 1 addition & 0 deletions trpc/util/log/default/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ cc_library(
hdrs = ["default_log.h"],
deps = [
"//trpc/common/config:default_log_conf",
"//trpc/common/config:default_value",
"//trpc/common/config:local_file_sink_conf",
"//trpc/common/config:stdout_sink_conf",
"//trpc/log:logging",
Expand Down
Loading

0 comments on commit 1f475f3

Please sign in to comment.