⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 log.c

📁 用c/c++实现的一个CMPP API
💻 C
字号:
/************************************************************************  Copyright    : 2001-2002, ASPIRE TECHNOLOGIES (SHENZHEN) LTD.  File name    : log.c  Description  : 简单日志处理  Others       :  History      : Author        Date         Version       Description                 =====================================================                 sunjun     2002-11-26       1.0           create                 wenyz      2002-12-11       增加通过配置来开关日志功能************************************************************************/#include <stdio.h>#include <stdlib.h>#include <string.h>#include <stdarg.h>#include <sys/types.h>#include <sys/timeb.h>#include <time.h>#include "os.h"#include "cmppthread.h"#include "cfgs.h"//#include "cmppthread.h"FILE *g_fplog = NULL;     /* log file point *///add by houzhstatic time_t timetLastTime =0;const char* sDefaultIniFile = "cmppapi.ini";    /* 缺省的配置文件 */static  int g_nEnableLog = -1;                  /* 是否打开日志 */char  g_sFileName[256];int   g_nLineNum = 0;const char* sDefaultLogFile = "cmppapi.log";     /*缺省的日志文件名*//************************************************************************ * Function ID: vPrintBin                     * Description:  打印二进制信息 * Input Param:  const char *sContent  打印的内容, int nLength   :内容的长度                          * Output Param:  char 组织后的信息 * Return:    无             ************************************************************************/void  vPrintBin(const char *sContent, int nLength,char *pValue){    char     sTemp[1024]="";       /* 临时字符串 */    char     sShortTemp[6];     /* 短串,一个字符 */    int         i;	    /* 清打印字串 */    *sTemp = 0;        for( i = 0; i < nLength; i++ )    {        if (i % 16 == 0)        {            sprintf(sShortTemp, "\n%04X\t", i);            strcat(sTemp, sShortTemp);            /* 清打印字串 */           // *sTemp = 0;        }        sprintf(sShortTemp, "%02X ",(unsigned char)sContent[i] & 0xFF);        strcat(sTemp, sShortTemp);    }    strcat(sTemp, "\n\n");    /* 打印最后一行 */    memcpy(pValue , sTemp,1024);}/************************************************************************ * Function ID: vGetTimeofNow                     * Description: 获取系统当前时间 * Input Param: 无                             * Output Param:  char *stimeofnow    当前时间YYYY-MM-DD hh:mm:ss * Return:    无             ************************************************************************/void vGetTimeofNow( char *stimeofnow ){    struct tm *ptmNow;    time_t time_tNow = 0;    struct timeb    rtimeb;        if ( stimeofnow == NULL )    {         return;    }    time( &time_tNow );    ptmNow = localtime( &time_tNow );    strftime( stimeofnow, 20, "%Y-%m-%d %H:%M:%S", ptmNow);        ftime( &rtimeb );    sprintf( stimeofnow+19, ":%d", rtimeb.millitm );}/************************************************************************ * Function ID: nInitLog                     * Description: 日志记录初始化 * Input Param:  char *slogfile    log文件名,当为NULL时,取缺省值 * Output Param: 无                             * Return:    无             ************************************************************************/int  nCmppInitLog( char *slogfile ){    char    sTmpStr[ 8 ];    if ( g_fplog != NULL )    {       fclose( g_fplog );       g_fplog = NULL;    }    if( g_nEnableLog != -1 )    return 0;    //add by houzh	time(&timetLastTime);        /*    *读取配置文件    */    memset( sTmpStr, 0, sizeof( sTmpStr ) );    if ( NULL != sCfgsReadString( (char*)sDefaultIniFile, "SP",                                 "Enable_Log", sTmpStr, 1+1 ) )    {    /* 是否打开日志 */         g_nEnableLog = atoi(sTmpStr);            }    else    {        /* 不打开日志 */         g_nEnableLog = 0;    }    if(g_nEnableLog == 0)    {        return 0;    }		    //printf( "g_nEnableLog = %d\n", g_nEnableLog );    //printf( "in open log file \n" );    if ( slogfile == NULL )     {        g_fplog = fopen( sDefaultLogFile, "a");    }    else    {        g_fplog = fopen( slogfile, "a");    }    if ( g_fplog == NULL )     {        return -1;    }    return 0;}/************************************************************************ * Function ID: vTrace                     * Description: 记录跟踪信息 * Input Param: 文件名 行数 跟踪信息                     * Return:    无             ************************************************************************/void vTrace( const char * format, ... ){      va_list args;    char *p;    char  sTempFormat[4000];  /* 临时Format串 */    int   flag = 0;           /* 是否补充"\n"的标志 */		static time_t timetCurrentTime = 0;    if ( g_nLineNum == 0 )  /* 一般不允许直接调用本函数,必须通过宏调用 */    {       return;    }        if (g_fplog == NULL )    {	          nCmppInitLog( NULL );    }	    time(&timetCurrentTime);	if(timetCurrentTime - timetLastTime > 60)	{		g_nEnableLog = -1;		nCmppInitLog( NULL);	}    if( g_nEnableLog == 0 )    // 如果没有配置开日志    {		        return ;    }	if (g_fplog == NULL )	{		return;	}    vGetTimeofNow( sTempFormat );    fprintf( g_fplog, "%s %s L%d: %d ", sTempFormat,g_sFileName, g_nLineNum ,nAPIGetpthreadId());    va_start(args, format);    /* 如果格式串中末尾没有"\n",程序自动添加一个 */    memset(sTempFormat, '\0', sizeof(sTempFormat));    strcpy(sTempFormat, format);    for( p = sTempFormat + strlen(format) - 1; p > sTempFormat; p--)    {        if(*p == '\n')        {            break;        }        else if (*p == ' ')        {            *p = '\0';            continue;        }        else        {            flag = 1;            break;        }    }    if(flag == 1)    {        strcat(sTempFormat, "\n");    }       /* 写详细信息 */    vfprintf(g_fplog, sTempFormat, args);    fflush( g_fplog );    va_end( args );}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -