#include "commLOG.h" //日志的编号 int giInfoNumb = 1; int debugInfo(char *pLogPath, char *pFile, const char *pFuncName, int iLineNumb, char *fmt, ...) { if(NULL == pLogPath ||'\0' == pLogPath[0] || NULL == pFile || '\0' == pFile[0] || NULL == pFuncName ||'\0' == pFuncName[0]){ printf("debugInfo param err:%s,%s,%s,%d\r\n", pLogPath, pFile, pFuncName, iLineNumb); return LOG_VS_ERR; } //判断文件大小是否清空该文件,每1000次写日志里检测1次文件大小 if(0 == (giInfoNumb % 10)) { struct stat fileStat; if((giInfoNumb >= LOG_LINE_MAX) || ((0 == stat(pLogPath, &fileStat)) && (fileStat.st_size > LOG_SIZE_MAX))){ printf("\ndebugInfo log too big,%dline,%dbyte\n", giInfoNumb, (int)fileStat.st_size); remove(pLogPath); giInfoNumb = 0; } } //写入日期、函数信息 time_t timeSecs = time(NULL); struct tm *timeInfo = localtime(&timeSecs); char acTitle[LOG_STR_LEN_1024] = { 0 }; char debugMsg[LOG_STR_LEN_512] = { 0 }; va_list args; va_start(args, fmt); vsnprintf(debugMsg, sizeof(debugMsg), fmt, args); va_end(args); #if 0 snprintf(acTitle, sizeof(acTitle), "[%04d] [%d%02d%02d/%02d:%02d:%02d] [%s] [%s:%d] %s\n", giInfoNumb++, timeInfo->tm_year + 1900, timeInfo->tm_mon + 1, timeInfo->tm_mday, timeInfo->tm_hour, timeInfo->tm_min, timeInfo->tm_sec, pFile, pFuncName, iLineNumb, acTitle); #else //节约空间 snprintf(acTitle, sizeof(acTitle), "%02d %02d/%02d:%02d:%02d %s\n", giInfoNumb++, timeInfo->tm_mday,timeInfo->tm_hour, timeInfo->tm_min, timeInfo->tm_sec, debugMsg); #endif //调试打印 printf(acTitle); //打开文件,写入日志 FILE *pLogHandle = fopen(pLogPath, "a+"); if(NULL == pLogHandle){ printf("ERROR,debugInfo:fopen=%s\n",strerror(errno)); return LOG_VS_ERR; } int iLen = strlen(acTitle); fwrite(acTitle, iLen, 1, pLogHandle); fclose(pLogHandle); return LOG_VS_OK; }