fix: update logger (#80)
This commit is contained in:
parent
076f76394e
commit
7ba8f93fdf
@ -3,16 +3,12 @@
|
|||||||
|
|
||||||
#include "core/logger/console_logger_backend.h"
|
#include "core/logger/console_logger_backend.h"
|
||||||
|
|
||||||
#include <chrono>
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <regex>
|
#include <regex>
|
||||||
|
|
||||||
#include "core/logger/formatter.h"
|
#include "core/logger/formatter.h"
|
||||||
#include "core/logger/log_level_tool.h"
|
|
||||||
#include "util/exception.h"
|
#include "util/exception.h"
|
||||||
#include "util/format.h"
|
|
||||||
#include "util/time_util.h"
|
|
||||||
|
|
||||||
#if defined(_WIN32)
|
#if defined(_WIN32)
|
||||||
#include <windows.h>
|
#include <windows.h>
|
||||||
|
@ -69,7 +69,7 @@ void LoggerManager::Initialize(YAML::Node options_node) {
|
|||||||
if (options_node && !options_node.IsNull())
|
if (options_node && !options_node.IsNull())
|
||||||
options_ = options_node.as<Options>();
|
options_ = options_node.as<Options>();
|
||||||
|
|
||||||
// 生成logger
|
// create logger
|
||||||
for (auto& backend_options : options_.backends_options) {
|
for (auto& backend_options : options_.backends_options) {
|
||||||
auto finditr = logger_backend_gen_func_map_.find(backend_options.type);
|
auto finditr = logger_backend_gen_func_map_.find(backend_options.type);
|
||||||
AIMRT_CHECK_ERROR_THROW(finditr != logger_backend_gen_func_map_.end(),
|
AIMRT_CHECK_ERROR_THROW(finditr != logger_backend_gen_func_map_.end(),
|
||||||
@ -121,13 +121,13 @@ void LoggerManager::Shutdown() {
|
|||||||
|
|
||||||
AIMRT_INFO("Logger manager 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_) {
|
for (auto& backend : logger_backend_vec_) {
|
||||||
backend->Shutdown();
|
backend->Shutdown();
|
||||||
}
|
}
|
||||||
|
|
||||||
// logger_backend不能清,可能会有未完成的日志任务
|
// logger_backend cannot be cleared, there may be unfinished log tasks
|
||||||
|
|
||||||
logger_backend_gen_func_map_.clear();
|
logger_backend_gen_func_map_.clear();
|
||||||
|
|
||||||
@ -155,10 +155,9 @@ void LoggerManager::RegisterLoggerBackendGenFunc(
|
|||||||
|
|
||||||
const LoggerProxy& LoggerManager::GetLoggerProxy(const util::ModuleDetailInfo& module_info) {
|
const LoggerProxy& LoggerManager::GetLoggerProxy(const util::ModuleDetailInfo& module_info) {
|
||||||
AIMRT_CHECK_ERROR_THROW(
|
AIMRT_CHECK_ERROR_THROW(
|
||||||
state_.load() == State::kInit || state_.load() == State::kStart,
|
state_.load() == State::kInit,
|
||||||
"Method can only be called when state is 'Init' or 'Start'.");
|
"Method can only be called when state is 'Init'.");
|
||||||
|
|
||||||
// module_name为空等效于aimrt节点
|
|
||||||
const std::string& real_module_name =
|
const std::string& real_module_name =
|
||||||
(module_info.name.empty()) ? "core" : module_info.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) {
|
const LoggerProxy& LoggerManager::GetLoggerProxy(std::string_view logger_name) {
|
||||||
AIMRT_CHECK_ERROR_THROW(
|
AIMRT_CHECK_ERROR_THROW(
|
||||||
state_.load() == State::kInit || state_.load() == State::kStart,
|
state_.load() == State::kInit,
|
||||||
"Method can only be called when state is 'Init' or 'Start'.");
|
"Method can only be called when state is 'Init'.");
|
||||||
|
|
||||||
// logger_name为空等效于core节点
|
|
||||||
const std::string& real_logger_name =
|
const std::string& real_logger_name =
|
||||||
(logger_name.empty()) ? "core" : std::string(logger_name);
|
(logger_name.empty()) ? "core" : std::string(logger_name);
|
||||||
|
|
||||||
auto itr = logger_proxy_map_.find(real_logger_name);
|
auto itr = logger_proxy_map_.find(real_logger_name);
|
||||||
if (itr != logger_proxy_map_.end()) return *(itr->second);
|
if (itr != logger_proxy_map_.end()) return *(itr->second);
|
||||||
|
|
||||||
// 统一使用core_lvl
|
// use core_lvl
|
||||||
auto emplace_ret = logger_proxy_map_.emplace(
|
auto emplace_ret = logger_proxy_map_.emplace(
|
||||||
real_logger_name,
|
real_logger_name,
|
||||||
std::make_unique<LoggerProxy>(real_logger_name, options_.core_lvl, logger_backend_vec_));
|
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 {
|
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;
|
std::unordered_map<std::string, aimrt_log_level_t> result;
|
||||||
for (const auto& itr : logger_proxy_map_) {
|
for (const auto& itr : logger_proxy_map_) {
|
||||||
result.emplace(itr.first, itr.second->LogLevel());
|
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(
|
void LoggerManager::SetLoggerLevels(
|
||||||
const std::unordered_map<std::string, aimrt_log_level_t>& logger_lvls) {
|
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) {
|
for (const auto& itr : logger_lvls) {
|
||||||
auto find_itr = logger_proxy_map_.find(itr.first);
|
auto find_itr = logger_proxy_map_.find(itr.first);
|
||||||
if (find_itr == logger_proxy_map_.end()) continue;
|
if (find_itr == logger_proxy_map_.end()) continue;
|
||||||
|
@ -34,7 +34,7 @@ class LoggerProxy {
|
|||||||
|
|
||||||
const aimrt_logger_base_t* NativeHandle() const { return &base_; }
|
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_; }
|
aimrt_log_level_t LogLevel() const { return lvl_; }
|
||||||
void SetLogLevel(aimrt_log_level_t lvl) { lvl_ = lvl; }
|
void SetLogLevel(aimrt_log_level_t lvl) { lvl_ = lvl; }
|
||||||
|
|
||||||
|
@ -3,17 +3,13 @@
|
|||||||
|
|
||||||
#include "core/logger/rotate_file_logger_backend.h"
|
#include "core/logger/rotate_file_logger_backend.h"
|
||||||
|
|
||||||
#include <chrono>
|
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <regex>
|
#include <regex>
|
||||||
|
|
||||||
#include "core/logger/log_level_tool.h"
|
|
||||||
#include "util/exception.h"
|
#include "util/exception.h"
|
||||||
#include "util/format.h"
|
|
||||||
#include "util/string_util.h"
|
#include "util/string_util.h"
|
||||||
#include "util/time_util.h"
|
|
||||||
|
|
||||||
namespace YAML {
|
namespace YAML {
|
||||||
template <>
|
template <>
|
||||||
|
@ -56,7 +56,7 @@ class RotateFileLoggerBackend : public LoggerBackendBase {
|
|||||||
std::function<aimrt::executor::ExecutorRef(std::string_view)> get_executor_func_;
|
std::function<aimrt::executor::ExecutorRef(std::string_view)> get_executor_func_;
|
||||||
executor::ExecutorRef log_executor_;
|
executor::ExecutorRef log_executor_;
|
||||||
|
|
||||||
std::string base_file_name_; // 基础文件路径
|
std::string base_file_name_;
|
||||||
std::ofstream ofs_;
|
std::ofstream ofs_;
|
||||||
|
|
||||||
std::atomic_bool run_flag_ = false;
|
std::atomic_bool run_flag_ = false;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user