www.pudn.com > PEMonitor_0.10_src.zip > LogServ.c


/////////////////////////////////////////////////////////////////////////////// 
// 
//  FileName    :   LogServ.c 
//  Version     :   1.0 
//  Author      :   Luo Cong 
//  Date        :   2004-09-02 9:59:19 
//  Comment     :    
// 
/////////////////////////////////////////////////////////////////////////////// 
 
#include  
#include  
#include "LogServ.h" 
#include "Misc.h" 
 
char g_szLogContent[LOG_MAXLENGTH]; 
char g_szLogFileName[MAX_PATH]; 
 
int WriteToLog( 
    /* [in] */ const int nLen 
) 
{ 
    int nRetResult = 0; 
    FILE *fp_out = NULL; 
 
    fp_out = fopen(g_szLogFileName, "w"); 
    MY_PROCESS_ERROR_WITH_MSG1(fp_out, ErrMsg[ERR_OPEN_LOG_FILE]); 
 
    fwrite(g_szLogContent, 1, nLen, fp_out); 
 
    nRetResult = 1; 
Exit0: 
    if (fp_out) 
    { 
        fclose(fp_out); 
        fp_out = NULL; 
    } 
    return nRetResult; 
} 
 
int TruncateLogBuff(void) 
{ 
    int nRetResult = 0; 
    int nRetCode; 
    int nLen = 0; 
 
    nLen = strlen(g_szLogContent); 
    if (nLen >= LOG_TRUNCATELEN) 
    { 
        nRetCode = WriteToLog(LOG_TRUNCATELEN); 
        MY_PROCESS_ERROR(nRetCode); 
        strcpy(g_szLogContent, g_szLogContent + LOG_TRUNCATELEN); 
    } 
 
    nRetResult = 1; 
Exit0: 
    return nRetResult; 
} 
 
int AddToLogTail( 
    /* [in] */ const char *szLogContent 
) 
{ 
    int nRetResult = 0; 
    int nRetCode; 
 
    strcat(g_szLogContent, szLogContent); 
    nRetCode = TruncateLogBuff(); 
    MY_PROCESS_ERROR(nRetCode); 
 
    nRetResult = 1; 
Exit0: 
    return nRetResult; 
} 
 
int InitLogFile( 
    /* [in] */ const char *szVirusFileName 
) 
{ 
    int nRetResult = 0; 
 
    int nPos; 
    char *pDest = NULL; 
 
    pDest = strrchr(szVirusFileName, '.'); 
    if (!pDest) 
        goto Exit0; 
 
    nPos = pDest - szVirusFileName; 
    strcpy(g_szLogFileName, szVirusFileName); 
    strcpy(&g_szLogFileName[nPos], ".log"); 
 
    nRetResult = 1; 
Exit0: 
    return nRetResult; 
} 
 
int FinalizeLogFile() 
{ 
    int nRetResult = 0; 
    int nRetCode; 
    int nLen = 0; 
 
    nLen = strlen(g_szLogContent); 
    if (0 != nLen) 
    { 
        nRetCode = WriteToLog(nLen); 
        MY_PROCESS_ERROR(nRetCode); 
    } 
 
    nRetResult = 1; 
Exit0: 
    return nRetResult; 
}