123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954 |
- #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"
- #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;
- 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;
- long m_time;
- 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)
- {
- m_time = time(NULL);
- TraceID = ResultMsg = LogCode = API = BussID = TipMsg = SourceType = VtmCode = "";
- logProducer = NULL;
- }
- /** 这里先采用硬编码,控制日志等级 [Gifur@202268]*/
- #if defined(_MSC_VER)
- bool IsAllowToRecord()
- {
- return true;
- }
- #else
- bool IsAllowToRecord()
- {
- return (this->Level >= LOG_LEVEL_INFO);
- }
- #endif //_MSC_VER
- };
- /*北斗链路实例
- {
- "businessId": "ChromiumAutoGen",
- "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 {
-
- 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, m_priPtr->m_time);
- else if (log.Type != LOG_TYPE_SYSTEM)
- rst = log_producer_client_add_log(cur, &log, 1, m_priPtr->m_time);
- }
- }
- //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 {
- 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, m_priPtr->m_time);
- else if (log.Type != LOG_TYPE_SYSTEM)
- log_producer_client_add_log(cur, &log, 1, m_priPtr->m_time);
- }
- 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();
- #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, 50 * 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*)"LR04");
- if(CmptName.GetLength() > 0)
- log_producer_config_set_skyeye_cmptname(config, (char*)CmptName.GetData());
- else
- log_producer_config_set_skyeye_cmptname(config, (char*)"FrameworkLib");
- log_producer_config_set_skyeye_version(config, (char*)"1.0");
- log_producer_config_set_persistent(config, 1);
- #ifndef _WIN32
- 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);
- }
- #endif
- 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*)"LR04");
- 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*)"LR04.02_FrameworkLib");
- log_producer_config_set_united_serviceUnitId(config, (char*)"LR04.02@FrameworkLib_PRD_PRD");
- #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, 50 * 1000);
- log_producer_config_set_skyeye_servname(config, (char*)"Terminal");
- log_producer_config_set_skyeye_cmptId(config, (char*)"LR04");
- log_producer_config_set_skyeye_cmptname(config, (char*)"FrameworkLib");
- 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);
- #ifndef _WIN32
- 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);
- }
- #endif
- 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*)"LR04");
- 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*)"LR04.02_FrameworkLib");
- log_producer_config_set_united_serviceUnitId(config, (char*)"LR04.02@FrameworkLib_PRD_PRD");
- 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 bool create_log_producer_default(CSimpleStringA entityName, int idx)
- {
- 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;
- }
|