一個寫log的函數 log_func.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <stdarg.h>

/***  user defined include file:   ***/

//#include <log_def.h>
#include "log_def.h"

extern int get_conf(char *name,char *value);

/******************************************************************************/
/* 模塊說明:公用函數集                                                       */
/* 包括函數:                                                                  */
/*     void get_date_time()      取操作系統的日期和時間                       */
/*     int write_log()           記正常、錯誤日誌或輸出到屏幕上               */
/******************************************************************************/

/******************************************************************************/
/* 函數名稱:void get_date_time()                                             */
/* 功    能:取操作系統的日期和時間                                           */
/* 輸入輸出:char *today    輸出參數,返回日期                                */
/*           char *now_time 輸出參數,返回時間                                */
                                                      */                                                                        */
/******************************************************************************/
void _get_date_time(char *today,char *now_time)
{
        struct tm *tp;
        time_t stClk;

        time(&stClk);
        tp=localtime(&stClk);
        sprintf(today,"%4d%02d%02d",tp->tm_year+1900,tp->tm_mon+1,tp->tm_mday);
        *(today+8)='/0';
        sprintf(now_time,"%02d%02d%02d",tp->tm_hour,tp->tm_min,tp->tm_sec);
        *(now_time+6)='/0';
}

/******************************************************************************/
/* 函數名稱:int write_log()                                                  */
/* 功    能:記日誌或輸出到屏幕上                                             */
/* 輸入輸出:int iLog_flag  輸入參數,記日誌標誌                              */
/*                  1: 正常日誌,-1:錯誤日誌,0:屏幕輸出                    */
/*           char *pFormat  輸入參數,格式輸入參數                            */
/* 函數返回:0:正常    < 0:出錯                                               */
/* 調用函數:_get_date_time                                                   */
/* 環境變量:RUN_HOME                                                         */
                              
/******************************************************************************/
int write_log(int iLog_flag,const char *pFormat, ...)
{
        va_list   ap_list;
        FILE      *fp;
        char      chToday[9],chNow_time[7],chFilename[80];
        char      pchFilepath[60],chMvname[80];
        struct stat stFilestat;

        _get_date_time(chToday, chNow_time);
        memset(chFilename,0x00,sizeof(chFilename));
//      get_conf("SYSLOG",pchFilepath);
        sprintf(pchFilepath,"/root/test");

        switch(iLog_flag){
                case NORMAL_LOG:
                        sprintf(chFilename,"%s/log%2.2s.dat",pchFilepath,chToday+6);
                        sprintf(chMvname,"%s/log%2.2s.bak",pchFilepath,chToday+6);
                        break;
                case ERROR_LOG:
                        sprintf(chFilename,"%s/err%s.dat",pchFilepath,chToday);
                //      sprintf(chFilename,"%s/err%2.2s.dat",pchFilepath,chToday);
                        sprintf(chMvname,"%s/err%2.2s.bak",pchFilepath,chToday+6);
                        break;
                case SCROUT_LOG:   /* 屏幕輸出 */
                        break;
                default:
                        return(ERROR_PARAM_ERR);
        }

        /* 控制錯誤日誌文件,跟蹤日誌文件大小爲 1M */
        if(iLog_flag == NORMAL_LOG || iLog_flag == ERROR_LOG){
                if(stat(chFilename,&stFilestat) == 0){  /* 文件已存在 */
                        if(stFilestat.st_size >= 1024*1024){ 
                                rename(chFilename,chMvname);
                        }
                }
        }

        if(iLog_flag != SCROUT_LOG){
                fp = fopen(chFilename,"a");
                if(fp == (FILE *)NULL){
                        write_log(SCROUT_LOG,"open file [%s] error!/n",chFilename);
                        perror("open file");
                        return(ERROR_OPEN_FILE_ERR);
                }
        }

        va_start(ap_list, pFormat);
        if(iLog_flag == SCROUT_LOG){
                vfprintf(stdout, pFormat, ap_list);
        }
        else{
                fprintf(fp,"[%2.2s-%2.2s-%2.2s|%2.2s:%2.2s:%2.2s]: ", /
                     chToday+2,chToday+4,chToday+6,chNow_time,chNow_time+2,chNow_time+4);
                vfprintf(fp, pFormat, ap_list);
                fprintf(fp,"/n");       /* 換行 */
                fclose(fp);
        }
        va_end(ap_list);
        return(0);
}

發佈了18 篇原創文章 · 獲贊 1 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章