#include "log_builder.h" #include #include #include #include "cJSON.h" #include #include "inner_log.h" /** * adjust buffer, this function will ensure tag's buffer size >= tag->now_buffer_len + new_len * @param tag * @param new_len new buffer len */ void _adjust_buffer(build_item* tag, uint32_t new_len) { if (tag->buffer == NULL) { tag->buffer = (char*)malloc(new_len); memset(tag->buffer, 0, new_len); tag->buffer_len = new_len; return; } tag->buffer = (char*)realloc(tag->buffer, new_len); tag->buffer_len = new_len; } void _adjust_buffer2(log_buf* tag, uint32_t new_len) { if (tag->buffer == NULL) { tag->buffer = (char*)malloc(new_len); memset(tag->buffer, 0, new_len); tag->n_buffer = new_len; return; } tag->buffer = (char*)realloc(tag->buffer, new_len); tag->n_buffer = new_len; } int add_beidou_log(log_group_builder* bder, beidou_log_item* log) { cJSON* united_content; char* str_united_content; int32_t totalBufferSize; build_item* grp_log; long now; char unitedNowTime[64] = { 0 }, nowTime[64] = { 0 }; const char* t_header = "[[tracing]]"; if (bder->grp->n_logs >= MAX_LOG_COUNT) { return -1; } memset(bder->grp->logs[bder->grp->n_logs].uuid, 0, MAX_UUID_LEN); get_format_uuid(bder->grp->logs[bder->grp->n_logs].uuid, MAX_UUID_LEN); now = time(NULL); GetUnitedTimeStr(now, unitedNowTime); #if defined(_MSC_VER) ltoa(now, nowTime, 10); #else sprintf(nowTime, "%ld", now); #endif //_MSC_VER sprintf(nowTime, "%s000", nowTime); united_content = cJSON_CreateObject(); /* 基础格式 { "_CMB_LOG_SPEC_VERSION": "2.0", "level": "INFO", "ts": "2022-10-28T09:18:31.000000000Z", "log": { "content": { "ServName": "Terminal", "CmptId": "LR04", "CmptName": "FrameworkLib", "LogFile": "SI", "EvenTime": "2022-10-28 09:18:31.000", "ClientIP": "25.6.177.60", "Version": "1.0", "DateTime": "2022-10-28 09:18:31.000", "ResultCode": "SUC0000", "LogCode": "", "API": "", "TipMsg": "", "BussID": "", "SourceType": "", "ResultMsg": "userCode: 0x50810005 send", "TraceID": "", "Item": "62", "EntityName": "mod_guiconsole", "LifeID": 82223, "TimeSn": 16437, "TerminalNo": "7715011029", "SN": "7715011029" } }, "type": "BASETYPE", "serviceUnitId": "LR04.02@FrameworkLib_PRD_PRD" } */ //统一日志 { // 应用信息 //cJSON_AddItemToObject(united_content, "appName", cJSON_CreateString(bder->config->unitedAppName)); //cJSON_AddItemToObject(united_content, "_CMB_LOG_SPEC_VERSION", cJSON_CreateString(bder->config->unitedVersion)); //cJSON_AddItemToObject(meta_log_content, "uuid", cJSON_CreateString(bder->grp->logs[bder->grp->n_logs].uuid)); //cJSON_AddItemToObject(meta_log_content, "needArchived", cJSON_CreateBool(bder->config->unitedNeedArchived)); cJSON_AddItemToObject(united_content, "businessId", cJSON_CreateString(log->BussID)); cJSON_AddItemToObject(united_content, "traceId", cJSON_CreateString(log->TraceID)); cJSON_AddItemToObject(united_content, "parentSpanId", cJSON_CreateString(log->ParentSpanId)); cJSON_AddItemToObject(united_content, "spanId", cJSON_CreateString(log->SpanId)); cJSON_AddItemToObject(united_content, "timestamp", cJSON_CreateString(nowTime)); cJSON_AddItemToObject(united_content, "deployUnitId", cJSON_CreateString(bder->config->unitedDeployUnitId)); cJSON_AddItemToObject(united_content, "serviceUnitId", cJSON_CreateString(bder->config->unitedServiceUnitId)); if (log->Host != NULL) { cJSON_AddItemToObject(united_content, "host", cJSON_CreateString(log->Host)); } if (log->Api != NULL) { cJSON_AddItemToObject(united_content, "api", cJSON_CreateString(log->Api)); } if (log->ReturnCode != NULL) { cJSON_AddItemToObject(united_content, "returnCode", cJSON_CreateString(log->ReturnCode)); } if (log->ResponseTime != NULL) { cJSON_AddItemToObject(united_content, "responseTime", cJSON_CreateString(log->ResponseTime)); } if (log->CallStack != NULL) { cJSON* callstack_root = cJSON_Parse(log->CallStack); cJSON* callstack_content = cJSON_GetObjectItem(callstack_root, "callStack"); if (callstack_content != NULL) { cJSON_DetachItemViaPointer(callstack_root, callstack_content); cJSON_AddItemToObject(united_content, "callStack", callstack_content); } if (callstack_root) cJSON_Delete(callstack_root); } if (log->DbStack != NULL) { cJSON* dbstack_root = cJSON_Parse(log->DbStack); cJSON* dbstack_content = cJSON_GetObjectItem(dbstack_root, "dbStack"); if (dbstack_content != NULL) { cJSON_DetachItemViaPointer(dbstack_root, dbstack_content); cJSON_AddItemToObject(united_content, "dbStack", dbstack_content); } if (dbstack_root) cJSON_Delete(dbstack_root); } if (log->Tags != NULL) { cJSON* tags_root = cJSON_Parse(log->Tags); cJSON* tags_content = cJSON_GetObjectItem(tags_root, "tags"); if (tags_content != NULL) { cJSON_DetachItemViaPointer(tags_root, tags_content); cJSON_AddItemToObject(united_content, "tags", tags_content); } if (tags_root) cJSON_Delete(tags_root); } str_united_content = cJSON_PrintUnformatted(united_content); } totalBufferSize = strlen(str_united_content) + 1 + strlen(t_header) * 2; grp_log = &(bder->grp->logs[bder->grp->n_logs]); if (grp_log->buffer == NULL || grp_log->buffer_len < totalBufferSize) { _adjust_buffer(grp_log, totalBufferSize); } #if defined(_MSC_VER) sprintf_s(grp_log->buffer, totalBufferSize, "%s%s%s", t_header, str_united_content, t_header); #else sprintf(grp_log->buffer, "%s%s%s", t_header, str_united_content, t_header); #endif //_MSC_VER //memcpy(grp_log->buffer, str_united_content, totalBufferSize); bder->loggroup_size += totalBufferSize; grp_log->buffer_len = totalBufferSize; grp_log->type = LOG_TYPE_BEIDOU; grp_log->level = 0; ++bder->grp->n_logs; //delete if(united_content) cJSON_Delete(united_content); if(str_united_content) cJSON_free(str_united_content); return 0; } int add_log(log_group_builder* bder, long start_time, uint32_t pack_index, log_item* log, long record_time) { cJSON* skyeye_content; cJSON* united_content; cJSON* meta_log_content; cJSON* log_content; cJSON* simpleLog_content; char* str_united_content; int32_t totalBufferSize; build_item* grp_log; long now; char nowTime[64] = { 0 }, local_ip_str[64] = { 0 }, unitedNowTime[64] = { 0 }; if (bder->grp->n_logs >= MAX_LOG_COUNT) { return -1; } memset(bder->grp->logs[bder->grp->n_logs].uuid, 0, MAX_UUID_LEN); get_format_uuid(bder->grp->logs[bder->grp->n_logs].uuid, MAX_UUID_LEN); aos_debug_log((LB, "add_log 1")); GetLocalIP(local_ip_str); aos_debug_log((LB, "add_log 2")); now = record_time; aos_debug_log((LB, "add_log 3")); GetTimeStr(now, nowTime); aos_debug_log((LB, "add_log 4")); GetUnitedTimeStr(now, unitedNowTime); aos_debug_log((LB, "add_log 5")); skyeye_content = cJSON_CreateObject(); meta_log_content = cJSON_CreateObject(); log_content = cJSON_CreateObject(); united_content = cJSON_CreateObject(); simpleLog_content = cJSON_CreateObject(); //天眼日志 { // 应用信息 if(bder->config->skyeyeTerminalNo != NULL) cJSON_AddItemToObject(skyeye_content, "ServName", cJSON_CreateString(bder->config->skyeyeTerminalNo)); cJSON_AddItemToObject(skyeye_content, "CmptId", cJSON_CreateString(bder->config->skyeyeCmptId)); cJSON_AddItemToObject(skyeye_content, "CmptName", cJSON_CreateString(bder->config->skyeyeCmptName)); //日志数据信息 if (log->Type == LOG_TYPE_USER || log->Type == LOG_TYPE_BUSINESS_USER) { if (log->Level == LOG_LEVEL_DEBUG) { cJSON_AddItemToObject(skyeye_content, "LogFile", cJSON_CreateString("UD")); cJSON_AddItemToObject(meta_log_content, "baseLogType", cJSON_CreateString("DEBUG")); cJSON_AddItemToObject(log_content, "type", cJSON_CreateString("BUSINESS_DEBUG")); } else if (log->Level == LOG_LEVEL_INFO) { cJSON_AddItemToObject(skyeye_content, "LogFile", cJSON_CreateString("UI")); cJSON_AddItemToObject(meta_log_content, "baseLogType", cJSON_CreateString("INFO")); cJSON_AddItemToObject(log_content, "type", cJSON_CreateString("BUSINESS_INFO")); } else if (log->Level == LOG_LEVEL_WARN) { cJSON_AddItemToObject(skyeye_content, "LogFile", cJSON_CreateString("UW")); cJSON_AddItemToObject(meta_log_content, "baseLogType", cJSON_CreateString("WARN")); cJSON_AddItemToObject(log_content, "type", cJSON_CreateString("BUSINESS_WARN")); } else if (log->Level == LOG_LEVEL_ERROR) { cJSON_AddItemToObject(skyeye_content, "LogFile", cJSON_CreateString("UE")); cJSON_AddItemToObject(meta_log_content, "baseLogType", cJSON_CreateString("ERROR")); cJSON_AddItemToObject(log_content, "type", cJSON_CreateString("BUSINESS_ERROR")); } else if (log->Level == LOG_LEVEL_FATAL) { cJSON_AddItemToObject(skyeye_content, "LogFile", cJSON_CreateString("UF")); cJSON_AddItemToObject(meta_log_content, "baseLogType", cJSON_CreateString("PERFORMANCE")); cJSON_AddItemToObject(log_content, "type", cJSON_CreateString("BUSINESS_PERFORMANCE")); } cJSON_AddItemToObject(skyeye_content, "BeginTime", cJSON_CreateNumber(log->BeginTime)); cJSON_AddItemToObject(skyeye_content, "EndTime", cJSON_CreateNumber(log->EndTime)); cJSON_AddItemToObject(skyeye_content, "ClientIP", cJSON_CreateString(local_ip_str)); } else{ if (log->Level == LOG_LEVEL_DEBUG) { cJSON_AddItemToObject(skyeye_content, "LogFile", cJSON_CreateString("SD")); cJSON_AddItemToObject(meta_log_content, "baseLogType", cJSON_CreateString("DEBUG")); cJSON_AddItemToObject(log_content, "type", cJSON_CreateString("SYSTEM_DEBUG")); } else if (log->Level == LOG_LEVEL_INFO) { cJSON_AddItemToObject(skyeye_content, "LogFile", cJSON_CreateString("SI")); cJSON_AddItemToObject(meta_log_content, "baseLogType", cJSON_CreateString("INFO")); cJSON_AddItemToObject(log_content, "type", cJSON_CreateString("SYSTEM_INFO")); } else if (log->Level == LOG_LEVEL_WARN) { cJSON_AddItemToObject(skyeye_content, "LogFile", cJSON_CreateString("SW")); cJSON_AddItemToObject(meta_log_content, "baseLogType", cJSON_CreateString("WARN")); cJSON_AddItemToObject(log_content, "type", cJSON_CreateString("SYSTEM_WARN")); } else if (log->Level == LOG_LEVEL_ERROR) { cJSON_AddItemToObject(skyeye_content, "LogFile", cJSON_CreateString("SE")); cJSON_AddItemToObject(meta_log_content, "baseLogType", cJSON_CreateString("ERROR")); cJSON_AddItemToObject(log_content, "type", cJSON_CreateString("SYSTEM_ERROR")); } else if (log->Level == LOG_LEVEL_FATAL) { cJSON_AddItemToObject(skyeye_content, "LogFile", cJSON_CreateString("SF")); cJSON_AddItemToObject(meta_log_content, "baseLogType", cJSON_CreateString("PERFORMANCE")); cJSON_AddItemToObject(log_content, "type", cJSON_CreateString("SYSTEM_PERFORMANCE")); } cJSON_AddItemToObject(skyeye_content, "EvenTime", cJSON_CreateString(nowTime)); if (log->BeginTime || log->EndTime) { cJSON_AddItemToObject(skyeye_content, "BeginTime", cJSON_CreateNumber(log->BeginTime)); cJSON_AddItemToObject(skyeye_content, "EndTime", cJSON_CreateNumber(log->EndTime)); } if (strlen(local_ip_str)) cJSON_AddItemToObject(skyeye_content, "ClientIP", cJSON_CreateString(local_ip_str)); } cJSON_AddItemToObject(skyeye_content, "CostTime", cJSON_CreateNumber(log->CostTime)); cJSON_AddItemToObject(skyeye_content, "Version", cJSON_CreateString(bder->config->skyeyeVersion)); cJSON_AddItemToObject(skyeye_content, "DateTime", cJSON_CreateString(nowTime)); cJSON_AddItemToObject(skyeye_content, "ResultCode", cJSON_CreateString(log->ResultCode)); //迁移到公共位置,都需要这个数? if (log->LogCode != NULL) { cJSON_AddItemToObject(skyeye_content, "LogCode", cJSON_CreateString(log->LogCode)); } if (log->API != NULL) { cJSON_AddItemToObject(skyeye_content, "API", cJSON_CreateString(log->API)); } if (log->TipMsg != NULL) { cJSON_AddItemToObject(skyeye_content, "TipMsg", cJSON_CreateString(log->TipMsg)); } if (log->BussID != NULL) { cJSON_AddItemToObject(skyeye_content, "BussID", cJSON_CreateString(log->BussID)); } if (log->SourceType != NULL) { cJSON_AddItemToObject(skyeye_content, "SourceType", cJSON_CreateString(log->SourceType)); } if (strlen(log->VtmCode) > 0) { cJSON_AddItemToObject(skyeye_content, "VtmCode", cJSON_CreateString(log->VtmCode)); } if (log->ResultMsg != NULL) { cJSON_AddItemToObject(skyeye_content, "ResultMsg", cJSON_CreateString(log->ResultMsg)); } //追踪信息 cJSON_AddItemToObject(skyeye_content, "TraceID", cJSON_CreateString(log->TraceID)); if (bder->config->skyeyeTerminalNo != NULL) { cJSON_AddItemToObject(skyeye_content, "TerminalNo", cJSON_CreateString(bder->config->skyeyeTerminalNo)); } if (bder->config->skyeyeSn != NULL) { cJSON_AddItemToObject(skyeye_content, "SN", cJSON_CreateString(bder->config->skyeyeSn)); } cJSON_AddItemToObject(skyeye_content, "Item", cJSON_CreateString(bder->config->skyeyeItem)); cJSON_AddItemToObject(skyeye_content, "EntityName", cJSON_CreateString(bder->config->skyeyeEntityName)); if(now - start_time < 0) cJSON_AddItemToObject(skyeye_content, "LifeID", cJSON_CreateNumber(0)); else if(now - start_time > 3600 * 1000) cJSON_AddItemToObject(skyeye_content, "LifeID", cJSON_CreateNumber(3600 * 1000)); else cJSON_AddItemToObject(skyeye_content, "LifeID", cJSON_CreateNumber(now - start_time)); if(pack_index > 1000000) cJSON_AddItemToObject(skyeye_content, "TimeSn", cJSON_CreateNumber(1000000)); else cJSON_AddItemToObject(skyeye_content, "TimeSn", cJSON_CreateNumber(pack_index)); } aos_debug_log((LB, "add_log 6")); { cJSON* t_baseLogType = NULL; // ӦϢ //cJSON_AddItemToObject(united_content, "appName", cJSON_CreateString(bder->config->unitedAppName)); cJSON_AddItemToObject(united_content, "_CMB_LOG_SPEC_VERSION", cJSON_CreateString(bder->config->unitedVersion)); t_baseLogType = cJSON_GetObjectItem(meta_log_content, "baseLogType"); if (t_baseLogType) { cJSON_DetachItemViaPointer(meta_log_content, t_baseLogType); cJSON_AddItemToObject(united_content, "level", t_baseLogType); } cJSON_AddItemToObject(united_content, "ts", cJSON_CreateString(unitedNowTime)); //cJSON_AddItemToObject(meta_log_content, "uuid", cJSON_CreateString(bder->grp->logs[bder->grp->n_logs].uuid)); //cJSON_AddItemToObject(meta_log_content, "needArchived", cJSON_CreateBool(bder->config->unitedNeedArchived)); //cJSON_AddItemToObject(log_content, "ts", cJSON_CreateString(unitedNowTime)); //cJSON_AddItemToObject(log_content, "deployUnitId", cJSON_CreateString(bder->config->unitedDeployUnitId)); //cJSON_AddItemToObject(log_content, "serviceUnitId", cJSON_CreateString(bder->config->unitedServiceUnitId)); cJSON_AddItemToObject(simpleLog_content, "content", skyeye_content); cJSON_AddItemToObject(united_content, "log", simpleLog_content); //cJSON_AddItemToObject(united_content, "content", skyeye_content); cJSON_AddItemToObject(united_content, "type", cJSON_CreateString("BASETYPE")); cJSON_AddItemToObject(united_content, "serviceUnitId", cJSON_CreateString(bder->config->unitedServiceUnitId)); str_united_content = cJSON_PrintUnformatted(united_content); } aos_debug_log((LB, "add_log 7")); totalBufferSize = strlen(str_united_content) + 1; grp_log = &(bder->grp->logs[bder->grp->n_logs]); //发送内容存储在这里 if (grp_log->buffer == NULL || grp_log->buffer_len < totalBufferSize) { _adjust_buffer(grp_log, totalBufferSize); } memcpy(grp_log->buffer, str_united_content, totalBufferSize); bder->loggroup_size += totalBufferSize; grp_log->buffer_len = totalBufferSize; if (log->Type == LOG_TYPE_USER) { grp_log->type = LOG_TYPE_USER_SKYEYE; } else if (log->Type == LOG_TYPE_BUSINESS_SYSTEM) { grp_log->type = LOG_TYPE_SYS_BUSINESS; } else if (log->Type == LOG_TYPE_BUSINESS_USER) { grp_log->type = LOG_TYPE_USER_BUSINESS; } else { grp_log->type = LOG_TYPE_SYS_SKYEYE; } grp_log->level = log->Level; ++bder->grp->n_logs; if (united_content) cJSON_Delete(united_content); if (log_content) cJSON_Delete(log_content); if (meta_log_content) cJSON_Delete(meta_log_content); if(str_united_content) cJSON_free(str_united_content); aos_debug_log((LB, "add_log 8")); return 0; } log_group_builder* log_group_create(log_producer_config* config) { int i = 0; log_group_builder* bder = (log_group_builder*)malloc(sizeof(log_group_builder) + sizeof(log_group)); memset(bder, 0, sizeof(log_group_builder) + sizeof(log_group)); bder->grp = (log_group*)((char*)(bder)+sizeof(log_group_builder)); bder->loggroup_size = 0; bder->builder_time = time(NULL); for (i = 0; i < MAX_LOG_COUNT; i++) { memset(bder->grp->logs[i].uuid, 0, MAX_UUID_LEN); } bder->config = config; return bder; } void log_group_destroy(log_group_builder* bder) { int i = 0; log_group* group = bder->grp; // free log for (i = 0; i < MAX_LOG_COUNT; i++) { if (group->logs[i].buffer != NULL) { free(group->logs[i].buffer); } } // free self free(bder); } log_group_builder* log_group_clone(log_group_builder* bder) { int i; log_group_builder* new_builder = log_group_create(bder->config); strcpy(new_builder->modular, bder->modular); new_builder->builder_time = bder->builder_time; for (i = 0; i < bder->grp->n_logs; i++) { add_log_raw(new_builder, &(bder->grp->logs[i])); } return NULL; } int add_log_raw(log_group_builder* bder, build_item* log) { build_item* build_data; if (bder->grp->n_logs >= MAX_LOG_COUNT) { return -1; } build_data = &(bder->grp->logs[bder->grp->n_logs]); if (build_data->buffer == NULL || build_data->buffer_len < log->buffer_len) { _adjust_buffer(build_data, log->buffer_len); } memcpy(build_data->buffer, log->buffer, log->buffer_len); build_data->buffer_len = log->buffer_len; memcpy(build_data->uuid, log->uuid, MAX_UUID_LEN); build_data->type = log->type; build_data->level = log->level; bder->loggroup_size += log->buffer_len; ++bder->grp->n_logs; return 0; } int add_log_serialize_buf(log_group_builder* bder, serialize_buf* buffer) { build_item* build_data; if (bder->grp->n_logs >= MAX_LOG_COUNT) { return -1; } build_data = &(bder->grp->logs[bder->grp->n_logs]); if (build_data->buffer == NULL || build_data->buffer_len < buffer->buffer_len) { _adjust_buffer(build_data, buffer->buffer_len); } memcpy(build_data->buffer, buffer->buffer, buffer->buffer_len); build_data->buffer_len = buffer->buffer_len; memcpy(build_data->uuid, buffer->uuid, MAX_UUID_LEN); build_data->type = buffer->type; build_data->level = buffer->level; bder->loggroup_size += buffer->buffer_len; ++bder->grp->n_logs; return 0; } build_item* log_group_get_last(log_group_builder* bder) { if (bder->grp->n_logs > 0) { return &(bder->grp->logs[bder->grp->n_logs - 1]); } else { return NULL; } } void log_group_clear_last(log_group_builder* bder) { if (bder->grp->n_logs > 0) { if (bder->grp->logs[bder->grp->n_logs - 1].buffer != NULL) { free(bder->grp->logs[bder->grp->n_logs - 1].buffer); } bder->grp->logs[bder->grp->n_logs - 1].buffer = NULL; bder->grp->logs[bder->grp->n_logs - 1].buffer_len = 0; memset(bder->grp->logs[bder->grp->n_logs - 1].uuid, 0, MAX_UUID_LEN); bder->grp->n_logs--; } } static uint32_t _log_pack(log_group_builder* bder, log_buf* buf) { int i = 0; int vaild_logs = 0; cJSON* pack_content; cJSON* messages_content; int32_t totalBufferSize; char* str_pack_content; char* message_buffer[MAX_LOG_COUNT] = {0}; pack_content = cJSON_CreateObject(); message_buffer[0] = bder->grp->logs[0].buffer; if (bder->grp->logs[0].type == LOG_TYPE_USER_SKYEYE) cJSON_AddItemToObject(pack_content, "topic", cJSON_CreateString(bder->config->skyeye_user_topic)); else if (bder->grp->logs[0].type == LOG_TYPE_SYS_SKYEYE) cJSON_AddItemToObject(pack_content, "topic", cJSON_CreateString(bder->config->skyeye_sys_topic)); else if (bder->grp->logs[0].type == LOG_TYPE_BEIDOU) cJSON_AddItemToObject(pack_content, "topic", cJSON_CreateString(bder->config->beidou_topic)); else if (bder->grp->logs[0].type == LOG_TYPE_USER_BUSINESS) cJSON_AddItemToObject(pack_content, "topic", cJSON_CreateString(bder->config->business_user_topic)); else if (bder->grp->logs[0].type == LOG_TYPE_SYS_BUSINESS) cJSON_AddItemToObject(pack_content, "topic", cJSON_CreateString(bder->config->business_sys_topic)); for (i = 0; i < bder->grp->n_logs; i++) { if (bder->grp->logs[i].buffer != NULL && (bder->grp->logs[0].type == LOG_TYPE_BEIDOU || bder->grp->logs[i].check)) { message_buffer[vaild_logs] = bder->grp->logs[i].buffer; memcpy(buf->uuid[vaild_logs], bder->grp->logs[i].uuid, MAX_UUID_LEN); vaild_logs++; } } buf->n_logs = vaild_logs; messages_content = cJSON_CreateStringArray(message_buffer, vaild_logs); cJSON_AddItemToObject(pack_content, "message_list", messages_content); str_pack_content = cJSON_PrintUnformatted(pack_content); totalBufferSize = strlen(str_pack_content) + 1; if (buf->buffer == NULL || buf->n_buffer < totalBufferSize) { _adjust_buffer2(buf, totalBufferSize); } memcpy(buf->buffer, str_pack_content, totalBufferSize); //delete cJSON_Delete(pack_content); cJSON_free(str_pack_content); return totalBufferSize; } void add_log_terminal_sn(log_group_builder* bder, char* sn) { int32_t totalBufferSize; build_item* grp_log; char* str_message_content; int i, changed = 0; cJSON* message_content = NULL; for (i = 0; i < bder->grp->n_logs; i++) { if (bder->grp->logs[i].buffer != NULL) { cJSON* message_content = cJSON_Parse(bder->grp->logs[i].buffer); if (message_content == NULL) continue; if (bder->grp->logs[i].type != LOG_TYPE_BEIDOU) { cJSON* content = NULL; cJSON* log = cJSON_GetObjectItem(message_content, "log"); if (log == NULL) continue; content = cJSON_GetObjectItem(log, "content"); if (content != NULL) { if (!cJSON_HasObjectItem(content, "SN")) { cJSON_AddItemToObject(content, "SN", cJSON_CreateString(sn)); changed = 1; } } } if (changed) { str_message_content = cJSON_PrintUnformatted(message_content); totalBufferSize = strlen(str_message_content) + 1; grp_log = &(bder->grp->logs[i]); if (grp_log->buffer == NULL || grp_log->buffer_len < totalBufferSize) { _adjust_buffer(grp_log, totalBufferSize); } memcpy(grp_log->buffer, str_message_content, totalBufferSize); bder->loggroup_size -= grp_log->buffer_len; bder->loggroup_size += totalBufferSize; grp_log->buffer_len = totalBufferSize; cJSON_free(str_message_content); } //delete cJSON_Delete(message_content); } else { break; } } } void clear_errJson_msg(log_group_builder* bder) { int i= 0; cJSON* message_content = NULL; for (i = 0; i < bder->grp->n_logs; i++) { if (bder->grp->logs[i].buffer != NULL) { cJSON* message_content = cJSON_Parse(bder->grp->logs[i].buffer); if (message_content == NULL) bder->grp->logs[i].check = 0; else bder->grp->logs[i].check = 1; cJSON_Delete(message_content); } } } void add_log_terminal_no(log_group_builder* bder, char* no) { int32_t totalBufferSize; build_item* grp_log; char* str_message_content; int i, changed = 0; cJSON* message_content = NULL; for (i = 0; i < bder->grp->n_logs; i++) { if (bder->grp->logs[i].buffer != NULL) { cJSON* message_content = cJSON_Parse(bder->grp->logs[i].buffer); if (message_content == NULL) continue;//不完整json if (bder->grp->logs[i].type != LOG_TYPE_BEIDOU) { cJSON* content = NULL; cJSON* log = cJSON_GetObjectItem(message_content, "log"); if (log == NULL) continue; content = cJSON_GetObjectItem(log, "content"); if (content != NULL) { if (!cJSON_HasObjectItem(content, "ServName")) { cJSON_AddItemToObject(content, "ServName", cJSON_CreateString(no)); } if (!cJSON_HasObjectItem(content, "TerminalNo")) { cJSON_AddItemToObject(content, "TerminalNo", cJSON_CreateString(no)); changed = 1; } } } else { cJSON* tags = cJSON_GetObjectItem(message_content, "tags"); if (tags != NULL && !cJSON_HasObjectItem(tags, "TerminalNo")) { // 在tags节点中添加新? cJSON_AddItemToObject(tags, "TerminalNo", cJSON_CreateString(no)); changed = 1; } } if (changed) { str_message_content = cJSON_PrintUnformatted(message_content); totalBufferSize = strlen(str_message_content) + 1; grp_log = &(bder->grp->logs[i]); if (grp_log->buffer == NULL || grp_log->buffer_len < totalBufferSize) { _adjust_buffer(grp_log, totalBufferSize); } memcpy(grp_log->buffer, str_message_content, totalBufferSize); bder->loggroup_size -= grp_log->buffer_len; bder->loggroup_size += totalBufferSize; grp_log->buffer_len = totalBufferSize; cJSON_free(str_message_content); } //delete cJSON_Delete(message_content); } else { break; } } } log_buf serialize_to_proto_buf_with_malloc(log_group_builder* bder) { int i = 0; log_buf buf; buf.buffer = NULL; buf.n_buffer = 0; if (bder->grp->n_logs == 0 || bder->grp->logs[0].buffer == NULL) { return buf; } buf.n_buffer = _log_pack(bder, &buf);//已通过该函数清理了不合规范的模? buf.n_logs = bder->grp->n_logs; buf.type = bder->grp->logs[0].type; for (i = 0; i < buf.n_logs; i++) { memcpy(buf.uuid[i], bder->grp->logs[i].uuid, MAX_UUID_LEN); } strcpy(buf.modular, bder->modular); return buf; } lz4_log_buf* serialize_to_proto_buf_with_malloc_no_lz4(log_group_builder* bder) { int i = 0; log_buf buf = serialize_to_proto_buf_with_malloc(bder); lz4_log_buf* pLogbuf = (lz4_log_buf*)malloc(sizeof(lz4_log_buf) + buf.n_buffer); pLogbuf->length = buf.n_buffer; pLogbuf->raw_length = buf.n_buffer; memcpy(pLogbuf->data, buf.buffer, buf.n_buffer); pLogbuf->n_logs = buf.n_logs; pLogbuf->type = buf.type; for (i = 0; i < pLogbuf->n_logs; i++) { memcpy(pLogbuf->uuid[i], buf.uuid[i], MAX_UUID_LEN); } strcpy(pLogbuf->modular, bder->modular); free(buf.buffer); return pLogbuf; } lz4_log_buf* serialize_to_proto_buf_with_malloc_lz4(log_group_builder* bder) { int i = 0; lz4_log_buf* pLogbuf; log_buf buf = serialize_to_proto_buf_with_malloc(bder); int compress_bound = LZ4_compressBound(buf.n_buffer); char* compress_data = (char*)malloc(compress_bound); int compressed_size = LZ4_compress_default((char*)buf.buffer, compress_data, buf.n_buffer, compress_bound); if (compressed_size <= 0) { free(compress_data); return NULL; } pLogbuf = (lz4_log_buf*)malloc(sizeof(lz4_log_buf) + compressed_size); pLogbuf->length = compressed_size; pLogbuf->raw_length = buf.n_buffer; memcpy(pLogbuf->data, compress_data, compressed_size); pLogbuf->n_logs = buf.n_logs; pLogbuf->type = buf.type; for (i = 0; i < pLogbuf->n_logs; i++) { memcpy(pLogbuf->uuid[i], buf.uuid[i], MAX_UUID_LEN); } strcpy(pLogbuf->modular, bder->modular); free(compress_data); free(buf.buffer); return pLogbuf; } void free_lz4_log_buf(lz4_log_buf* pBuf) { // free self free(pBuf); } void serialize_to_buf(build_item* log, serialize_buf* buf) { buf->type = log->type; buf->level = log->level; memcpy(buf->uuid, log->uuid, MAX_UUID_LEN); buf->buffer_len = log->buffer_len; memcpy(buf->buffer, log->buffer, log->buffer_len > sizeof(buf->buffer) - 1 ? sizeof(buf->buffer) - 1 : log->buffer_len); }