日志程序

头文件
#ifndef __xlog_h
#define __xlog_h



#define DEFAULT_CONFIG "."
#define PRIORITY_NUM 5
#define P_FATAL  1
#define P_ERROR  2
#define P_WARN  3
#define P_INFO  4
#define P_DEBUG  5

void xlog_out(char *subject,int priority_level,char *fmt,...);

#endif
------------------------------------------------------------------
源文件
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>

FILE * pLogFile;
time_t t;
struct tm *st;
//enum Priority {0,1,2,3,4};

//读取日志文件
FILE* OpenLog(char* pszLogFileName){

     if (!(pLogFile = fopen(pszLogFileName,"ab+"))){
         return NULL;
     }
     return pLogFile;
}

//关闭日志文件
int CloseLog(){
     int nErr = 0;
     if (nErr = fclose(pLogFile)) {
         return nErr;
     }
     return 0;
}

//写日志
//void WriteLog(int Priority,const char* Fmt){
void Test_LogRecordEX(){
   t=time(NULL);
   st=localtime(&t);
   fprintf(pLogFile,"%d-%d-%d %2d:%2d:%2d   ",st->tm_year+1900,st->tm_mon+1,st->tm_mday,st->tm_hour,st->tm_min,st->tm_sec);
   // 变量:__FILE__ 可以得到当前文件名 .  使用变量:__LINE__ 可以得到当前文件中执行到的行号
   fprintf(pLogFile,"File name = %s    Line number = %d\r\n",__FILE__,__LINE__);
   printf("File name = %s    Line number = %d \r\n",__FILE__,__LINE__);
}


int main()
{
char * pszLogFileName =  "file.log";
OpenLog(pszLogFileName);
Test_LogRecordEX();
CloseLog();
}

你可能感兴趣的:(日志)