modify logs
This commit is contained in:
@@ -3362,7 +3362,7 @@ bool send_file_list(terminal_dev* dev, const std::vector<tag_dir_info>& 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<DZ_k
|
||||
// 3) 组包顶层
|
||||
nlohmann::json j;
|
||||
j["guid"] = dev.guid;
|
||||
j["FrontIP"] = FRONT_IP;
|
||||
j["FrontId"] = FRONT_INST;
|
||||
j["Node"] = g_front_seg_index;
|
||||
j["Dev_mac"] = normalize_mac(dev.addr_str);
|
||||
|
||||
|
||||
@@ -703,7 +703,7 @@ int parse_device_cfg_web()
|
||||
|
||||
// 1. 构造入参 JSON
|
||||
std::string input_jstr = "{";
|
||||
input_jstr += "\"ip\":\"" + FRONT_IP + "\",";
|
||||
input_jstr += "\"id\":\"" + FRONT_INST + "\",";
|
||||
input_jstr += "\"runFlag\":" + TERMINAL_STATUS;
|
||||
input_jstr += "}";
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
#include <array>
|
||||
#include <map>
|
||||
#include <mutex>
|
||||
#include <sstream>
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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, ...) \
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user