1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408 |
- /************** Begin of log.c *******************************************/
- /*
- * log.cpp
- *
- * added by jesse in 2008-2-3
- * 日志相关代码的定义文件
- */
- #ifdef HAVE_CONFIG_H
- #endif
- #include "log4c/config.h"
- #include <stdlib.h>
- #include <string.h>
- #ifdef HAVE_ALLOCA_H
- #include <alloca.h>
- #endif
- #include <sd/sprintf.h>
- #include <sd/malloc.h>
- #include <sd/factory.h>
- #include <log4c/appender.h>
- #include <log4c/priority.h>
- #include <log4c/logging_event.h>
- #include <log4c/category.h>
- #include <log4c/rc.h>
- #include <sd/error.h>
- #include <sd/sd_xplatform.h>
- #include <sd/sd_xplatform.h>
- #include "log4c/vos.h"
- #include "log4c/log.h"
- #include "GlobalMacro.h"
- //////////////////////////////////////////////////////////////////////////
- ///下面括起来代码和log_check拷贝自init.c
- typedef struct log_c_rcfile
- {
- char name[256];
- time_t ctime;
- int exists;
- } log_c_rcfile_t;
- static log_c_rcfile_t log_c_rcfiles[] =
- {
- #if 1
- { "$LOG4C_RCPATH/log4crc" },
- { "$HOME/.log4crc" },
- #endif
- { "./log4crc" }
- };
- static const int log_c_nrcfiles = sizeof(log_c_rcfiles) / sizeof(log_c_rcfiles[0]);
- //////////////////////////////////////////////////////////////////////////
- static SEM_ID gs_semphore = NULL;
- static int gs_iModuleUsage_shmid = 0;
- //////////////////////////////////////////////////////////////////////////
- /** 日志模块初始化
- @return int:return 0 for success
- 作者:jesse 日期:2008-2-3
- */
- int log_init()
- {
- ///设置两个环境变量SD_DEBUG和SD_ERROR,使sd_debug和sd_error两个函数起作用
- ///putenv("SD_DEBUG=SD_DEBUG");
- ///putenv("SD_ERROR=SD_ERROR");
- log_check();
- return log4c_init();
- }
- /** 日志模块初始化,指定配置文件名称
- @return int:return 0 for success
- 作者:jesse 日期:2008-9-5
- */
- int log_init_with_cfg_file(const char *strCfgFileName)
- {
- log_check_with_cfg_file(strCfgFileName);
- return(log4c_init_with_cfg_file(strCfgFileName));
- }
- extern void ReleaseModuleUsage();
- /** 日志模块清理
- @return int:return 0 for success
- 作者:jesse 日期:2008-2-3
- */
- int log_fini()
- {
- int iResult = log4c_fini();
- // 解决资源泄漏 [4/18/2009 jesse]
- VOS_DELETE_SEM(gs_semphore);
- ReleaseModuleUsage();
- return iResult;
- }
- /**检测配置文件是否存在
- @return const int : iPriority.
- 作者:jesse 日期:2008-2-18
- */
- const int log_check(void)
- {
- int iReturn = 0;
- BOOL bCfgFileExist = FALSE;
- int i = 0;
- printf("check configuration files...\n");
- /* check configuration files */
- printf("looking for conf files...\n");
- #if 1
- _snprintf_s(log_c_rcfiles[0].name, sizeof(log_c_rcfiles[0].name) - 1,_TRUNCATE, "%s/log4crc",
- getenv("LOG4C_RCPATH") ? getenv("LOG4C_RCPATH") : LOG4C_RCPATH);
- _snprintf_s(log_c_rcfiles[1].name, sizeof(log_c_rcfiles[1].name) - 1, _TRUNCATE, "%s/.log4crc",
- getenv("HOME") ? getenv("HOME") : "");
- #endif
- for (i = 0; i < log_c_nrcfiles; i++)
- {
- printf("checking for conf file at '%s'\n", log_c_rcfiles[i].name);
- if (SD_ACCESS_READ(log_c_rcfiles[i].name))
- {
- continue;
- }
- if (SD_STAT_CTIME(log_c_rcfiles[i].name,&log_c_rcfiles[i].ctime) != 0)
- {
- sd_error("sd_stat_ctime %s failed", log_c_rcfiles[i].name);
- }
- log_c_rcfiles[i].exists=1;
- bCfgFileExist = TRUE;
- #if 0
- if (log4c_load(log_c_rcfiles[i].name) == -1)
- {
- sd_error("loading %s failed", log_c_rcfiles[i].name);
- iReturn = -1;
- }
- else
- {
- printf("loading %s succeeded\n", log_c_rcfiles[i].name);
- break;
- }
- #endif
- }
- if (TRUE == bCfgFileExist)
- {
- printf("find configuration file\n");
- }
- else
- {
- printf("can't find configuration file,\nplease put you conf file in the same dir of execute file.\n");
- }
- return iReturn;
- }
- /**检测配置文件是否存在,只检测传入的配置文件名
- @return const int : iPriority.
- 作者:jesse 日期:2008-9-4
- */
- const int log_check_with_cfg_file(const char *strCfgFileName)
- {
- int iReturn = 0;
- BOOL bCfgFileExist = FALSE;
- int i = 0;
- printf("check configuration files...\n");
- /* check configuration files */
- printf("looking for conf files...\n");
- #if 1
- _snprintf_s(log_c_rcfiles[0].name, sizeof(log_c_rcfiles[0].name) - 1, _TRUNCATE, "%s/log4crc",
- getenv("LOG4C_RCPATH") ? getenv("LOG4C_RCPATH") : LOG4C_RCPATH);
- _snprintf_s(log_c_rcfiles[1].name, sizeof(log_c_rcfiles[1].name) - 1, _TRUNCATE, "%s/.log4crc",
- getenv("HOME") ? getenv("HOME") : "");
- #endif
- if(NULL == strCfgFileName)
- {
- iReturn = -1;
- }
- else
- {
- printf("checking for conf file at '%s'\n", strCfgFileName);
- if (SD_ACCESS_READ(strCfgFileName))
- {
- iReturn = -1;
- }
- else
- {
- if (SD_STAT_CTIME(strCfgFileName,&log_c_rcfiles[i].ctime) != 0)
- {
- sd_error("sd_stat_ctime %s failed", strCfgFileName);
- }
- bCfgFileExist = TRUE;
- #if 0
- if (log4c_load(strCfgFileName) == -1)
- {
- sd_error("loading %s failed", strCfgFileName);
- iReturn = -1;
- }
- else
- {
- printf("loading %s succeeded\n", strCfgFileName);
- }
- #endif
- }
- }
- if (TRUE == bCfgFileExist)
- {
- printf("find configuration file\n");
- }
- else
- {
- printf("can't find configuration file,\nplease put you conf file in the same dir of execute file.\n");
- }
- return iReturn;
- }
- /** 日志记录
- 日志记录为一个字符串指针指向的内容
- @return void
- 作者:jesse 日期:2008-2-3
- */
- void log_msg(
- const char *strFile, ///文件名
- const int iLineNum, ///行号
- const char *strCatName, ///category名
- const int iPriority, ///日志记录级别
- const char *strFormat, ///日志内容格式
- ... ///日志内容
- )
- {
- const log4c_category_t* a_category = log4c_category_get(strCatName);
- if (log4c_category_is_priority_enabled(a_category, iPriority))
- {
- va_list va;
- va_start(va, strFormat);
- ///下面从带路径的文件名解析出不带路径的纯文件名
- {
- ///static SEM_ID semphore = NULL;
- const char * strFilePath = strFile;
- const char * strFileName = strrchr(strFilePath, '/');
- char strMsgBuffer[LOG4C_BUFFER_SIZE_MAX] = {0};
- ///为线程安全这里加锁,[2/15/2008 jesse]
- if (NULL == gs_semphore)
- {
- gs_semphore = VOS_CreateMSem(VOS_SEM_Q_FIFO);
- }
- VOS_TakeSem(gs_semphore, VOS_WAIT_FOREVER);
- if (strFileName != NULL)
- {
- strFileName++;
- }
- else
- {
- strFileName = strrchr(strFilePath, '\\');
- if (strFileName != NULL)
- {
- strFileName++;
- }
- else
- {
- strFileName = strFilePath;
- }
- }
- _snprintf_s(strMsgBuffer, sizeof(strMsgBuffer)-1, _TRUNCATE, "%-8s(%d) %s", strFileName, iLineNum, strFormat);
- log4c_category_vlog(a_category, iPriority, strMsgBuffer, va);
- // 解锁 [2/15/2008 jesse]
- VOS_GiveSem(gs_semphore);
- }
- va_end(va);
- }
- }
- /** 日志记录
- 日志记录为一个字符串指针指向的内容
- @return void
- 作者:jesse 日期:2008-2-3
- */
- void log_msg_func(
- const char *strFile, ///文件名
- const char *strFuncName,///函数名
- const int iLineNum, ///行号
- const char *strCatName, ///category名
- const int iPriority, ///日志记录级别
- const char *strFormat, ///日志内容格式
- ... ///日志内容
- )
- {
- const log4c_category_t* a_category = log4c_category_get(strCatName);
- if (log4c_category_is_priority_enabled(a_category, iPriority))
- {
- va_list va;
- va_start(va, strFormat);
- ///下面从带路径的文件名解析出不带路径的纯文件名
- {
- ///static SEM_ID semphore = NULL;
- const char * strFilePath = strFile;
- const char * strFileName = strrchr(strFilePath, '/');
- char strMsgBuffer[LOG4C_BUFFER_SIZE_MAX] = {0};
- ///为线程安全这里加锁,[2/15/2008 jesse]
- if (NULL == gs_semphore)
- {
- gs_semphore = VOS_CreateMSem(VOS_SEM_Q_FIFO);
- }
- VOS_TakeSem(gs_semphore, VOS_WAIT_FOREVER);
- if (strFileName != NULL)
- {
- strFileName++;
- }
- else
- {
- strFileName = strrchr(strFilePath, '\\');
- if (strFileName != NULL)
- {
- strFileName++;
- }
- else
- {
- strFileName = strFilePath;
- }
- }
- _snprintf_s(strMsgBuffer, sizeof(strMsgBuffer)-1, _TRUNCATE, "%-8s(%d) Function:%s %s\n", strFileName, iLineNum, strFuncName, strFormat);
- log4c_category_vlog(a_category, iPriority, strMsgBuffer, va);
- // 解锁 [2/15/2008 jesse]
- VOS_GiveSem(gs_semphore);
- }
- va_end(va);
- }
- }
- /************************************************************************/
- /* 函数:[3/30/2019 Wang];
- /* 描述:日志记录,不记录文件名和行号,日志记录为一个字符串指针指向的内容;
- /* 参数:;
- /* [IN] strCatName:category名;
- /* [IN] iPriority:日志记录级别;
- /* [IN] strFormat:日志内容格式;
- /* [IN] ...:日志内容;
- /* 返回:void;
- /************************************************************************/
- void log_msg_no_file_num(const char *strCatName, const int iPriority, const char *strFormat, ...)
- {
- const log4c_category_t* a_category = log4c_category_get(strCatName);
- if (log4c_category_is_priority_enabled(a_category, iPriority))
- {
- va_list va;
- va_start(va, strFormat);
- {
- ///static SEM_ID semphore = NULL;
- char strMsgBuffer[10240] = {0};
- ///为线程安全这里加锁,[2/15/2008 jesse]
- if (NULL == gs_semphore)
- {
- gs_semphore = VOS_CreateMSem(VOS_SEM_Q_FIFO);
- }
- VOS_TakeSem(gs_semphore, VOS_WAIT_FOREVER);
- _snprintf_s(strMsgBuffer, sizeof(strMsgBuffer)-1, _TRUNCATE, "%s\n", strFormat);
- log4c_category_vlog(a_category, iPriority, strMsgBuffer, va);
- // 解锁 [2/15/2008 jesse]
- VOS_GiveSem(gs_semphore);
- }
- va_end(va);
- }
- }
- /** 日志记录,不记录文件名和行号,没有任何layout转换,直接输出相应的字符文本到日志中
- 此条记录没有行号,也没有线程号,也没有回车等
- 日志记录为一个字符串指针指向的内容
- @return void
- 作者:jesse 日期:2008-9-14
- */
- void log_msg_no_file_num_no_layout(
- const char *strCatName, ///category名
- const int iPriority, ///日志记录级别
- const char *strFormat, ///日志内容格式
- ... ///日志内容
- )
- {
- const log4c_category_t* a_category = log4c_category_get(strCatName);
- if (log4c_category_is_priority_enabled(a_category, iPriority))
- {
- va_list va;
- va_start(va, strFormat);
- {
- ///static SEM_ID semphore = NULL;
- char strMsgBuffer[10240] = {0};
- ///为线程安全这里加锁,[2/15/2008 jesse]
- if (NULL == gs_semphore)
- {
- gs_semphore = VOS_CreateMSem(VOS_SEM_Q_FIFO);
- }
- VOS_TakeSem(gs_semphore, VOS_WAIT_FOREVER);
- _snprintf_s(strMsgBuffer, sizeof(strMsgBuffer)-1, _TRUNCATE, "%s", strFormat);
- log4c_category_vlog_no_file_num_no_layout(a_category, iPriority, strMsgBuffer, va);
- // 解锁 [2/15/2008 jesse]
- VOS_GiveSem(gs_semphore);
- }
- va_end(va);
- }
- }
- /** 设置appender
- @return int:
- 作者:jesse 日期:2008-2-3
- */
- int log_setappender(
- const char *strCatName, ///category名
- const char *strAppenderName ///appender名
- )
- {
- log4c_category_set_appender(log4c_category_get(strCatName), log4c_appender_get(strAppenderName));
- return(0);
- }
- /** 日志记录
- 支持类似printf函数的带格式输出
- @return void
- 作者:jesse 日期:2008-2-3
- */
- void log_log(
- const char *strCatName, ///category名
- const int iPriority, ///日志记录级别
- const char* strFormat, ///日志内容格式
- ... ///日志内容
- )
- {
- const log4c_category_t* a_category = log4c_category_get(strCatName);
- if (log4c_category_is_priority_enabled(a_category, iPriority))
- {
- va_list va;
- va_start(va, strFormat);
- log4c_category_vlog(a_category, iPriority, strFormat, va);
- va_end(va);
- }
- }
- #if 0
- /** 日志记录Wrapper函数
- 日志记录为一个字符串指针指向的内容
- @return int:return 0 for success
- 作者:jesse 日期:2008-2-4
- */
- void log_msg_wrapper (
- const int iPriority, ///日志记录级别
- const char *strFormat, ///日志内容格式
- ... ///日志内容
- )
- {
- const log4c_category_t* a_category = log4c_category_get(DEFAULT_LOG_CATEGORY_NAME);
- ///log_msg(__FILE__, __LINE__, DEFAULT_LOG_CATEGORY_NAME, iPriority, strFormat);
- if (log4c_category_is_priority_enabled(a_category, iPriority))
- {
- char strMsg[10240] = {0};
- va_list va;
- va_start(va, strFormat);
- {
- int iSprintSize = 0;
- iSprintSize = vsnprintf(strMsg, sizeof(strMsg), strFormat, va);
- if ( iSprintSize >= sizeof(strMsg))
- {
- sd_error("truncating message of %d bytes (bufsize = %d)", iSprintSize, sizeof(strMsg));
- }
- }
- va_end(va);
- log4c_category_vlog(a_category, iPriority, strMsg, va);
- }
- }
- #endif
- const LOG_PARAM log_vsprintf_wrapper(
- const int iPriority, ///日志记录级别
- const char* strFormat, ///日志内容格式
- ... ///日志内容
- )
- {
- LOG_PARAM log_param;
- va_list va;
- va_start(va, strFormat);
- {
- #if 1
- _vsnprintf_s(log_param.strMsg, LOG4C_BUFFER_SIZE_DEFAULT, _TRUNCATE, strFormat, va);
- #else
- memcpy(log_param.strMsg, sd_vsprintf( strFormat, va), sizeof(log_param.strMsg));
- #endif
- }
- va_end(va);
- log_param.iPriority = iPriority;
- return log_param;
- }
- const LOG_PARAM log_condition_vsnprintf_wrapper(
- const int iCondition, ///条件
- const int iPriority, ///日志记录级别
- const char* strFormat, ///日志内容格式
- ... ///日志内容
- )
- {
- LOG_PARAM log_param;
- va_list va;
- va_start(va, strFormat);
- {
- #if 1
- _vsnprintf_s(log_param.strMsg, LOG4C_BUFFER_SIZE_DEFAULT, _TRUNCATE, strFormat, va);
- #else
- memcpy(log_param.strMsg, sd_vsprintf( strFormat, va), sizeof(log_param.strMsg));
- #endif
- }
- va_end(va);
- log_param.iCondition = iCondition;
- log_param.iPriority = iPriority;
- return log_param;
- }
- /************************************************************************/
- /* 函数:[3/30/2019 Wang];
- /* 描述:;
- /* 参数:;
- /* [IN] iPriority:日志记录级别;
- /* [IN] strHexBuf:缓冲区首地址;
- /* [IN] iHexBufLen:缓冲区长度;
- /* [IN] ...:日志内容;
- /* 返回:void;
- /************************************************************************/
- void log_hex_dump_vsnprintf_wrapper(const int iPriority, const char* strHexBuf, const int iHexBufLen, ...)
- {
- char *strBuffer = (char *)malloc(LOG4C_BUFFER_SIZE_MAX);
- memset(strBuffer, 0, LOG4C_BUFFER_SIZE_MAX);
- log4c_sprintf_data((char*)(strHexBuf), (iHexBufLen), strBuffer);
- log_msg_no_file_num(DEFAULT_LOG_CATEGORY_NAME, iPriority, strBuffer );
- }
- /************************************************************************/
- /* 函数:[3/30/2019 Wang];
- /* 描述:;
- /* 参数:;
- /* [IN] strFormat:日志内容格式;
- /* [IN] ...:日志内容;
- /* 返回:void;
- /************************************************************************/
- const LOG_PARAM log_vsnprintf_wrapper_msg(const char* strFormat, ...)
- {
- LOG_PARAM log_param;
- va_list va;
- va_start(va, strFormat);
- {
- #if 1
- _vsnprintf_s(log_param.strMsg, LOG4C_BUFFER_SIZE_DEFAULT, _TRUNCATE, strFormat, va);
- #else
- memcpy(log_param.strMsg, sd_vsprintf( strFormat, va), sizeof(log_param.strMsg));
- #endif
- }
- va_end(va);
- return log_param;
- }
- void log_msg_wrapper(
- const char *strFileName,///文件名
- const int iLineNum, ///行号
- const char *strCatName, ///category名
- const int iPriority, ///日志记录级别
- const char *strFormat, ///日志内容格式
- ... ///日志内容
- )
- {
- const log4c_category_t* a_category = log4c_category_get(strCatName);
- if (log4c_category_is_priority_enabled(a_category, iPriority))
- {
- va_list va;
- va_start(va, strFormat);
- log4c_category_vlog(a_category, iPriority, strFormat, va);
- va_end(va);
- }
- }
- /**宏参数抽取format函数
- 本函数接受LOG_DEBUG(X)的参数,并从该宏定义的参数中返回format的值
- @return const char* : strFormat
- 作者:jesse 日期:2008-2-4
- */
- const char* log_get_format_wrapper(
- const int iPriority, ///日志记录级别
- const char* strFormat, ///日志内容格式
- ... ///日志内容
- )
- {
- return strFormat;
- #if 0
- char* buffer = strdup(strFormat);
- return buffer;
- #endif
- }
- /**宏参数抽取priority函数
- 本函数接受LOG_DEBUG(X)的参数,并从该宏定义的参数中返回priority的值
- @return const int : iPriority.
- 作者:jesse 日期:2008-2-4
- */
- const int log_get_priority_wrapper(
- const int iPriority, ///日志记录级别
- const char* strFormat, ///日志内容格式
- ... ///日志内容
- )
- {
- return iPriority;
- }
- /**
- 将大小为count的缓冲区中内容按字节以16进制字符串打印出来,
- 返回值即为指向相应的字符串,
- 该返回指向的存储区域要调用本函数的用户显示的进行删除
- */
- void log4c_sprintf_data(char *buff, int count, char *dest_buffer)
- {
- int i,j,c;
- int printnext=1;
- char str[10] = {0};///由于这里要进行格式输出如"%.4x "等,这里给出10个字符作为格式输出冗余
- if(count)
- {
- if(count%16)
- {
- c=count+(16-count%16);
- }
- else
- {
- c=count;
- }
- }
- else
- {
- c=count;
- }
- for(i=0;i<c;i++)
- {
- if(printnext)
- {
- printnext--;
- _snprintf_s(str, sizeof(str), _TRUNCATE, "%.4x ",i&0xffff);
- strncat_s(dest_buffer, LOG4C_BUFFER_SIZE_MAX, str, count);
- ///ostr << str;
- }
- if(i<count)
- {
- _snprintf_s(str, sizeof(str), _TRUNCATE, "%3.2x",buff[i]&0xff);
- strncat_s(dest_buffer, LOG4C_BUFFER_SIZE_MAX, str, count);
- ///ostr << str;
- }
- else
- {
- _snprintf_s(str, sizeof(str), _TRUNCATE, " ");
- strncat_s(dest_buffer, LOG4C_BUFFER_SIZE_MAX, str, count);
- ///ostr << str;
- }
- if(!((i+1)%8))
- {
- if((i+1)%16)
- {
- _snprintf_s(str, sizeof(str), _TRUNCATE, " -");
- strncat_s(dest_buffer, LOG4C_BUFFER_SIZE_MAX, str, count);
- ///ostr << str;
- }
- else
- {
- _snprintf_s(str, sizeof(str), _TRUNCATE, " ");
- strncat_s(dest_buffer, LOG4C_BUFFER_SIZE_MAX, str, count);
- ///ostr << str;
- for(j=i-15;j<=i;j++)
- {
- if(j<count)
- {
- if( (buff[j]&0xff) >= 0x20 &&
- (buff[j]&0xff)<=0x7e)
- {
- _snprintf_s(str, sizeof(str), _TRUNCATE, "%c",buff[j]&0xff);
- strncat_s(dest_buffer, LOG4C_BUFFER_SIZE_MAX, str, count);
- }
- else
- {
- _snprintf_s(str, sizeof(str), _TRUNCATE, ".");
- strncat_s(dest_buffer, LOG4C_BUFFER_SIZE_MAX, str, count);
- }
- }
- else
- {
- _snprintf_s(str, sizeof(str), _TRUNCATE, " ");
- strncat_s(dest_buffer, LOG4C_BUFFER_SIZE_MAX, str, count);
- ///ostr << str;
- }
- }
- _snprintf_s(str, sizeof(str), _TRUNCATE, "\n");
- strncat_s(dest_buffer, LOG4C_BUFFER_SIZE_MAX, str, count);
- printnext=1;
- }
- }
- }
- }
- //////////////////////////////////////////////////////////////////////////
- //////////////////////////////////////////////////////////////////////////
- static char gs_strFileContent_InC[] =
- "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n"
- "<!DOCTYPE log4c SYSTEM \"\">\n"
- "\n"
- "<log4c version=\"1.2.1\">\n"
- " <config>\n"
- " <bufsize>0</bufsize>\n"
- " <debug level=\"2\"/>\n"
- " <nocleanup>0</nocleanup>\n"
- " <reread>1</reread>\n"
- " </config>\n"
- "\n"
- " <category name=\"root\" priority=\"notice\" appender=\"aname\"/>\n"
- "\n"
- " <rollingpolicy name=\"a_policy_name\" type=\"sizewin\" maxsize=\"1048576\" maxnum=\"15\" />\n"
- " <appender name=\"aname\" type=\"rollingfile\" logdir=\".\" prefix=\"log\" ext=\"txt\" layout=\"dated_threadid\" rollingpolicy=\"a_policy_name\" />\n"
- "\n"
- " <appender name=\"stdout\" type=\"stream\" layout=\"dated_threadid\"/>\n"
- " <appender name=\"stderr\" type=\"stream\" layout=\"dated\"/>\n"
- " <appender name=\"syslog\" type=\"syslog\" layout=\"basic\"/>\n"
- "</log4c>\n";
- static char gs_strDefualtFileName[] = "log4crc";
- int log_init_with_string(
- const char *strFileContent,
- const char *strFileName
- )
- {
- int iResult = FAILURE;
- const char * strLocalFileContent = NULL;
- const char * strLocalFileName = NULL;
- PAssertNotNull_ReturnWithValue(strFileContent, FAILURE);
- PAssertNotNull_ReturnWithValue(strFileName, FAILURE);
- strLocalFileContent = (strlen(strFileContent) > 0 ? strFileContent:gs_strFileContent_InC);
- strLocalFileName = (strlen(strFileName) > 0 ? strFileName:gs_strDefualtFileName);
- /**
- 若文件已存在则不重新生成配置文件了
- 否则生成新的配置文件
- */
- if (OK != SD_ACCESS_READ(strLocalFileName))
- {
- ///文件不存在新建
- FILE *file = fopen(strLocalFileName, "wb");
- if (NULL != file)
- {
- fwrite(strLocalFileContent, 1, strlen(strLocalFileContent), file);
- fclose(file);
- }
- }
- else
- {
- ///文件存在,do nothing,使用原来的配置文件就OK了。
- }
- //////////////////////////////////////////////////////////////////////////
- ///调用Init函数进行实际的日志模块初始化工作
- iResult = log_init_with_cfg_file_wrapper(strLocalFileName);
- return iResult;
- }
- int log_init_with_cfg_file_wrapper(const char * strConfigFile)
- {
- int iResult = FAILURE;
- PAssertNotNull_ReturnWithValue(strConfigFile, FAILURE);
- ///调用log4c的函数来初始化日志
- if (strlen(strConfigFile) > 0)
- {
- iResult = log_init_with_cfg_file(strConfigFile);
- }
- if ( OK != iResult)
- {
- iResult = log_init();
- }
- return iResult;
- }
- /*****************************新增的一些日志模块配置属性设置类****************/
- #define MAX_FILE_NAME_LEN 512
- struct ST_LogProperty
- {
- char m_strLogCfgFileName[MAX_FILE_NAME_LEN];
- char m_strLogFileName[MAX_FILE_NAME_LEN];
- int m_iLogFileSize;
- char m_strLogLevel[128];
- int m_iLogFileNum;
- BOOL m_bReReadLogCfgFile;
- };
- typedef struct ST_LogProperty LogProperty;
- static LogProperty gs_st_LogProperty =
- {
- "log4crc",
- "log",
- 1048576,
- "notice",
- 15,
- TRUE
- };
- static char gs_strFileContentFormat[] =
- "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n"
- "<!DOCTYPE log4c SYSTEM \"\">\n"
- "\n"
- "<log4c version=\"1.2.1\">\n"
- " <config>\n"
- " <bufsize>0</bufsize>\n"
- " <debug level=\"2\"/>\n"
- " <nocleanup>0</nocleanup>\n"
- " <reread>%d</reread>\n"
- " </config>\n"
- "\n"
- " <category name=\"root\" priority=\"%s\" appender=\"aname\"/>\n"
- "\n"
- " <rollingpolicy name=\"a_policy_name\" type=\"sizewin\" maxsize=\"%d\" maxnum=\"%d\" />\n"
- " <appender name=\"aname\" type=\"rollingfile\" logdir=\".\" prefix=\"%s\" ext=\"txt\" layout=\"dated_threadid\" rollingpolicy=\"a_policy_name\" />\n"
- "\n"
- " <appender name=\"stdout\" type=\"stream\" layout=\"dated_threadid\"/>\n"
- " <appender name=\"stderr\" type=\"stream\" layout=\"dated\"/>\n"
- " <appender name=\"syslog\" type=\"syslog\" layout=\"basic\"/>\n"
- "</log4c>\n";
- static char gs_strFileContentBuffer[4096] = {0};
- void log_set_log_cfg_file_name(const char *strFileName)
- {
- PAssertNotNull_Return(strFileName);
- strncpy_s(gs_st_LogProperty.m_strLogCfgFileName, sizeof(gs_st_LogProperty.m_strLogCfgFileName), strFileName, _TRUNCATE);
- }
- void log_set_log_file_name(const char *strFileName)
- {
- PAssertNotNull_Return(strFileName);
- strncpy_s(gs_st_LogProperty.m_strLogCfgFileName, sizeof(gs_st_LogProperty.m_strLogCfgFileName), strFileName, _TRUNCATE);
- }
- void log_set_log_level(const char *strLogLevel)
- {
- strncpy_s(gs_st_LogProperty.m_strLogLevel, sizeof(gs_st_LogProperty.m_strLogLevel), strLogLevel, _TRUNCATE);
- }
- void log_set_log_file_size(const int iFileSize)
- {
- gs_st_LogProperty.m_iLogFileSize = iFileSize;
- }
- void log_set_log_file_num(const int iFileNum)
- {
- gs_st_LogProperty.m_iLogFileNum = iFileNum;
- }
- void log_set_reread_log_cfg_file(const BOOL bReReadLogCfgFile)
- {
- gs_st_LogProperty.m_bReReadLogCfgFile = bReReadLogCfgFile;
- }
- int log_init_with_param()
- {
- _snprintf_s(gs_strFileContentBuffer, sizeof(gs_strFileContentBuffer),_TRUNCATE,
- gs_strFileContentFormat,
- gs_st_LogProperty.m_bReReadLogCfgFile,
- gs_st_LogProperty.m_strLogLevel,
- gs_st_LogProperty.m_iLogFileSize,
- gs_st_LogProperty.m_iLogFileNum,
- gs_st_LogProperty.m_strLogFileName
- );
- return log_init_with_string(gs_strFileContentBuffer, gs_st_LogProperty.m_strLogCfgFileName);
- }
- /************************************************************************/
- /************************************************************************/
- // Instruct the compiler to put the g_lModuleUsage data
- // variable in its own data section, called Shared. We
- // then instruct the linker that we want the data in this
- // section to be shared by all instances of this application.
- #ifdef WIN32
- #pragma data_seg("LogShared")
- /** 数据库日志模块运行的实例个数
- 避免多个日志模块的运行实例使用同一个配置文件,避免使用同一个log文件来记录日志而产生冲突
- */
- long g_lLogModuleUsage = -1;
- #pragma data_seg()
- // Instruct the linker to make the Shared section
- // readable, writable, and shared.
- #pragma comment(linker, "/section:LogShared,rws")
- #else
- // long g_lLogModuleUsage __attribute__((section ("shared"), shared)) = -1;
- // int g_lLogModuleUsage __attribute__((section ("shared"), shared)) = -1;
- // extern long GetModuleUsage();
- // long g_lLogModuleUsage = GetModuleUsage();
- long g_lLogModuleUsage = -1;
- #endif
- int log_init_with_param_multi_process()
- {
- char strLogCfgFileName[MAX_FILE_NAME_LEN] = {0};
- char strLogFileName[MAX_FILE_NAME_LEN] = {0};
- char strLogModuleUsageNum[MAX_FILE_NAME_LEN] = {0};
- IncreaseLogModuleUsage();
- //snprintf(gs_st_LogProperty.m_strLogCfgFileName, sizeof(gs_st_LogProperty.m_strLogCfgFileName),
- // "%s%d", gs_st_LogProperty.m_strLogCfgFileName, g_lLogModuleUsage);
- //snprintf(gs_st_LogProperty.m_strLogFileName, sizeof(gs_st_LogProperty.m_strLogFileName),
- // "%s%d", gs_st_LogProperty.m_strLogFileName, g_lLogModuleUsage);
- _snprintf_s(strLogModuleUsageNum, sizeof(strLogModuleUsageNum), _TRUNCATE, "%d", g_lLogModuleUsage);
- strcat_s(gs_st_LogProperty.m_strLogCfgFileName, sizeof(gs_st_LogProperty.m_strLogCfgFileName), strLogModuleUsageNum);
- strcat_s(gs_st_LogProperty.m_strLogFileName, sizeof(gs_st_LogProperty.m_strLogFileName),strLogModuleUsageNum);
- return log_init_with_param();
- }
- extern long GetModuleUsage();
- extern void SetModuleUsage(long lModuleUsage);
- void IncreaseLogModuleUsage()
- {
- #ifdef WIN32
- InterlockedIncrement((long*) &g_lLogModuleUsage);
- #else
- g_lLogModuleUsage = GetModuleUsage();
- g_lLogModuleUsage ++;
- SetModuleUsage(g_lLogModuleUsage);
- #endif
- }
- /************************************************************************/
- /************************************************************************/
- ///下面为LOG4C_BLOCK_XXX相关宏定义的实现代码
- ///进行LOG4C_BLOCK_BEGIN和LOG4C_BLOCK_END宏定义使用的Block结构
- struct ST_Block
- {
- #define BLOCK_FREE 0
- #define BLOCK_BUSY 1
- ///标志是否被使用
- int iUseStatus;
- ///线程ID
- TASK_ID threadId;
- ///Block的层次
- int iBlockLevel;
- void (*Init)(struct ST_Block *ptrThis);
- void (*Reset)(struct ST_Block *ptrThis);
- int (*GetUseStatus)(struct ST_Block *ptrThis);
- TASK_ID (*GetThreadId)(struct ST_Block *ptrThis);
- int (*GetBlockLevel)(struct ST_Block *ptrThis);
- void (*SetUseStatus)(struct ST_Block *ptrThis, int iUseStatus);
- void (*SetThreadId)(struct ST_Block *ptrThis, TASK_ID threadId);
- void (*SetBlockLevel)(struct ST_Block *ptrThis, int iBlockLevel);
- };
- typedef struct ST_Block Block;
- /**一个ST_BlockGroup包含5个ST_Block
- ST_BlockGroup中作为ST_BlockManager中管理的节点
- */
- struct ST_BlockGroup
- {
- #define BLOCK_GROUP_BLOCK_NUM (5)
- Block aBlock[BLOCK_GROUP_BLOCK_NUM];
- struct ST_BlockGroup *ptrBlockGroupPre;
- struct ST_BlockGroup *ptrBlockGroupNext;
- void (*Init)(struct ST_BlockGroup *ptrThis);
- Block *(*GetThreadBlock)(struct ST_BlockGroup *ptrThis, TASK_ID threadId);
- Block *(*GetFreeBlock)(struct ST_BlockGroup *ptrThis);
- };
- typedef struct ST_BlockGroup BlockGroup;
- struct ST_BlockManager
- {
- BlockGroup *ptrBlockGroupHeader;
- BlockGroup *ptrBlockGroupTail;
- Block *(*GetThreadBlock)(struct ST_BlockManager *ptrThis, TASK_ID threadId);
- Block *(*GetFreeBlock)(struct ST_BlockManager *ptrThis);
- };
- typedef struct ST_BlockManager BlockManager;
- extern Block *BlockManager_GetThreadBlock(BlockManager *ptrThis, TASK_ID threadId);
- extern Block *BlockManager_GetFreeBlock(BlockManager *ptrThis);
- BlockManager g_blockManager =
- {
- NULL,
- NULL,
- BlockManager_GetThreadBlock,
- BlockManager_GetFreeBlock
- };
- extern void Block_Reset(Block *ptrThis);
- extern int Block_GetUseStatus(Block *ptrThis);
- extern TASK_ID Block_GetThreadId(Block *ptrThis);
- extern int Block_GetBlockLevel(Block *ptrThis);
- extern void Block_SetUseStatus(Block *ptrThis, int iUseStatus);
- extern void Block_SetThreadId(Block *ptrThis, TASK_ID threadId);
- extern void Block_SetBlockLevel(Block *ptrThis, int iBlockLevel);
- /**ST_Block的初始化代码
- @param :Block *ptrThis
- @rerurn void.
- */
- void Block_Init(Block *ptrThis)
- {
- PAssert_Return(ptrThis);
- memset(ptrThis, 0, sizeof(*ptrThis));
- ///设置本结构的成员函数
- ptrThis->Init = Block_Init;
- ptrThis->Reset = Block_Reset;
- ptrThis->SetBlockLevel = Block_SetBlockLevel;
- ptrThis->GetBlockLevel = Block_GetBlockLevel;
- ptrThis->SetThreadId = Block_SetThreadId;
- ptrThis->GetThreadId = Block_GetThreadId;
- ptrThis->SetUseStatus = Block_SetUseStatus;
- ptrThis->GetUseStatus = Block_GetUseStatus;
- return;
- }
- /**ST_Block的重置代码
- @param :Block *ptrThis
- @rerurn void.
- */
- void Block_Reset(Block *ptrThis)
- {
- PAssert_Return(ptrThis);
- Block_Init(ptrThis);
- return;
- }
- /**ST_Block的获取成员变量iUseStatus的函数
- 其实直接引用成员变量就可以了,这里为了练习C实现C++而特意实现的
- @param :int
- @rerurn void.
- */
- int Block_GetUseStatus(Block *ptrThis)
- {
- PAssert_ReturnWithValue(ptrThis, 0);
- return ptrThis->iUseStatus;
- }
- /**ST_Block的获取成员变量threadId的函数
- 其实直接引用成员变量就可以了,这里为了练习C实现C++而特意实现的
- @param :TASK_ID.
- @rerurn void.
- */
- TASK_ID Block_GetThreadId(Block *ptrThis)
- {
- PAssert_ReturnWithValue(ptrThis, 0);
- return ptrThis->threadId;
- }
- /**ST_Block的获取成员变量iBlockLevel的函数
- 其实直接引用成员变量就可以了,这里为了练习C实现C++而特意实现的
- @param :int.
- @rerurn void.
- */
- int Block_GetBlockLevel(Block *ptrThis)
- {
- PAssert_ReturnWithValue(ptrThis, 0);
- return ptrThis->iBlockLevel;
- }
- /**ST_Block的设置成员变量iUseStatus的函数
- 其实直接引用成员变量就可以了,这里为了练习C实现C++而特意实现的
- @param :Block *ptrThis
- @param :int
- @rerurn void.
- */
- void Block_SetUseStatus(Block *ptrThis, int iUseStatus)
- {
- PAssert_Return(ptrThis);
- ptrThis->iUseStatus = iUseStatus;
- }
- /**ST_Block的设置成员变量threadId的函数
- 其实直接引用成员变量就可以了,这里为了练习C实现C++而特意实现的
- @param :Block *ptrThis
- @param :TASK_ID.
- @rerurn void.
- */
- void Block_SetThreadId(Block *ptrThis, TASK_ID threadId)
- {
- PAssert_Return(ptrThis);
- ptrThis->threadId = threadId;
- }
- /**ST_Block的设置成员变量iBlockLevel的函数
- 其实直接引用成员变量就可以了,这里为了练习C实现C++而特意实现的
- @param :Block *ptrThis
- @param :int.
- @rerurn void.
- */
- void Block_SetBlockLevel(Block *ptrThis, int iBlockLevel)
- {
- PAssert_Return(ptrThis);
- ptrThis->iBlockLevel = iBlockLevel;
- }
- extern Block *BlockGroup_GetThreadBlock(BlockGroup *ptrThis, TASK_ID threadId);
- extern Block *BlockGroup_GetFreeBlock(BlockGroup *ptrThis);
- /**ST_BlockGroup的初始化代码
- @param :Block *ptrThis
- @rerurn void.
- */
- void BlockGroup_Init(BlockGroup *ptrThis)
- {
- int i = 0;
- PAssert_Return(ptrThis);
- memset(ptrThis, 0, sizeof(*ptrThis));
- ptrThis->Init = BlockGroup_Init;
- ptrThis->GetThreadBlock = BlockGroup_GetThreadBlock;
- ptrThis->GetFreeBlock = BlockGroup_GetFreeBlock;
- ///初始化每个Block
- for (i = 0; i < BLOCK_GROUP_BLOCK_NUM; i++)
- {
- Block *ptrBlock = &ptrThis->aBlock[i];
- ptrBlock->Init = Block_Init;
- ptrBlock->Init(ptrBlock);
- }
- return;
- }
- /**从ST_BlockGroup中获取给定线程的Block的指针
- @param :Block *ptrThis
- @param :TASK_ID threadId
- @rerurn Block *.
- */
- Block *BlockGroup_GetThreadBlock(BlockGroup *ptrThis, TASK_ID threadId)
- {
- Block *ptrBlockReturn = NULL;
- int i = 0;
- PAssert_ReturnWithValue(ptrThis, NULL);
- for (i = 0; i < BLOCK_GROUP_BLOCK_NUM; i++)
- {
- Block *ptrBlock = &ptrThis->aBlock[i];
- if (ptrBlock->GetThreadId(ptrBlock) == threadId)
- {
- ptrBlockReturn = ptrBlock;
- break;
- }
- }
- return ptrBlockReturn;
- }
- /**从ST_BlockGroup中获取空闲的Block的指针
- @param :BlockGroup *ptrThis
- @rerurn Block *.
- */
- Block *BlockGroup_GetFreeBlock(BlockGroup *ptrThis)
- {
- Block *ptrBlockReturn = NULL;
- int i = 0;
- PAssert_ReturnWithValue(ptrThis, NULL);
- for (i = 0; i < BLOCK_GROUP_BLOCK_NUM; i++)
- {
- Block *ptrBlock = &ptrThis->aBlock[i];
- if (ptrBlock->GetUseStatus(ptrBlock) == BLOCK_FREE)
- {
- ptrBlockReturn = ptrBlock;
- break;
- }
- }
- return ptrBlockReturn;
- }
- Block *BlockManager_GetThreadBlock(BlockManager *ptrThis, TASK_ID threadId)
- {
- Block *ptrBlockReturn = NULL;
- BlockGroup *ptrBlockGroup = NULL;
- PAssert_ReturnWithValue(ptrThis, NULL);
- if (NULL == ptrThis->ptrBlockGroupHeader)
- {
- BlockGroup *ptrGroupBlock = (BlockGroup *)malloc(sizeof(BlockGroup));
- ptrGroupBlock->Init = BlockGroup_Init;
- ptrGroupBlock->Init(ptrGroupBlock);
- ptrThis->ptrBlockGroupHeader = ptrGroupBlock;
- ptrThis->ptrBlockGroupTail = ptrGroupBlock;
- }
- PAssert_ReturnWithValue(ptrThis->ptrBlockGroupHeader, NULL);
- ptrBlockGroup = ptrThis->ptrBlockGroupHeader;
- do
- {
- ptrBlockReturn = ptrBlockGroup->GetThreadBlock(ptrBlockGroup, threadId);
- if (NULL != ptrBlockReturn)
- {
- break;
- }
- else
- {
- ptrBlockGroup = ptrBlockGroup->ptrBlockGroupNext;
- }
- } while (NULL != ptrBlockGroup);
- ///此时找遍所有的链表都没有找到相应的Block,则重新找块没用的Block
- if (NULL == ptrBlockReturn)
- {
- ptrBlockReturn = ptrThis->GetFreeBlock(ptrThis);
- }
- return ptrBlockReturn;
- }
- Block *BlockManager_GetFreeBlock(BlockManager *ptrThis)
- {
- Block *ptrBlockReturn = NULL;
- PAssert_ReturnWithValue(ptrThis, NULL);
- if(NULL == ptrThis->ptrBlockGroupHeader)
- {
- ///原来没有空间则动态分配
- BlockGroup *ptrGroupBlock = (BlockGroup *)malloc(sizeof(BlockGroup));
- ptrGroupBlock->Init = BlockGroup_Init;
- ptrGroupBlock->Init(ptrGroupBlock);
- ptrThis->ptrBlockGroupHeader = ptrGroupBlock;
- ptrThis->ptrBlockGroupTail = ptrGroupBlock;
- ptrBlockReturn = ptrGroupBlock->GetFreeBlock(ptrGroupBlock);
- }
- else
- {
- BlockGroup *ptrBlockGroup = ptrThis->ptrBlockGroupHeader;
- do
- {
- ptrBlockReturn = ptrBlockGroup->GetFreeBlock(ptrBlockGroup);
- if (NULL != ptrBlockReturn)
- {
- break;
- }
- else
- {
- ptrBlockGroup = ptrBlockGroup->ptrBlockGroupNext;
- }
- } while (NULL != ptrBlockGroup);
- ///此时找遍所有的链表都没有找到空闲空间,则进行动态分配
- if(NULL == ptrBlockReturn)
- {
- BlockGroup *ptrBlockGroup = (BlockGroup *)malloc(sizeof(BlockGroup));
- ptrBlockGroup->Init = BlockGroup_Init;
- ptrBlockGroup->Init(ptrBlockGroup);
- ptrThis->ptrBlockGroupTail->ptrBlockGroupNext = ptrBlockGroup;
- ptrBlockGroup->ptrBlockGroupPre = ptrThis->ptrBlockGroupTail;
- ptrThis->ptrBlockGroupTail = ptrBlockGroup;
- ptrBlockReturn = ptrBlockGroup->GetFreeBlock(ptrBlockGroup);
- }
- }
- return ptrBlockReturn;
- }
- void log4c_block_begin(const char * fileName, int lineNum, const char * traceName)
- {
- TASK_ID threadId = VOS_GetSelfTaskID();
- int i = 0;
- Block *ptrBlock = NULL;
- PAssert_Return(fileName);
- PAssert_Return(traceName);
- ptrBlock = g_blockManager.GetThreadBlock(&g_blockManager, threadId);
- if (NULL != ptrBlock)
- {
- int iBlockLevel = 0;
- char strMsgBuffer[LOG4C_BUFFER_SIZE_MAX] = {0};
- if (ptrBlock->GetUseStatus(ptrBlock) == BLOCK_FREE)
- {
- ptrBlock->SetUseStatus(ptrBlock, BLOCK_BUSY);
- ptrBlock->SetThreadId(ptrBlock, threadId);
- ptrBlock->SetBlockLevel(ptrBlock, 0);
- }
- if (ptrBlock->GetThreadId(ptrBlock) != threadId)
- {
- ptrBlock->SetThreadId(ptrBlock, threadId);
- ptrBlock->SetBlockLevel(ptrBlock, 0);
- }
- ///ptrBlock->SetThreadId(ptrBlock, threadId);
- ///ptrBlock->SetBlockLevel(ptrBlock, 0);
- iBlockLevel = ++(ptrBlock->iBlockLevel) ;
- strncat_s(strMsgBuffer, LOG4C_BUFFER_SIZE_MAX, "B-Entry\t", sizeof(strMsgBuffer));
- for (i = 0; i < iBlockLevel; i++)
- {
- strncat_s(strMsgBuffer, LOG4C_BUFFER_SIZE_MAX,"==", sizeof(strMsgBuffer));
- }
- strncat_s(strMsgBuffer, LOG4C_BUFFER_SIZE_MAX, "> ", sizeof(strMsgBuffer));
- strncat_s(strMsgBuffer, LOG4C_BUFFER_SIZE_MAX, traceName, sizeof(strMsgBuffer));
- ///调用log4c的函数来记录日志
- log_msg(fileName, lineNum, "", LOG4C_PRIORITY_TRACE, strMsgBuffer);
- }
- }
- void log4c_block_end(const char * fileName, int lineNum, const char * traceName)
- {
- TASK_ID threadId = VOS_GetSelfTaskID();
- int i = 0;
- Block *ptrBlock = NULL;
- PAssert_Return(fileName);
- PAssert_Return(traceName);
- ptrBlock = g_blockManager.GetThreadBlock(&g_blockManager, threadId);
- if (NULL != ptrBlock)
- {
- int iBlockLevel = ptrBlock->GetBlockLevel(ptrBlock);
- char strMsgBuffer[LOG4C_BUFFER_SIZE_MAX] = {0};
- strncat_s(strMsgBuffer, LOG4C_BUFFER_SIZE_MAX, "B-Exit\t", sizeof(strMsgBuffer));
- strncat_s(strMsgBuffer, LOG4C_BUFFER_SIZE_MAX, "<", sizeof(strMsgBuffer));
- for (i = 0; i < iBlockLevel; i++)
- {
- strncat_s(strMsgBuffer, LOG4C_BUFFER_SIZE_MAX, "==", sizeof(strMsgBuffer));
- }
- strncat_s(strMsgBuffer, LOG4C_BUFFER_SIZE_MAX, " ", sizeof(strMsgBuffer));
- strncat_s(strMsgBuffer, LOG4C_BUFFER_SIZE_MAX, traceName, sizeof(strMsgBuffer));
- ///调用log4c的函数来记录日志
- log_msg(fileName, lineNum, "", LOG4C_PRIORITY_TRACE, strMsgBuffer);
- iBlockLevel = --ptrBlock->iBlockLevel ;
- if (ptrBlock->iBlockLevel == 0)
- {
- ptrBlock->SetThreadId(ptrBlock, 0);
- ptrBlock->SetBlockLevel(ptrBlock, 0);
- ptrBlock->SetUseStatus(ptrBlock, BLOCK_FREE);
- }
- }
- }
- /************************************************************************/
- /************** End of log.c *******************************************/
|