diff --git a/LFtid1056/cloudfront/code/cfg_parser.cpp b/LFtid1056/cloudfront/code/cfg_parser.cpp index 5cdd353..c34987e 100644 --- a/LFtid1056/cloudfront/code/cfg_parser.cpp +++ b/LFtid1056/cloudfront/code/cfg_parser.cpp @@ -3362,7 +3362,7 @@ bool send_file_list(terminal_dev* dev, const std::vector& FileList // 构造 JSON 报文 nlohmann::json j; j["guid"] = dev->guid; - j["FrontIP"] = FRONT_IP; // 这里填你的前置机 IP + j["FrontId"] = FRONT_INST; // 这里填你的前置机id j["Node"] = g_front_seg_index; // 节点号 j["Dev_mac"] = normalize_mac(dev->addr_str); // addr_str 存的是 MAC @@ -3736,7 +3736,7 @@ bool send_set_value_reply(const std::string &dev_id, unsigned char mp_index, con // 顶层 j["guid"] = dev.guid; - j["FrontIP"] = FRONT_IP; // 你的前置机 IP(项目已有常量/变量) + j["FrontId"] = FRONT_INST; // 你的前置机 IP(项目已有常量/变量) j["Node"] = g_front_seg_index; // 节点号(项目已有变量) j["Dev_mac"] = normalize_mac(dev.addr_str); @@ -3861,7 +3861,7 @@ bool send_internal_value_reply(const std::string &dev_id, const std::vector #include #include +#include /////////////////////////////////////////////////////////////////////////////////////////// @@ -695,9 +696,23 @@ inline std::string sanitize(std::string s) { } return s; } + +// 当前本地时间,格式 "YYYY-MM-DD HH:MM:SS" +inline std::string now_yyyy_mm_dd_hh_mm_ss() { + std::time_t t = std::time(nullptr); + std::tm tmv; +#if defined(_WIN32) + localtime_s(&tmv, &t); // Windows 线程安全 +#else + localtime_r(&t, &tmv); // POSIX 线程安全 +#endif + std::ostringstream oss; + oss << std::put_time(&tmv, "%Y-%m-%d %H:%M:%S"); + return oss.str(); +} ////////////////////////////////////////////////////////////////////////////////////////////////////////////////// extern int g_front_seg_index; -extern std::string FRONT_IP; +extern std::string FRONT_INST; extern std::string FRONT_PATH; extern std::string WEB_FILEUPLOAD; diff --git a/LFtid1056/cloudfront/code/log4.cpp b/LFtid1056/cloudfront/code/log4.cpp index d86b519..7b3453b 100644 --- a/LFtid1056/cloudfront/code/log4.cpp +++ b/LFtid1056/cloudfront/code/log4.cpp @@ -150,6 +150,7 @@ protected: << "\",\"nodeId\":\"" << FRONT_INST << "\",\"businessId\":\"" << extract_logger_id(logger_name) << "\",\"level\":\"" << level_str + << "\",\"time\":\"" << now_yyyy_mm_dd_hh_mm_ss() << "\",\"grade\":\"" << get_level_str(level) // ★新增:输出 code 字段(整型) << "\",\"code\":\"" << code @@ -485,17 +486,27 @@ extern "C" { } //标准化日志接口 + // #define LOGMSG_WITH_TS // 需要时间时再打开 + void format_log_msg(char* buf, size_t buf_size, const char* fmt, ...) { + if (!buf || buf_size == 0) return; + buf[0] = '\0'; + if (!fmt) return; + + va_list args; + va_start(args, fmt); + #ifdef LOGMSG_WITH_TS // 写入时间 time_t now = time(NULL); struct tm tm_info; localtime_r(&now, &tm_info); - strftime(buf, buf_size, "%Y-%m-%d %H:%M:%S ", &tm_info); // 时间+空格 - - // 处理可变参数并写入剩余内容 - va_list args; - va_start(args, fmt); - vsnprintf(buf + strlen(buf), buf_size - strlen(buf), fmt, args); + size_t n = strftime(buf, buf_size, "%Y-%m-%d %H:%M:%S ", &tm_info); + if (n < buf_size) { + vsnprintf(buf + n, buf_size - n, fmt, args); + } + #else + vsnprintf(buf, buf_size, fmt, args); + #endif va_end(args); } diff --git a/LFtid1056/cloudfront/code/log4.h b/LFtid1056/cloudfront/code/log4.h index c4d47c8..b86b126 100644 --- a/LFtid1056/cloudfront/code/log4.h +++ b/LFtid1056/cloudfront/code/log4.h @@ -122,17 +122,10 @@ typedef enum LogCode { LOG_CODE_REPORT = 500, /* 报告处理 */ LOG_CODE_COMM = 600, /* 通讯状态 */ LOG_CODE_SPACE_ALARM = 700, /* 空间告警 */ - LOG_CODE_DEV_ALARM = 800 /* 装置告警 */ + LOG_CODE_DEV_ALARM = 800 /* 设备告警 */ } LogCode; // ====================== 日志宏区域 ====================== -// 原始不带 code 的实现(兼容/复用) -#define DIY_LOG(LEVEL_FUNC, KEY, ...) \ - do { \ - char buf[256]; \ - format_log_msg(buf, sizeof(buf), __VA_ARGS__); \ - LEVEL_FUNC(KEY, buf); \ - } while (0) // ★新增:带 code 的实现(C/C++ 通用,使用 TLS 保存/恢复) #define DIY_LOG_CODE(LEVEL_FUNC, KEY, LEVEL_INT, CODE_INT, ...) \ diff --git a/LFtid1056/cloudfront/code/rocketmq.cpp b/LFtid1056/cloudfront/code/rocketmq.cpp index 4c2d16d..9ac2037 100644 --- a/LFtid1056/cloudfront/code/rocketmq.cpp +++ b/LFtid1056/cloudfront/code/rocketmq.cpp @@ -1849,7 +1849,7 @@ void send_reply_to_cloud(int reply_code, const std::string& dev_id, int type, co // ---- 构造根 JSON ---- nlohmann::json obj; obj["guid"] = guid; - obj["FrontIP"] = FRONT_IP; + obj["FrontId"] = FRONT_INST; obj["Node"] = g_front_seg_index; // Dev_mac:从台账取 addr_str 并规范化 @@ -1918,11 +1918,11 @@ rocketmq::ConsumeStatus cloudMessageCallback(const rocketmq::MQMessageExt& msg) // 消息解析 std::string guid; std::string devid; - std::string FrontIP; + std::string FrontId; int Node; nlohmann::json DetailObj; - if (!parseJsonMessageCLOUD(body, devid, guid, DetailObj,FrontIP,Node)) { + if (!parseJsonMessageCLOUD(body, devid, guid, DetailObj,FrontId,Node)) { std::cerr << "Failed to parse the JSON message." << std::endl; DIY_ERRORLOG("process", "【ERROR】前置消费topic:%s_%s的云前置控制消息失败,消息的json格式不正确", FRONT_INST.c_str(), G_MQCONSUMER_TOPIC_RT.c_str()); return rocketmq::RECONSUME_LATER; @@ -1932,11 +1932,11 @@ rocketmq::ConsumeStatus cloudMessageCallback(const rocketmq::MQMessageExt& msg) std::cout << "[CLOUD Msg Parsed] " << "guid=" << guid << ", devid=" << devid - << ", FrontIP=" << FrontIP + << ", FrontId=" << FrontId << ", Node=" << Node << std::endl; - if(FrontIP != FRONT_IP || Node != g_front_seg_index){ + if(FrontId != FRONT_INST || Node != g_front_seg_index){ std::cout << "当前进程不消费这个消息" << std::endl; return rocketmq::CONSUME_SUCCESS; }