📄 cutil-log.c
字号:
#include "cutil.h"#define MAX_FILENAME_LEN 1024static pthread_mutex_t gstruDbgMutex = PTHREAD_MUTEX_INITIALIZER;static pthread_mutex_t gstruErrMutex = PTHREAD_MUTEX_INITIALIZER;static pthread_mutex_t gstruTraMutex = PTHREAD_MUTEX_INITIALIZER;static int gbIfDebug = TRUE;static FILE * get_debug_file(struct tm *pstruTmNow){ static FILE *gpstruDbgFile = NULL; static char gszDbgFileName[MAX_FILENAME_LEN]; char szFileName[MAX_FILENAME_LEN]; memset(szFileName, 0, sizeof(szFileName)); snprintf(szFileName, sizeof(szFileName), "%s/%s-%04d%02d%02d.dbg", getenv("LOG_DIR") ? getenv("LOG_DIR") : getenv("HOME"), program_invocation_short_name, pstruTmNow -> tm_year + 1900, pstruTmNow -> tm_mon + 1, pstruTmNow -> tm_mday); if((access(szFileName, F_OK|W_OK) != 0) || (strcmp(szFileName, gszDbgFileName) != 0) || (gpstruDbgFile == NULL)) { strcpy(gszDbgFileName, szFileName); if(gpstruDbgFile) fclose(gpstruDbgFile); gpstruDbgFile = fopen(gszDbgFileName, "a"); if(gpstruDbgFile == NULL) { fprintf(stderr, "******[%d-%d-%d][%d:%d:%d][%d]create logfile '%s' error '%s'\n", pstruTmNow -> tm_year + 1900, pstruTmNow -> tm_mon + 1, pstruTmNow -> tm_mday, pstruTmNow -> tm_hour, pstruTmNow -> tm_min, pstruTmNow -> tm_sec, getpid(), gszDbgFileName, strerror(errno)); return NULL; } chmod(gszDbgFileName, S_IRUSR|S_IWUSR); } return gpstruDbgFile;}static FILE * get_error_file(struct tm *pstruTmNow){ static FILE *gpstruErrFile = NULL; static char gszErrFileName[MAX_FILENAME_LEN]; char szFileName[MAX_FILENAME_LEN]; memset(szFileName, 0, sizeof(szFileName)); snprintf(szFileName, sizeof(szFileName), "%s/%s-%04d%02d%02d.err", getenv("LOG_DIR") ? getenv("LOG_DIR") : getenv("HOME"), program_invocation_short_name, pstruTmNow -> tm_year + 1900, pstruTmNow -> tm_mon + 1, pstruTmNow -> tm_mday); if((access(szFileName, F_OK|W_OK) != 0) || (strcmp(szFileName, gszErrFileName) != 0) || (gpstruErrFile == NULL)) { strcpy(gszErrFileName, szFileName); if(gpstruErrFile) fclose(gpstruErrFile); gpstruErrFile = fopen(gszErrFileName, "a"); if(gpstruErrFile == NULL) { fprintf(stderr, "******[%d-%d-%d][%d:%d:%d][%d]create logfile '%s' error '%s'\n", pstruTmNow -> tm_year + 1900, pstruTmNow -> tm_mon + 1, pstruTmNow -> tm_mday, pstruTmNow -> tm_hour, pstruTmNow -> tm_min, pstruTmNow -> tm_sec, getpid(), gszErrFileName, strerror(errno)); return NULL; } chmod(gszErrFileName, S_IRUSR|S_IWUSR); } return gpstruErrFile;}static FILE * get_trans_file(struct tm *pstruTmNow){ static FILE *gpstrutraFile = NULL; static char gszTraFileName[MAX_FILENAME_LEN]; char szFileName[MAX_FILENAME_LEN]; memset(szFileName, 0, sizeof(szFileName)); snprintf(szFileName, sizeof(szFileName), "%s/%s-%04d%02d%02d.tra", getenv("LOG_DIR") ? getenv("LOG_DIR") : getenv("HOME"), program_invocation_short_name, pstruTmNow -> tm_year + 1900, pstruTmNow -> tm_mon + 1, pstruTmNow -> tm_mday); if((access(szFileName, F_OK|W_OK) != 0) || (strcmp(szFileName, gszTraFileName) != 0) || (gpstrutraFile == NULL)) { strcpy(gszTraFileName, szFileName); if(gpstrutraFile) fclose(gpstrutraFile); gpstrutraFile = fopen(gszTraFileName, "a"); if(gpstrutraFile == NULL) { fprintf(stderr, "******[%d-%d-%d][%d:%d:%d][%d]create logfile '%s' error '%s'\n", pstruTmNow -> tm_year + 1900, pstruTmNow -> tm_mon + 1, pstruTmNow -> tm_mday, pstruTmNow -> tm_hour, pstruTmNow -> tm_min, pstruTmNow -> tm_sec, getpid(), gszTraFileName, strerror(errno)); return NULL; } chmod(gszTraFileName, S_IRUSR|S_IWUSR); } return gpstrutraFile;}/** * cutil_log_set_debug: * @bIfDebug: TRUE输出调试日志 FALSE不输出调试日志 * * 是否要求输出调试日志 * */void cutil_log_set_debug(int bIfDebug){ gbIfDebug = bIfDebug;}/** * cutil_log_print: * @nLogLevel:日志的等级. * @pszFile:__FILE__宏 * @nLine: __LINE__宏 * @pszFmt: 日志文件格式. * @...: 变参数 * * 记录信息到日志文件.调试日志的后缀为dbg,错误日志的后缀为err * 调试日志的后缀为tra.日志文件的目录为环境变量LOG_DIR指定 * 如果没有环境变量则为HOME目录.日志文件的名称为程序名称 * */void cutil_log_print(int nLogLevel, char *pszFile, int nLine, char *pszFmt, ...){ struct tm struTmNow; time_t struTimeNow; va_list Arg; FILE *pstruFile = NULL; time(&struTimeNow); localtime_r(&struTimeNow, &struTmNow); switch (nLogLevel) { case CUTIL_LOG_DEBUG: if(!gbIfDebug) return ; pthread_mutex_lock(&gstruDbgMutex); pstruFile = get_debug_file(&struTmNow); break; case CUTIL_LOG_ERROR: pthread_mutex_lock(&gstruErrMutex); pstruFile = get_error_file(&struTmNow); break; case CUTIL_LOG_TRANS: pthread_mutex_lock(&gstruTraMutex); pstruFile = get_trans_file(&struTmNow); break; } if(pstruFile) { fprintf(pstruFile, "[%d-%d-%d][%d:%d:%d][%s][%d][%d]\n\t", struTmNow.tm_year + 1900, struTmNow.tm_mon + 1, struTmNow.tm_mday, struTmNow.tm_hour, struTmNow.tm_min, struTmNow.tm_sec, pszFile, nLine, getpid()); va_start(Arg, pszFmt); vfprintf(pstruFile, pszFmt, Arg); va_end(Arg); fflush(pstruFile); } switch (nLogLevel) { case CUTIL_LOG_DEBUG: pthread_mutex_unlock(&gstruDbgMutex); break; case CUTIL_LOG_ERROR: pthread_mutex_unlock(&gstruErrMutex); break; case CUTIL_LOG_TRANS: pthread_mutex_unlock(&gstruTraMutex); break; }}/** * cutil_log_print_hex: * @nLogLevel:日志的等级. * @pszFile:__FILE__宏 * @nLine: __LINE__宏 * @pszBuffer: 要记录的数据 * @nLen: 数据的长度 * * 以十六进制记录信息到日志文件 * */void cutil_log_print_hex(int nLogLevel, char *pszFile, int nLine, char *pszBuffer, int nLen){ register int i,j; register int nRowNo; unsigned char cTemp; struct tm struTmNow; time_t struTimeNow; FILE *pstruFile = NULL; time(&struTimeNow); localtime_r(&struTimeNow, &struTmNow); switch (nLogLevel) { case CUTIL_LOG_DEBUG: if(!gbIfDebug) return ; pthread_mutex_lock(&gstruDbgMutex); pstruFile = get_debug_file(&struTmNow); break; case CUTIL_LOG_ERROR: pthread_mutex_lock(&gstruErrMutex); pstruFile = get_error_file(&struTmNow); break; case CUTIL_LOG_TRANS: pthread_mutex_lock(&gstruTraMutex); pstruFile = get_trans_file(&struTmNow); break; } if(pstruFile) { fprintf(pstruFile, "[%d-%d-%d][%d:%d:%d][%s][%d][%d]\n", struTmNow.tm_year + 1900, struTmNow.tm_mon + 1, struTmNow.tm_mday, struTmNow.tm_hour, struTmNow.tm_min, struTmNow.tm_sec, pszFile, nLine, getpid()); nRowNo = nLen / 16; for(i = 0; i < nRowNo; i ++) { fprintf(pstruFile, "%08X | ", i * 16); for(j = 0; j < 16; j ++) { fprintf(pstruFile, "%02X ", \ *(unsigned char *)(pszBuffer + i * 16 + j)); } fprintf(pstruFile,"| "); for(j = 0; j < 16; j ++) { cTemp = *(unsigned char *)(pszBuffer + i * 16 + j); cTemp = cTemp > 32 ? cTemp : '.'; fprintf(pstruFile, "%c", cTemp); } fprintf(pstruFile, "\n"); } nRowNo = nLen % 16; if(nRowNo) { fprintf(pstruFile, "%08X | ", i * 16); for(j = 0; j < nRowNo; j ++) { fprintf(pstruFile, "%02X ", \ *(unsigned char *)(pszBuffer + i * 16 + j)); } while(j ++ < 16) fprintf(pstruFile, " "); fprintf(pstruFile, "| "); for(j = 0; j < nRowNo; j ++) { cTemp = *(unsigned char *)(pszBuffer + i * 16 + j); cTemp = cTemp > 32 ? cTemp : '.'; fprintf(pstruFile, "%c", cTemp); fflush(pstruFile); } while(j ++ < 16) fprintf(pstruFile, " "); fprintf(pstruFile, "\n"); } fflush(pstruFile); } switch(nLogLevel) { case CUTIL_LOG_DEBUG: pthread_mutex_unlock(&gstruDbgMutex); break; case CUTIL_LOG_ERROR: pthread_mutex_unlock(&gstruErrMutex); break; case CUTIL_LOG_TRANS: pthread_mutex_unlock(&gstruTraMutex); break; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -