add connect interface and log4 with code
This commit is contained in:
@@ -1227,7 +1227,7 @@ int recall_json_handle(const std::string& jstr) {
|
||||
jr.STEADY = rp.STEADY;
|
||||
jr.VOLTAGE = rp.VOLTAGE;
|
||||
|
||||
std::lock_guard<std::mutex> lk(g_StatisticLackList_list_mutex);
|
||||
std::lock_guard<std::mutex> lock(g_StatisticLackList_list_mutex);
|
||||
g_StatisticLackList.push_back(jr);
|
||||
}
|
||||
}
|
||||
@@ -3549,7 +3549,7 @@ bool send_set_value_reply(const std::string &dev_id, unsigned char mp_index, con
|
||||
connect_info.strText = j.dump(); // 序列化为字符串
|
||||
|
||||
{
|
||||
std::lock_guard<std::mutex> lk(queue_data_list_mutex);
|
||||
std::lock_guard<std::mutex> lock(queue_data_list_mutex);
|
||||
queue_data_list.push_back(std::move(connect_info));
|
||||
}
|
||||
|
||||
@@ -3745,7 +3745,7 @@ bool send_internal_value_reply(const std::string &dev_id, const std::vector<DZ_k
|
||||
connect_info.strText = j.dump(); // 序列化为字符串
|
||||
|
||||
{
|
||||
std::lock_guard<std::mutex> lk(queue_data_list_mutex);
|
||||
std::lock_guard<std::mutex> lock(queue_data_list_mutex);
|
||||
queue_data_list.push_back(std::move(connect_info));
|
||||
}
|
||||
|
||||
|
||||
@@ -535,6 +535,9 @@ bool save_set_value(const std::string &dev_id, unsigned char mp_index, const std
|
||||
//发送文件
|
||||
void SendFileWeb(const std::string& strUrl, const std::string& localpath, const std::string& cloudpath, std::string& wavepath);
|
||||
|
||||
//状态翻转
|
||||
void connect_status_update(const std::string& id, int status);
|
||||
|
||||
//小工具
|
||||
inline std::string trim_cstr(const char* s, size_t n) {
|
||||
if (!s) return {};
|
||||
|
||||
@@ -129,6 +129,10 @@ bool DebugSwitch::match(const std::string& logger_name, int level, int logtype)
|
||||
std::map<std::string, TypedLogger> 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) {
|
||||
@@ -145,6 +149,9 @@ protected:
|
||||
else
|
||||
level_str = "terminal";
|
||||
|
||||
// ★读取 TLS 中的 code(在打日志的线程里由宏设定)
|
||||
int code = g_log_code_tls; // 若未显式传入,则为 0
|
||||
|
||||
if (level == ERROR_LOG_LEVEL || level == WARN_LOG_LEVEL || g_debug_switch.match(logger_name, level, logtype)) {
|
||||
std::ostringstream oss;
|
||||
oss << "{\"processNo\":\"" << std::to_string(g_front_seg_index)
|
||||
@@ -154,6 +161,8 @@ protected:
|
||||
<< "\",\"grade\":\"" << get_level_str(level)
|
||||
<< "\",\"logtype\":\"" << (logtype == LOGTYPE_COM ? "com" : "data")
|
||||
<< "\",\"frontType\":\"" << "cloudfront"
|
||||
// ★新增:输出 code 字段(整型)
|
||||
<< "\",\"code\":\"" << code
|
||||
<< "\",\"log\":\"" << escape_json(msg) << "\"}";
|
||||
|
||||
std::string jsonString = oss.str();
|
||||
|
||||
@@ -88,7 +88,16 @@ void log_error(const char* key, const char* msg);
|
||||
void send_reply_to_queue_c(const char* guid, const char* step, const char* result);
|
||||
void format_log_msg(char* buf, size_t buf_size, const char* fmt, ...);
|
||||
|
||||
//宏定义
|
||||
// ====================== ★新增:线程局部变量透传 code ======================
|
||||
// 说明:使用编译器的 TLS(__thread)保存当前日志的 code 值。
|
||||
// 在每次打日志前写入,打完后恢复,Appender 读取该值写入 JSON。
|
||||
|
||||
extern int g_log_code_tls; // 声明为 TLS 变量,定义见 log4.cpp
|
||||
// ====================== ★新增结束 ======================
|
||||
|
||||
|
||||
// ====================== 日志宏区域 ======================
|
||||
// 原始不带 code 的实现(兼容/复用)
|
||||
#define DIY_LOG(LEVEL_FUNC, KEY, ...) \
|
||||
do { \
|
||||
char buf[256]; \
|
||||
@@ -96,10 +105,52 @@ void format_log_msg(char* buf, size_t buf_size, const char* fmt, ...);
|
||||
LEVEL_FUNC(KEY, buf); \
|
||||
} while (0)
|
||||
|
||||
#define DIY_ERRORLOG(KEY, ...) DIY_LOG(log_error, KEY, __VA_ARGS__)
|
||||
#define DIY_WARNLOG(KEY, ...) DIY_LOG(log_warn, KEY, __VA_ARGS__)
|
||||
#define DIY_INFOLOG(KEY, ...) DIY_LOG(log_info, KEY, __VA_ARGS__)
|
||||
#define DIY_DEBUGLOG(KEY, ...) DIY_LOG(log_debug, KEY, __VA_ARGS__)
|
||||
// ★新增:带 code 的实现(C/C++ 通用,使用 TLS 保存/恢复)
|
||||
#define DIY_LOG_CODE(LEVEL_FUNC, KEY, CODE_INT, ...) \
|
||||
do { \
|
||||
int __old_code__ = g_log_code_tls; /* 备份旧值 */ \
|
||||
g_log_code_tls = (int)(CODE_INT); /* 设置本次日志 code */ \
|
||||
char buf[256]; \
|
||||
format_log_msg(buf, sizeof(buf), __VA_ARGS__); \
|
||||
LEVEL_FUNC(KEY, buf); /* 输出日志 */ \
|
||||
g_log_code_tls = __old_code__; /* 恢复旧值 */ \
|
||||
} while (0)
|
||||
|
||||
// ★修改:默认宏改为 code=0(兼容原有用法)
|
||||
#define DIY_ERRORLOG(KEY, ...) DIY_LOG_CODE(log_error, KEY, 0, __VA_ARGS__) // ★修改:默认 code=0
|
||||
#define DIY_WARNLOG(KEY, ...) DIY_LOG_CODE(log_warn, KEY, 0, __VA_ARGS__) // ★修改:默认 code=0
|
||||
#define DIY_INFOLOG(KEY, ...) DIY_LOG_CODE(log_info, KEY, 0, __VA_ARGS__) // ★修改:默认 code=0
|
||||
#define DIY_DEBUGLOG(KEY, ...) DIY_LOG_CODE(log_debug, KEY, 0, __VA_ARGS__) // ★修改:默认 code=0
|
||||
|
||||
// ★新增:显式传入 code 的便捷宏
|
||||
// 用法示例:DIY_WARNLOG_CODE(full_key_m_c, warn_recallstart, "【WARN】监测点:%s ...", ...);
|
||||
#define DIY_ERRORLOG_CODE(KEY, CODE_INT, ...) DIY_LOG_CODE(log_error, KEY, CODE_INT, __VA_ARGS__) // ★新增
|
||||
#define DIY_WARNLOG_CODE(KEY, CODE_INT, ...) DIY_LOG_CODE(log_warn, KEY, CODE_INT, __VA_ARGS__) // ★新增
|
||||
#define DIY_INFOLOG_CODE(KEY, CODE_INT, ...) DIY_LOG_CODE(log_info, KEY, CODE_INT, __VA_ARGS__) // ★新增
|
||||
#define DIY_DEBUGLOG_CODE(KEY, CODE_INT, ...) DIY_LOG_CODE(log_debug, KEY, CODE_INT, __VA_ARGS__) // ★新增
|
||||
|
||||
|
||||
// ====================== 日志宏区域 ======================
|
||||
|
||||
|
||||
typedef enum LogCode {
|
||||
LOG_CODE_OTHER = 99, /* 其他类型 */
|
||||
LOG_CODE_LEDGER = 100, /* 台账类型 */
|
||||
LOG_CODE_RPTINIT = 101, /* 报告初始化 */
|
||||
LOG_CODE_ICD_AND_DOWNLOAD = 200, /* ICD 和文件下载类型 */
|
||||
LOG_CODE_TRANSIENT = 300, /* 暂态发生 */
|
||||
LOG_CODE_TRANSIENT_COMM = 301, /* 暂态接口 */
|
||||
LOG_CODE_COMTRADE_FILE = 302, /* 录波文件(Comtrade) */
|
||||
LOG_CODE_MQ = 400, /* MQ发送 */
|
||||
LOG_CODE_RT_DATA = 401, /* 实时数据 */
|
||||
LOG_CODE_LEDGER_UPDATE = 402, /* 台账更新 */
|
||||
LOG_CODE_PROCESS_CONTROL = 403, /* 进程控制 */
|
||||
LOG_CODE_RECALL = 404, /* 补招相关 */
|
||||
LOG_CODE_LOG_REQUEST = 405, /* 日志请求 */
|
||||
LOG_CODE_REPORT = 500, /* 报告处理 */
|
||||
LOG_CODE_COMM = 600, /* 通讯状态 */
|
||||
LOG_CODE_SPACE_ALARM = 700 /* 空间告警 */
|
||||
} LogCode;
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
||||
@@ -320,6 +320,8 @@ void Front::FrontThread() {
|
||||
//check_3s_config(); // 实时数据触发
|
||||
//create_recall_xml(); // 生成待补招xml文件
|
||||
check_ledger_update(); // 触发台账更新
|
||||
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||
}
|
||||
} catch (const std::exception& e) {
|
||||
std::cerr << "[FrontThread] Caught exception: " << e.what() << std::endl;
|
||||
|
||||
@@ -2040,3 +2040,36 @@ void rocketmq_test_getdir(Front* front)//用来测试目录获取
|
||||
std::lock_guard<std::mutex> lock(queue_data_list_mutex);
|
||||
queue_data_list.push_back(data);
|
||||
}
|
||||
|
||||
//状态翻转
|
||||
void connect_status_update(const std::string& id, int status)
|
||||
{
|
||||
// 获取当前系统时间(格式: YYYY-MM-DD HH:MM:SS)
|
||||
auto now = std::chrono::system_clock::now();
|
||||
std::time_t now_c = std::chrono::system_clock::to_time_t(now);
|
||||
std::tm tm_buf;
|
||||
localtime_r(&now_c, &tm_buf);
|
||||
|
||||
std::ostringstream datetime_ss;
|
||||
datetime_ss << std::put_time(&tm_buf, "%Y-%m-%d %H:%M:%S");
|
||||
std::string datetime_str = datetime_ss.str();
|
||||
|
||||
// 构造 JSON 对象
|
||||
nlohmann::json j;
|
||||
j["id"] = id;
|
||||
j["date"] = datetime_str;
|
||||
j["status"] = std::to_string(status);
|
||||
|
||||
// 构造队列消息
|
||||
queue_data_t connect_info;
|
||||
connect_info.strTopic = G_CONNECT_TOPIC;
|
||||
connect_info.strText = j.dump(); // 转成字符串
|
||||
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(queue_data_list_mutex);
|
||||
queue_data_list.push_back(std::move(connect_info));
|
||||
}
|
||||
|
||||
// 调试打印
|
||||
std::cout << "[connect_status_update] queued JSON:\n" << j.dump(4) << std::endl;
|
||||
}
|
||||
Reference in New Issue
Block a user