fix: update logger (#80)

This commit is contained in:
wtudio 2024-11-04 18:54:47 +08:00 committed by GitHub
parent 076f76394e
commit 7ba8f93fdf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 18 additions and 20 deletions

View File

@ -3,16 +3,12 @@
#include "core/logger/console_logger_backend.h"
#include <chrono>
#include <iostream>
#include <mutex>
#include <regex>
#include "core/logger/formatter.h"
#include "core/logger/log_level_tool.h"
#include "util/exception.h"
#include "util/format.h"
#include "util/time_util.h"
#if defined(_WIN32)
#include <windows.h>

View File

@ -69,7 +69,7 @@ void LoggerManager::Initialize(YAML::Node options_node) {
if (options_node && !options_node.IsNull())
options_ = options_node.as<Options>();
// 生成logger
// create logger
for (auto& backend_options : options_.backends_options) {
auto finditr = logger_backend_gen_func_map_.find(backend_options.type);
AIMRT_CHECK_ERROR_THROW(finditr != logger_backend_gen_func_map_.end(),
@ -121,13 +121,13 @@ void LoggerManager::Shutdown() {
AIMRT_INFO("Logger manager shutdown.");
// logger_proxy_map_不能清,有些插件还会打日志
// logger_proxy_map_ cannot be cleared, some plugins still generate logs
for (auto& backend : logger_backend_vec_) {
backend->Shutdown();
}
// logger_backend不能清,可能会有未完成的日志任务
// logger_backend cannot be cleared, there may be unfinished log tasks
logger_backend_gen_func_map_.clear();
@ -155,10 +155,9 @@ void LoggerManager::RegisterLoggerBackendGenFunc(
const LoggerProxy& LoggerManager::GetLoggerProxy(const util::ModuleDetailInfo& module_info) {
AIMRT_CHECK_ERROR_THROW(
state_.load() == State::kInit || state_.load() == State::kStart,
"Method can only be called when state is 'Init' or 'Start'.");
state_.load() == State::kInit,
"Method can only be called when state is 'Init'.");
// module_name为空等效于aimrt节点
const std::string& real_module_name =
(module_info.name.empty()) ? "core" : module_info.name;
@ -180,17 +179,16 @@ const LoggerProxy& LoggerManager::GetLoggerProxy(const util::ModuleDetailInfo& m
const LoggerProxy& LoggerManager::GetLoggerProxy(std::string_view logger_name) {
AIMRT_CHECK_ERROR_THROW(
state_.load() == State::kInit || state_.load() == State::kStart,
"Method can only be called when state is 'Init' or 'Start'.");
state_.load() == State::kInit,
"Method can only be called when state is 'Init'.");
// logger_name为空等效于core节点
const std::string& real_logger_name =
(logger_name.empty()) ? "core" : std::string(logger_name);
auto itr = logger_proxy_map_.find(real_logger_name);
if (itr != logger_proxy_map_.end()) return *(itr->second);
// 统一使用core_lvl
// use core_lvl
auto emplace_ret = logger_proxy_map_.emplace(
real_logger_name,
std::make_unique<LoggerProxy>(real_logger_name, options_.core_lvl, logger_backend_vec_));
@ -199,6 +197,10 @@ const LoggerProxy& LoggerManager::GetLoggerProxy(std::string_view logger_name) {
}
std::unordered_map<std::string, aimrt_log_level_t> LoggerManager::GetAllLoggerLevels() const {
AIMRT_CHECK_ERROR_THROW(
state_.load() == State::kStart,
"Method can only be called when state is 'Start'.");
std::unordered_map<std::string, aimrt_log_level_t> result;
for (const auto& itr : logger_proxy_map_) {
result.emplace(itr.first, itr.second->LogLevel());
@ -208,6 +210,10 @@ std::unordered_map<std::string, aimrt_log_level_t> LoggerManager::GetAllLoggerLe
void LoggerManager::SetLoggerLevels(
const std::unordered_map<std::string, aimrt_log_level_t>& logger_lvls) {
AIMRT_CHECK_ERROR_THROW(
state_.load() == State::kStart,
"Method can only be called when state is 'Start'.");
for (const auto& itr : logger_lvls) {
auto find_itr = logger_proxy_map_.find(itr.first);
if (find_itr == logger_proxy_map_.end()) continue;

View File

@ -34,7 +34,7 @@ class LoggerProxy {
const aimrt_logger_base_t* NativeHandle() const { return &base_; }
// 这里不用atomic也不用加锁对修改的实时性要求不高
// There is no need for atomic or locking here, the real-time modification requirements for lvl_ are not high
aimrt_log_level_t LogLevel() const { return lvl_; }
void SetLogLevel(aimrt_log_level_t lvl) { lvl_ = lvl; }

View File

@ -3,17 +3,13 @@
#include "core/logger/rotate_file_logger_backend.h"
#include <chrono>
#include <filesystem>
#include <map>
#include <mutex>
#include <regex>
#include "core/logger/log_level_tool.h"
#include "util/exception.h"
#include "util/format.h"
#include "util/string_util.h"
#include "util/time_util.h"
namespace YAML {
template <>

View File

@ -56,7 +56,7 @@ class RotateFileLoggerBackend : public LoggerBackendBase {
std::function<aimrt::executor::ExecutorRef(std::string_view)> get_executor_func_;
executor::ExecutorRef log_executor_;
std::string base_file_name_; // 基础文件路径
std::string base_file_name_;
std::ofstream ofs_;
std::atomic_bool run_flag_ = false;