1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018 |
- #include "precompile.h"
- #include "SpBase.h"
- #include "SpUtility.h"
- #include "sp_checkEntity.h"
- #include "log_api.h"
- #include "log_producer_config.h"
- #include "log_producer_client.h"
- #include <cstdarg>
- #include <map>
- #include "sp_def.h"
- #include "sp_dir.h"
- #include "sp_logwithlinkforc.h"
- #include "sp_logwithlink.h"
- #include "sp_dbg_export.h"
- #include "fileutil.h"
- #include "iniutil.h"
- #include "charset.h"
- #include <vector>
- #include <algorithm>
- #include "sp_cfg.h"
- #include "sp_env.h"
- #include <locale>
- #include <winpr/file.h>
- #include <winpr/wlog.h>
- #include "sp_httpDefine.h"
- #include <fstream>
- #include <iostream>
- #include "SpDefine.h"
- #include <mutex>
- #define TAG SPBASE_TAG("sp_logwithlink")
- void* g_logProducer = NULL;//为spshell、spbase共用,所以不放入getEntityResource中
- //#define MAX_LOG_LEN 9700 //大致为10 * 1024 - 400(固定头)
- #define MAX_LOG_LEN 1900
- #define MAX_ENTITY_SAVE_LEN 100
- int g_curEntityIdx = 0;
- std::map<std::string, int> g_entityDebugLevelArr;
- void* g_logProduceSender = NULL;
- int curEntityLogLevel = 1;
- int g_spbaseLogNor = 1;
- std::mutex g_logMtx;
- void Log_GetToken(char* channelId, char* token, char *terminalno, char *reserve1) {
- sp_env_t* env = sp_get_env();
- if (NULL != env && NULL != env->cfg && NULL != env->cfg->shell_ini && env->cfg->shell_ini->channelId != NULL && env->cfg->shell_ini->token != NULL
- && NULL != env->cfg->root_ini && NULL != env->cfg->root_ini->terminal_no)
- {
- snprintf(channelId, 256, "%s", env->cfg->shell_ini->channelId);
- snprintf(token, 256, "%s", env->cfg->shell_ini->token);
- snprintf(terminalno, 256, "%s", env->cfg->root_ini->terminal_no);
- }
- }
- /*天眼日志实例
- {
- "appName": "LR04",
- "version": "1.0",
- "meta_log": {
- "baseLogType": "INFO",
- "uuid": "ba814f0b-31e3-42ed-bf45-a36e9eb5c1f0",
- "needArchived": false
- },
- "log": {
- "type": "SYSTEM_INFO",
- "ts": "2023-04-03T12:57:49.000000000Z",
- "deployUnitId": "LR04.02@FrameworkLib_PRD_PRD",
- "serviceUnitId": "LR04.02_FrameworkLib",
- "content": {
- "ServName": "Terminal",
- "CmptId": "LR04",
- "CmptName": "FrameworkLib",
- "LogFile": "SI",
- "EvenTime": "2023-04-03 12:57:49.000",
- "ClientIP": "99.6.149.222",
- "Version": "1.0",
- "DateTime": "2023-04-03 12:57:49.000",
- "ResultCode": "SUC0000",
- "LogCode": "",
- "API": "",
- "TipMsg": "",
- "BussID": "",
- "SourceType": "",
- "ResultMsg": "entity UpgradeManager,cpu ratio:2.031250",
- "TraceID": "",
- "Item": "52",
- "EntityName": "mod_selfchecker",
- "LifeID": 331,
- "TimeSn": 178,
- "TerminalNo": "5710010002",
- "SN": "5710010002"
- }
- }
- }
- */
- //#define GBK_COMPACT
- class DbgWithLink::Link_private {
- public:
- LOG_LEVEL_E Level;
- LOG_TYPE_E Type;
- CSimpleStringA ResultCode;
- CSimpleStringA TraceID;
- CSimpleStringA ResultMsg;
- CSimpleStringA LogCode;
- CSimpleStringA API;
- long BeginTime;
- long EndTime;
- long CostTime;
- CSimpleStringA BussID;
- CSimpleStringA TipMsg;
- CSimpleStringA SourceType;
- CSimpleString VtmCode;
- void* logProducer;
- bool m_doLog;
- Link_private(LOG_LEVEL_E t_level, LOG_TYPE_E t_type)
- : Level(t_level), Type(t_type), BeginTime(0), EndTime(0), CostTime(0), ResultCode("SUC0000"), m_doLog(true)
- {
- TraceID = ResultMsg = LogCode = API = BussID = TipMsg = SourceType = VtmCode = "";
- logProducer = NULL;
- }
- bool IsAllowToRecord()
- {
- return true;
- }
- };
- /*北斗链路实例
- {
- "businessId": "LR0402ChromiumAutoGen",
- "traceId": "d2a04ca0a4834623a1323372b88e4e0a",
- "parentSpanId": "0",
- "spanId": "db5e25dffa78475c",
- "timestamp": "1680504454",
- "deployUnitId": "LR04.02_FrameworkLib",
- "serviceUnitId": "LR04.02@FrameworkLib_PRD_PRD",
- "host": "vtmtlog.paas.cmbchina.cn:8080",
- "api": "Exit",
- "returnCode": "SUC0000",
- "responseTime": "0",
- "callStack": [],
- "dbStack": [],
- "tags": {}
- }
- */
- class DbgToBeidou::Beidou_private {
- public:
- CSimpleStringA BussID;
- CSimpleStringA TraceID;//链路追踪标记,用于标记一笔业务
- CSimpleStringA ParentSpanId;//父节点标记
- CSimpleStringA SpanId;//当前节点标记
- CSimpleStringA Host;
- CSimpleStringA Api;
- CSimpleStringA ReturnCode;
- CSimpleStringA ResponseTime;
- CSimpleStringA CallStack;
- CSimpleStringA DbStack;
- CSimpleStringA Tags;
- CSimpleStringA deployUnitId;
- CSimpleStringA serviceUnitId;
- void* logProducer;
- Beidou_private(const linkContext& t_context, CSimpleStringA t_Api)
- :logProducer(NULL)
- {
- BussID = t_context.bussinessId;
- TraceID = t_context.traceId;
- ParentSpanId = t_context.parentSpanId;
- SpanId = t_context.spanId;
- Api = t_Api;
- ReturnCode = "SUC0000";
- ResponseTime = "0";
- Host = "vtmtlog.paas.cmbchina.cn:8080";
- CallStack = "{\"callStack\":[]}";
- DbStack = "{\"dbStack\":[]}";
- Tags = "{\"tags\":{}}";
- deployUnitId = "";
- serviceUnitId = "";
- }
- };
- linkContext EntityResource::m_link;
- int EntityResource::m_saveFile(0);
- void EntityResource::setLink(const linkContext& cur)
- {
- m_link = cur;
- }
- const linkContext& EntityResource::getLink()
- {
- return m_link;
- }
- void EntityResource::clearLink()
- {
- m_link.clear();
- }
- DbgToBeidou::DbgToBeidou(const linkContext& t_context, CSimpleStringA t_Api)
- :m_priPtr(new Beidou_private(t_context, t_Api))
- {
- strcpy(CharZero, "0");
- }
- DbgToBeidou::~DbgToBeidou() {
- if (m_priPtr)
- delete m_priPtr;
- }
- DbgToBeidou& DbgToBeidou::setHost(CSimpleStringA t_host) {
- m_priPtr->Host = t_host;
- return *this;
- }
- DbgToBeidou& DbgToBeidou::setReturnCode(CSimpleStringA t_ReturnCode) {
- m_priPtr->ReturnCode = t_ReturnCode;
- return *this;
- }
- DbgToBeidou& DbgToBeidou::setResponseTime(CSimpleStringA t_ResponseTime) {
- m_priPtr->ResponseTime = t_ResponseTime;
- return *this;
- }
- DbgToBeidou& DbgToBeidou::setCallStack(CSimpleStringA t_CallStack) {
- m_priPtr->CallStack = t_CallStack;
- return *this;
- }
- DbgToBeidou& DbgToBeidou::setDbStack(CSimpleStringA t_DbStack) {
- m_priPtr->DbStack = t_DbStack;
- return *this;
- }
- DbgToBeidou& DbgToBeidou::setTags(CSimpleStringA t_Tags) {
- m_priPtr->Tags = t_Tags;
- return *this;
- }
- DbgToBeidou& DbgToBeidou::set_deployUnitId(CSimpleString t_deployUnitId) {
- m_priPtr->deployUnitId = t_deployUnitId;
- return *this;
- }
- DbgToBeidou& DbgToBeidou::set_serviceUnitId(CSimpleString t_serviceUnitId) {
- m_priPtr->serviceUnitId = t_serviceUnitId;
- return *this;
- }
- void DbgToBeidou::operator () () const {
- beidou_log_item beidou_log = { 0 };
- beidou_log.BussID = (char*)m_priPtr->BussID.GetData();
- beidou_log.TraceID = (char*)m_priPtr->TraceID.GetData();
- beidou_log.ParentSpanId = (char*)m_priPtr->ParentSpanId.GetData();
- beidou_log.SpanId = (char*)m_priPtr->SpanId.GetData();
- beidou_log.Host = (char*)m_priPtr->Host.GetData();
- beidou_log.Api = (char*)m_priPtr->Api.GetData();
- beidou_log.ReturnCode = (char*)m_priPtr->ReturnCode.GetData();
- beidou_log.ResponseTime = m_priPtr->ResponseTime.GetLength() == 0 ? (char*)CharZero : (char*)m_priPtr->ResponseTime.GetData();
- beidou_log.CallStack = (char*)m_priPtr->CallStack.GetData();
- beidou_log.DbStack = (char*)m_priPtr->DbStack.GetData();
- beidou_log.Tags = (char*)m_priPtr->Tags.GetData();
- if (m_priPtr->deployUnitId.GetLength() > 0)
- beidou_log.deployUnitId = (char*)m_priPtr->deployUnitId.GetData();
- else
- beidou_log.deployUnitId = NULL;
- if (m_priPtr->serviceUnitId.GetLength() > 0)
- beidou_log.serviceUnitId = (char*)m_priPtr->serviceUnitId.GetData();
- else
- beidou_log.serviceUnitId = NULL;
- log_producer_client* cur = NULL;
- if (m_priPtr->logProducer)
- cur = (log_producer_client*)m_priPtr->logProducer;
- else {
- if (NULL != g_logProducer)
- cur = (log_producer_client*)g_logProducer;
- else
- return;
- }
- auto rst = log_producer_client_add_beidou_log(cur, &beidou_log, 1);
- /*
- if (!is_log_producer_result_ok(rst))
- sp_dbg_errorNoOut("beidou add log to db err: %d, %s", rst);
- */
- }
- DbgToBeidou& DbgToBeidou::withLogProducer(void* t_logProducer) {
- m_priPtr->logProducer = t_logProducer;
- return *this;
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////
- DbgWithLink::DbgWithLink(LOG_LEVEL_E t_level, LOG_TYPE_E t_type) : m_priPtr(new Link_private(t_level, t_type))
- {
- auto curLink = EntityResource::getLink();
- if (curLink.traceId.GetLength() > 0 && curLink.bussinessId.GetLength() > 0)
- {
- m_priPtr->TraceID = curLink.traceId;
- m_priPtr->BussID = curLink.bussinessId;
- }
- }
- DbgWithLink::~DbgWithLink() {
- if (m_priPtr) delete m_priPtr;
- }
- DbgWithLink& DbgWithLink::setResultCode(CSimpleStringA t_ResultCode) {
- m_priPtr->ResultCode = t_ResultCode;
- return *this;
- }
- DbgWithLink& DbgWithLink::setTraceID(CSimpleStringA t_TraceID) {
- m_priPtr->TraceID = t_TraceID;
- return *this;
- }
- DbgWithLink& DbgWithLink::setResultMsg(CSimpleStringA t_ResultMsg) {
- if (t_ResultMsg.IsNullOrEmpty()) {
- m_priPtr->ResultMsg = "";
- } else {
- if (t_ResultMsg.GetLength() > MAX_LOG_LEN)
- t_ResultMsg = t_ResultMsg.SubString(0, MAX_LOG_LEN);
- #ifdef GBK_COMPACT
- const std::string tmp = SP::Utility::GBK2UTF8(t_ResultMsg.GetData());
- m_priPtr->ResultMsg = tmp.c_str();
- #else
- m_priPtr->ResultMsg = t_ResultMsg;
- #endif // GBK_COMPACT
- }
- return *this;
- }
- DbgWithLink& DbgWithLink::setLogCode(CSimpleStringA t_LogCode) {
- m_priPtr->LogCode = t_LogCode;
- return *this;
- }
- DbgWithLink& DbgWithLink::setAPI(CSimpleStringA t_API) {
- #if defined(RVC_OS_WIN)
- if (!t_API.IsNullOrEmpty() && t_API.IndexOf("::") != -1
- && !t_API.IsStartWith("DevAdapter::") /** treat specially [Gifur@2024730]*/
- ) {
- int tIdx = 0;
- const int nLen = t_API.GetLength();
- for (int i = nLen - 1; i > 0; --i) {
- if (t_API[i] == ':' && t_API[i - 1] == ':') {
- tIdx = i + 1;
- break;
- }
- }
- if (tIdx > 0 && tIdx < nLen) {
- t_API = t_API.SubString(tIdx);
- }
- else if (tIdx == nLen) {
- t_API.Clear();
- t_API = CSimpleStringA(true);
- }
- }
- #endif //RVC_OS_WIN
- m_priPtr->API = t_API;
- return *this;
- }
- DbgWithLink& DbgWithLink::setBussID(CSimpleStringA t_BussID) {
- m_priPtr->BussID = t_BussID;
- return *this;
- }
- DbgWithLink& DbgWithLink::setTipMsg(CSimpleStringA t_TipMsg) {
- m_priPtr->TipMsg = t_TipMsg;
- return *this;
- }
- DbgWithLink& DbgWithLink::setSourceType(CSimpleStringA t_SourceType) {
- m_priPtr->SourceType = t_SourceType;
- return *this;
- }
- DbgWithLink& DbgWithLink::setBeginTime(long t_BeginTime) {
- m_priPtr->BeginTime = t_BeginTime;
- return *this;
- }
- DbgWithLink& DbgWithLink::setEndTime(long t_EndTime) {
- m_priPtr->EndTime = t_EndTime;
- return *this;
- }
- DbgWithLink& DbgWithLink::setCostTime(long t_costTime) {
- m_priPtr->CostTime = t_costTime;
- return *this;
- }
- DbgWithLink& DbgWithLink::setVtmCode(CSimpleStringA t_VtmCode) {
- m_priPtr->VtmCode = t_VtmCode;
- return *this;
- }
- void DbgWithLink::operator () (const char* str, ...) const {
- std::lock_guard<std::mutex> lock(g_logMtx);
- if (str == nullptr)
- return;
- va_list arg;
- va_start(arg, str);
-
- int n = _vscprintf(str, arg) + 1;
- if (n > MAX_LOG_LEN) {
- n = MAX_LOG_LEN;
- }
- std::vector<char> buf(n + 1, '\0');
- vsnprintf(&buf[0], n, str, arg);
- va_end(arg);
- #ifdef GBK_COMPACT
- if (!toolkit_detect_utf8_str((const char*)&buf[0])) {
- char* tmp = &buf[0];
- const size_t len = toolkit_gbk2utf8(tmp, strlen(tmp) + 1, NULL, 0);
- if (len > 0) {
- char* temp = new char[len];
- if (temp != NULL) {
- memset(temp, 0, len);
- if (toolkit_gbk2utf8(tmp, strlen(tmp) + 1, temp, len) > 0) {
- strcpy(tmp, temp);
- }
- delete[] temp;
- }
- }
- }
- #endif //GBK_COMPACT
- if (m_priPtr->IsAllowToRecord()) {
- log_item log;
- log_producer_result rst;
- log.Level = m_priPtr->Level;
- log.Type = m_priPtr->Type;
- log.LogCode = (char*)m_priPtr->LogCode.GetData();
- log.API = (char*)m_priPtr->API.GetData();
- log.ResultCode = (char*)m_priPtr->ResultCode.GetData();
- log.ResultMsg = &buf[0];
- log.TipMsg = (char*)m_priPtr->TipMsg.GetData();
- log.BeginTime = m_priPtr->BeginTime;
- log.EndTime = m_priPtr->EndTime;
- log.CostTime = m_priPtr->CostTime;
- log.VtmCode = (char*)m_priPtr->VtmCode.GetData();
- log.TraceID = (char*)m_priPtr->TraceID.GetData();
- log.BussID = (char*)m_priPtr->BussID.GetData();
- log.SourceType = (char*)m_priPtr->SourceType.GetData();
- log_producer_client* cur = NULL;
- if (m_priPtr->logProducer) {
- cur = (log_producer_client*)m_priPtr->logProducer;
- } else {
- if (NULL != g_logProducer) {
- cur = (log_producer_client*)g_logProducer;
- }
- }
- if (cur != NULL) {
- if (log.Type == LOG_TYPE_SYSTEM && curEntityLogLevel <= log.Level)
- rst = log_producer_client_add_log(cur, &log, 1, time(NULL));
- else if (log.Type != LOG_TYPE_SYSTEM)
- rst = log_producer_client_add_log(cur, &log, 1, time(NULL));
- }
- }
- //LOG_LEVEL_DEBUG, LOG_LEVEL_INFO, LOG_LEVEL_WARN, LOG_LEVEL_ERROR, LOG_LEVEL_FATAL
- if (!m_priPtr->m_doLog) {
- return;
- }
- switch (m_priPtr->Level) {
- case LOG_LEVEL_DEBUG:
- sp_dbg_debugNoOut("%s", &buf[0]);
- break;
- case LOG_LEVEL_INFO:
- sp_dbg_infoNoOut("%s", &buf[0]);
- break;
- case LOG_LEVEL_WARN:
- sp_dbg_warnNoOut("%s", &buf[0]);
- break;
- case LOG_LEVEL_ERROR:
- sp_dbg_errorNoOut("%s", &buf[0]);
- break;
- case LOG_LEVEL_FATAL:
- sp_dbg_fatalNoOut("%s", &buf[0]);
- break;
- default:
- break;
- }
- }
- void DbgWithLink::operator () () const {
- std::lock_guard<std::mutex> lock(g_logMtx);
- log_item log;
- log.Level = m_priPtr->Level;
- log.Type = m_priPtr->Type;
- log.LogCode = (char*)m_priPtr->LogCode.GetData();
- log.API = (char*)m_priPtr->API.GetData();
- log.ResultCode = (char*)m_priPtr->ResultCode.GetData();
- if (m_priPtr->ResultMsg.GetLength() > MAX_LOG_LEN)
- m_priPtr->ResultMsg = m_priPtr->ResultMsg.SubString(0, MAX_LOG_LEN);
- log.ResultMsg = (char*)m_priPtr->ResultMsg.GetData();
- log.TipMsg = (char*)m_priPtr->TipMsg.GetData();
- log.BeginTime = m_priPtr->BeginTime;
- log.EndTime = m_priPtr->EndTime;
- log.CostTime = m_priPtr->CostTime;
- log.VtmCode = (char*)m_priPtr->VtmCode.GetData();
- log.TraceID = (char*)m_priPtr->TraceID.GetData();
- log.BussID = (char*)m_priPtr->BussID.GetData();
- log.SourceType = (char*)m_priPtr->SourceType.GetData();
- log_producer_client* cur = NULL;
- if (m_priPtr->logProducer) {
- cur = (log_producer_client*)m_priPtr->logProducer;
- } else {
- if (NULL != g_logProducer) {
- cur = (log_producer_client*)g_logProducer;
- }
- }
- if (cur != NULL) {
- if (log.Type == LOG_TYPE_SYSTEM && curEntityLogLevel <= log.Level)
- log_producer_client_add_log(cur, &log, 1, time(NULL));
- else if (log.Type != LOG_TYPE_SYSTEM)
- log_producer_client_add_log(cur, &log, 1, time(NULL));
- }
- if (!m_priPtr->m_doLog)
- return;
- switch (log.Level) {
- case LOG_LEVEL_DEBUG:
- sp_dbg_debugNoOut("%s", log.ResultMsg);
- break;
- case LOG_LEVEL_INFO:
- sp_dbg_infoNoOut("%s", log.ResultMsg);
- break;
- case LOG_LEVEL_WARN:
- sp_dbg_warnNoOut("%s", log.ResultMsg);
- break;
- case LOG_LEVEL_ERROR:
- sp_dbg_errorNoOut("%s", log.ResultMsg);
- break;
- case LOG_LEVEL_FATAL:
- sp_dbg_fatalNoOut("%s", log.ResultMsg);
- break;
- default:
- break;
- }
- }
- DbgWithLink& DbgWithLink::withLogProducer(void* t_logProducer)
- {
- m_priPtr->logProducer = t_logProducer;
- return *this;
- }
- DbgWithLink& DbgWithLink::withExtendLog(bool t_doLog) {
- m_priPtr->m_doLog = t_doLog;
- return *this;
- }
- CSimpleString generateDefaultStoragePath()
- {
- char dirPath[_MAX_PATH] = "";
- char tmp[MAX_PATH];
- GetModuleFileNameA(NULL, tmp, MAX_PATH);
- *strrchr(tmp, SPLIT_SLASH) = 0;
- *strrchr(tmp, SPLIT_SLASH) = 0;
- *strrchr(tmp, SPLIT_SLASH) = 0;
- *strrchr(tmp, SPLIT_SLASH) = 0;
- *strrchr(tmp, SPLIT_SLASH) = 0;
- sprintf(dirPath, "%s" SPLIT_SLASH_STR "rvc" SPLIT_SLASH_STR "terminaldbstorage", tmp);
- if (ExistsDirA(dirPath) || CreateDirectoryA(dirPath, NULL)) {
- return dirPath;
- }
- return "";
- }
- static CSimpleStringA g_entityName = "SpShell";
- void logCallback(const char* message) {
- // 在这里执行您的日志记录逻辑
- // 在这个示例中,我们简单地将日志消息输出到控制台
- WLog_DBG(TAG, "from rvcLogSDK:%s", message);
- }
- std::string toLowerCase(std::string str) {
- std::transform(str.begin(), str.end(), str.begin(), [](unsigned char c) {
- return std::tolower(c, std::locale());
- });
- return str;
- }
- SPBASE_API void* create_log_producer_storage(CSimpleStringA entityName, CSimpleStringA item, CSimpleStringA filePath, CSimpleStringA CmptId, CSimpleString CmptName)
- {
- WLog_DBG(TAG, "create log producer storage for %s, %s, %s", entityName.GetData(), item.GetData(), filePath.GetData());
- log_producer_config* config = create_log_producer_config();
- //TODO: CrossPlaform [Gifur@2025730]
- #ifdef _WIN32
- std::string t_entityName = toLowerCase(entityName.GetData());
- #else
- const std::string t_entityName = SP::Utility::ToLower(entityName.GetData());
- #endif
- if (config == NULL) {
- WLog_DBG(TAG, "create_log_producer_config return null!");
- return NULL;
- }
- // set resource params
- log_producer_config_set_packet_log_bytes(config, 4 * 1024 * 1024);
- log_producer_config_set_packet_log_count(config, 24);//最大发送条数
- log_producer_config_set_max_buffer_limit(config, 64 * 1024 * 1024);
- // set send thread count
- log_producer_config_set_send_thread_count(config, 1);
- log_producer_config_set_persistent_max_log_count(config, 20 * 1000);
- log_producer_config_set_skyeye_servname(config, (char*)"Terminal");
- if(CmptId.GetLength() > 0)
- log_producer_config_set_skyeye_cmptId(config, (char*)CmptId.GetData());
- else
- log_producer_config_set_skyeye_cmptId(config, (char*)SpDefine::cmptId);
- if(CmptName.GetLength() > 0)
- log_producer_config_set_skyeye_cmptname(config, (char*)CmptName.GetData());
- else
- log_producer_config_set_skyeye_cmptname(config, (char*)SpDefine::CmptName);
- log_producer_config_set_skyeye_version(config, (char*)"1.0");
- log_producer_config_set_persistent(config, 1);
- sp_env_t* env = sp_get_env();
- if (env && env->cfg && env->cfg->args && env->cfg->args->debug_mode) {
- //log_producer_config_switch_debug_mode(config, 1);
- log_producer_config_set_persistent_encrypt(config, 0);
- }
- log_producer_config_set_skyeye_entityname(config, (char*)t_entityName.c_str());
- g_entityName = t_entityName.c_str();
-
- #ifndef _WIN32
- WLog_DBG(TAG, "entity name: %s", t_entityName.c_str());
- if (t_entityName.compare("spshell") == 0) {
- log_producer_config_memory_deal_type(config, 1);
- }
- #endif
- log_producer_config_set_skyeye_item(config, (char*)item.GetData());
- if (filePath.GetLength() == 0) {
- auto dstPath = generateDefaultStoragePath();
- if (dstPath.GetLength() == 0) {
- return NULL;//create db file failed
- } else {
- log_producer_config_set_persistent_file_path(config, dstPath.GetData());
- }
- } else {
- if (!ExistsDirA(filePath.GetData())) {
- CreateDirRecursiveA(filePath.GetData());
- }
- log_producer_config_set_persistent_file_path(config, filePath.GetData());
- }
- //新加统一日志配置
- log_producer_config_set_united_appname(config, (char*)SpDefine::cmptId);
- log_producer_config_set_united_version(config, (char*)"2.0");
- log_producer_config_set_united_needArchived(config, 0);
- log_producer_config_set_united_deployUnitId(config, (char*)SpDefine::deployUnitId);
- log_producer_config_set_united_serviceUnitId(config, (char*)SpDefine::serviceUnitId);
- #ifndef _WIN32
- //log_producer_config_set_dbgFun(logCallback);
- WLog_DBG(TAG, "to create_log_producer_client...");
- log_producer_client* result = create_log_producer_client(config, NULL, NULL);
- if (result == NULL) {
- WLog_ERR(TAG, "create_log_producer_client return null!");
- } else {
- WLog_DBG(TAG, "to create_log_producer_client done");
- }
- return result;
- #else
- return create_log_producer_client(config, NULL, NULL);
- #endif
- }
- SPBASE_API void destroy_log_producer_storage(void* obj)
- {
- if (obj != NULL) {
- log_producer_client* result = (log_producer_client*)obj;
- destroy_log_producer_client(result);
- }
- }
- SPBASE_API void* create_log_producer_send(CSimpleStringA endpoint, CSimpleStringA topicSys, CSimpleStringA topicUser, CSimpleStringA topicBeidou, CSimpleStringA bussinessSys, CSimpleStringA bussinessUser, CSimpleStringA topicVTMWeb,
- CSimpleStringA terminalNo, CSimpleStringA sn, CSimpleStringA filePath, int sendWaitTime)
- {
- #ifndef _WIN32
- if (NULL != g_logProduceSender) {
- return g_logProduceSender;
- }
- #endif
- log_producer_config* config = create_log_producer_config();
- log_producer_config_set_endpoint(config, endpoint.GetData());
- log_producer_config_set_send_thread_wait_ms(config, sendWaitTime);
- // add topic, comment it
- log_producer_config_set_beidou_topic(config, topicBeidou.GetData());
- log_producer_config_set_skyeye_user_topic(config, topicUser.GetData());
- log_producer_config_set_skyeye_sys_topic(config, topicSys.GetData());
- log_producer_config_set_business_sys_topic(config, bussinessSys.GetData());
- log_producer_config_set_business_user_topic(config, bussinessUser.GetData());
- log_producer_config_set_vtmweb_topic(config, topicVTMWeb.GetData());
- // set resource params
- log_producer_config_set_packet_log_bytes(config, 4 * 1024 * 1024);
- log_producer_config_set_packet_log_count(config, 24);
- log_producer_config_set_max_buffer_limit(config, 64 * 1024 * 1024);
- // set send thread count
- log_producer_config_set_send_thread_count(config, 1);
- log_producer_config_set_persistent_max_log_count(config, 20 * 1000);
- log_producer_config_set_skyeye_servname(config, (char*)"Terminal");
- log_producer_config_set_skyeye_cmptId(config, (char*)SpDefine::cmptId);
- log_producer_config_set_skyeye_cmptname(config, (char*)SpDefine::CmptName);
- log_producer_config_set_skyeye_version(config, (char*)"1.0");
- log_producer_config_set_persistent(config, 1);
- log_producer_config_set_openDb(config, 1);
- log_producer_config_set_tokenCallBack(config, Log_GetToken);
- sp_env_t* env = sp_get_env();
- if (env && env->cfg && env->cfg->args && env->cfg->args->debug_mode) {
- //log_producer_config_switch_debug_mode(config, 1);
- log_producer_config_set_persistent_encrypt(config, 0);
- }
- log_producer_config_set_skyeye_entityname(config, (char*)g_entityName.GetData());
- log_producer_config_set_skyeye_item(config, (char*)"upload");
- log_producer_config_set_skyeye_terminalNo(config, (char*)terminalNo.GetData());
- log_producer_config_set_skyeye_sn(config, (char*)sn.GetData());
- if (filePath.GetLength() == 0) {
- auto dstPath = generateDefaultStoragePath();
- if (dstPath.GetLength() == 0) {
- return NULL;//create db file failed
- } else {
- log_producer_config_set_persistent_file_path(config, dstPath.GetData());
- }
- } else {
- if (!ExistsDirA(filePath.GetData())) {
- CreateDirRecursiveA(filePath.GetData());
- }
- log_producer_config_set_persistent_file_path(config, filePath.GetData());
- }
- //新加统一日志配置
- log_producer_config_set_united_appname(config, (char*)SpDefine::cmptId);
- log_producer_config_set_united_version(config, (char*)"2.0");
- log_producer_config_set_united_needArchived(config, 0);
- log_producer_config_set_united_deployUnitId(config, (char*)SpDefine::deployUnitId);
- log_producer_config_set_united_serviceUnitId(config, (char*)SpDefine::serviceUnitId);
- log_producer_config_set_enable_guard(config, 1);
- #ifndef _WIN32
- g_logProduceSender = create_log_producer_client(config, NULL, NULL);
- return g_logProduceSender;
- #else
- return create_log_producer_client(config, NULL, NULL);
- #endif
- }
- SPBASE_API void destroy_log_producer_send()
- {
- if (g_logProduceSender != NULL) {
- log_producer_client* result = (log_producer_client*)g_logProduceSender;
- destroy_log_producer_client(result);
- g_logProduceSender = NULL;
- }
- }
- SPBASE_API bool refreshLogLevel()
- {
- sp_env_t* env = sp_get_env();
- if (env) {
- sp_cfg_t* cfg = env->cfg;
- sp_cfg_shell_entity_t* ent = sp_cfg_get_entity_by_idx(cfg, g_curEntityIdx);
- if (ent) {
- curEntityLogLevel = ent->log_record_level;
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("refreshLogLevel change to EntityLevel:%d", curEntityLogLevel);
- }
- }
- else
- {
- if (g_entityDebugLevelArr.find("Common") != g_entityDebugLevelArr.end())
- {
- curEntityLogLevel = g_entityDebugLevelArr["Common"];
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("refreshLogLevel change to Common:%d", curEntityLogLevel);
- }
- else
- {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("refreshLogLevel change to Default:%d", curEntityLogLevel);
- curEntityLogLevel = 1;
- }
- }
- return true;
- }
- SPBASE_API int getCurLogLevel()
- {
- return curEntityLogLevel;
- }
- SPBASE_API void setReduceSpbaseLog(int isNor)
- {
- g_spbaseLogNor = isNor;
- }
- SPBASE_API void setTestLogMode()
- {
- log_producer_set_testLogMode();
- }
- SPBASE_API int SendTestLog_loki(const char* app, const char* env, const char* body)
- {
- return log_producer_sendTestLog_loki(app, env, body);
- }
- int changeDbtoNew()
- {
- char tmp[MAX_PATH];
- GetModuleFileNameA(NULL, tmp, MAX_PATH);
- *strrchr(tmp, SPLIT_SLASH) = 0;
- *strrchr(tmp, SPLIT_SLASH) = 0;
- *strrchr(tmp, SPLIT_SLASH) = 0;
- *strrchr(tmp, SPLIT_SLASH) = 0;
- *strrchr(tmp, SPLIT_SLASH) = 0;
- sprintf(tmp, "%s" SPLIT_SLASH_STR "rvc" SPLIT_SLASH_STR "terminaldbstorage" SPLIT_SLASH_STR "RVC.LogSdk" SPLIT_SLASH_STR "RvcLogSdk.db", tmp);
- // 检查文件是否存在
- std::string filePath = tmp;
- std::ifstream file(filePath);
- if (!file.good()) {
- return 1;
- }
- // 检查文件是否被占用
- file.close();
- // 获取当前日期和时间
- time_t now = time(0);
- tm* ltm = localtime(&now);
- // 格式化日期和时间
- char date[11];
- strftime(date, 11, "%Y%m%d", ltm);
- auto uuidStr = uuid4_generateStr(8);
- // 生成新文件名
- std::string newFilePath = filePath;
- size_t pos = newFilePath.find_last_of('.');
- newFilePath.insert(pos, "_" + std::string(date) + "_" + std::string(uuidStr.GetData()));
- if (!std::rename(filePath.c_str(), newFilePath.c_str())) {
- // 重命名文件失败
- return 3;
- }
-
- return 0;
- }
- SPBASE_API int getReduceSpbaseLog()
- {
- return g_spbaseLogNor;
- }
- SPBASE_API bool create_log_producer_default(CSimpleStringA entityName, int idx)
- {
- if (entityName.Compare("SpShell", true) == 0)
- changeDbtoNew();
- refreshLogLevel();
- CSimpleString idxStr = CSimpleString::Format("%d", idx);
- #ifndef _WIN32
-
- WLog_DBG(TAG, "create defaut log_producer(create log producer storage) for %s %d", entityName.GetData(), idxStr.GetData());
- g_logProducer = create_log_producer_storage(entityName, idxStr, "", "", "");
- if (g_logProducer == NULL)
- WLog_ERR(TAG, "create log producer storage return null!");
- return (NULL != g_logProducer);
- #else
- return NULL != (g_logProducer = create_log_producer_storage(entityName, idxStr, "", "", ""));
- #endif
- }
- SPBASE_API void destroy_log_producer_default()
- {
- if (g_logProducer != NULL) {
- log_producer_client* result = (log_producer_client*)g_logProducer;
- destroy_log_producer_client(result);
- g_logProducer = NULL;
- }
- }
- SPBASE_API void DbgWithLinkForC(LOG_LEVEL_E t_level, LOG_TYPE_E t_type, const char* str, ...)
- {
- if (str == nullptr)
- return;
- va_list arg;
- va_start(arg, str);
- int n = _vscprintf(str, arg) + 1;
- std::vector<char> buf(n + 1, '\0');
- vsnprintf(&buf[0], n, str, arg);
- DbgWithLink(t_level, t_type).setResultMsg(&buf[0])();
- va_end(arg);
-
- }
- extern "C" SPBASE_API void __stdcall setCurEntityIdx(int idx)
- {
- g_curEntityIdx = idx;
- }
- void load_debugLevelInCentersetting(const char* fileName)
- {
- array_header_t* arr_section = inifile_read_section_all(fileName);
- g_entityDebugLevelArr.clear();
- g_entityDebugLevelArr["Common"] = 1;
- if (!arr_section)
- return;
- for (int i = 0; i < arr_section->nelts; ++i) {
- char* sec = ARRAY_IDX(arr_section, i, char*);
- array_header_t* arr_key = inifile_read_section_key_all(fileName, sec);
- if (!arr_key) continue;
- for (int j = 0; j < arr_key->nelts; ++j)
- {
- char* key = ARRAY_IDX(arr_key, j, char*);
- if (_stricmp(key, "UpLoadLogLevel") == 0)
- {
- std::string secName = sec;
- std::transform(secName.begin(), secName.end(), secName.begin(), ::tolower);
- g_entityDebugLevelArr[secName] = inifile_read_int(fileName, sec, "UpLoadLogLevel", 2);
- }
-
- }
- }
- }
- void load_debugLevelInMem(std::map<std::string, std::map<std::string, std::string>> & tmp_centerConfig)
- {
- g_entityDebugLevelArr.clear();
- const std::string compareStr = "UpLoadLogLevel";
- for (auto it = tmp_centerConfig.begin(); it != tmp_centerConfig.end(); it++)
- {
- auto sectionInfo = it->second;
- for (auto obj = sectionInfo.begin(); obj != sectionInfo.end(); obj++)
- {
- if (obj->first == compareStr)
- g_entityDebugLevelArr[it->first] = atoi(obj->second.c_str());
- }
- }
- if (g_entityDebugLevelArr.find("Common") == g_entityDebugLevelArr.end())
- g_entityDebugLevelArr["Common"] = 1;
- }
- int getEntityLogLevel(std::string entityName)
- {
- if (g_entityDebugLevelArr.size() == 0)
- return 1;
- if (g_entityDebugLevelArr.find(entityName) != g_entityDebugLevelArr.end())
- return g_entityDebugLevelArr[entityName];
- else
- return g_entityDebugLevelArr["Common"];
- }
- ErrorCodeEnum GetAllEntityIdx(CAutoArray<CSimpleStringA>& strEntityNames, CAutoArray<WORD>& wEntityDevelopIDs)
- {
- sp_env_t* env = sp_get_env();
- sp_cfg_t* cfg = env->cfg;
- array_header_t* arr_entity = cfg->shell_ini->arr_entity;
- strEntityNames.Init(arr_entity->nelts - 1);
- wEntityDevelopIDs.Init(arr_entity->nelts - 1);
- for (int i = 1; i < arr_entity->nelts; ++i) { // from index 1, because zero for special entity, ie. spshell entity
- sp_cfg_shell_entity_t* ent = ARRAY_IDX(arr_entity, i, sp_cfg_shell_entity_t*);
- strEntityNames[i - 1] = ent->name;
- wEntityDevelopIDs[i - 1] = ent->idx;
- }
- return Error_Succeed;
- }
|