From 5d2059799305a118fb7f574a10f675862e102890 Mon Sep 17 00:00:00 2001 From: lnk Date: Wed, 10 Sep 2025 17:09:12 +0800 Subject: [PATCH] fix event fun --- LFtid1056/cloudfront/code/log4.cpp | 25 +++++++++---------------- LFtid1056/cloudfront/code/log4.h | 20 +++++++++++++++++--- 2 files changed, 26 insertions(+), 19 deletions(-) diff --git a/LFtid1056/cloudfront/code/log4.cpp b/LFtid1056/cloudfront/code/log4.cpp index d866c23..b222dac 100644 --- a/LFtid1056/cloudfront/code/log4.cpp +++ b/LFtid1056/cloudfront/code/log4.cpp @@ -50,19 +50,16 @@ extern std::string subdir; //日志主题 extern std::string G_LOG_TOPIC; +////////////////////////////////////////////////////////// +/* log4.cpp 顶部 */ +#if __cplusplus >= 201103L + thread_local int g_log_code_tls = 0; +#else + __thread int g_log_code_tls = 0; +#endif + ////////////////////////////////////////////////////////辅助函数 -/*std::string get_front_type_from_subdir() { - if (subdir == "cfg_3s_data") - return "realTime"; - else if (subdir == "cfg_soe_comtrade") - return "comtrade"; - else if (subdir == "cfg_recallhis_data") - return "recall"; - else if (subdir == "cfg_stat_data") - return "stat"; - else - return "unknown"; -}*/ + // 递归创建目录 bool create_directory_recursive(const std::string& path) { @@ -129,10 +126,6 @@ bool DebugSwitch::match(const std::string& logger_name, int level, int logtype) std::map logger_map; DebugSwitch g_debug_switch; -// ★新增:定义 TLS 变量,默认 0 - -thread_local int g_log_code_tls = 0; - class SendAppender : public Appender { protected: void append(const spi::InternalLoggingEvent& event) { diff --git a/LFtid1056/cloudfront/code/log4.h b/LFtid1056/cloudfront/code/log4.h index 581ff4e..5e05682 100644 --- a/LFtid1056/cloudfront/code/log4.h +++ b/LFtid1056/cloudfront/code/log4.h @@ -1,6 +1,16 @@ #ifndef LOG4_H #define LOG4_H +#if defined(__cplusplus) && __cplusplus >= 201103L + #define LOG_TLS_SPEC thread_local +#else + /* C 或旧编译器,走 GCC 扩展 */ + #define LOG_TLS_SPEC __thread +#endif + +/* ★统一的对外声明(别在其他头/源里再声明不同版本!) */ +extern LOG_TLS_SPEC int g_log_code_tls; + #ifdef __cplusplus #include @@ -73,7 +83,8 @@ void process_log_command(const std::string& id, const std::string& level, const void update_log_entries_countdown(); - +extern "C" { +#endif void remove_loggers_by_terminal_id(const std::string& terminal_id_cstr); void init_logger_process(); void init_loggers(); @@ -91,7 +102,7 @@ void format_log_msg(char* buf, size_t buf_size, const char* fmt, ...); // 说明:使用编译器的 TLS(__thread)保存当前日志的 code 值。 // 在每次打日志前写入,打完后恢复,Appender 读取该值写入 JSON。 -extern int g_log_code_tls; // 声明为 TLS 变量,定义见 log4.cpp + // ====================== ★新增结束 ====================== @@ -151,7 +162,10 @@ typedef enum LogCode { LOG_CODE_SPACE_ALARM = 700 /* 空间告警 */ } LogCode; -#endif // __cplusplus + +#ifdef __cplusplus +} +#endif