欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

用C语言打印日志(Log)

程序员文章站 2022-06-24 20:52:47
用c语言打印日志(log) 直接上源代码。 log.h 文件: /** log.h **/ #ifndef __log_h__ #define __log_h__ #i...

用c语言打印日志(log)

直接上源代码。

log.h 文件:

/** log.h **/  
  
#ifndef __log_h__  
#define __log_h__  
#include "stdio.h"  
#include "string.h"  
#include "stdlib.h"  
#include "time.h"  
#include "stdarg.h"  
#include "unistd.h"  
  
#define maxlen (2048)  
#define maxfilepath (512)  
#define maxfilename (50)  
typedef enum{  
    error_1=-1,  
    error_2=-2,  
    error_3=-3  
}error0;  
  
  
typedef enum{  
    none=0,  
    info=1,  
    debug=2,  
    warn=3,  
    error=4,  
    all=255  
}loglevel;  
  
typedef struct log{  
    char logtime[20];  
    char filepath[maxfilepath];  
    file *logfile;  
}log;  
  
typedef struct logseting{  
    char filepath[maxfilepath];  
    unsigned int maxfilelen;  
    unsigned char loglevel;  
}logset;  
  
int logwrite(unsigned char loglevel,char *fromat,...);  
#endif /* __log_h__ */ 

log.c 文件:

/** log.c **/  
  
#include "log.h"  
#define maxlevelnum (3)  
  
logset logsetting;  
log loging;  
  
const static char logleveltext[4][10]={"info","debug","warn","error"};  
  
static char * getdate(char *date);  
  
static unsigned char getcode(char *path){  
    unsigned char code=255;  
    if(strcmp("info",path)==0)  
        code=1;  
    else if(strcmp("warn",path)==0)  
        code=3;  
    else if(strcmp("error",path)==0)  
        code=4;  
    else if(strcmp("none",path)==0)  
        code=0;  
    else if(strcmp("debug",path)==0)  
        code=2;  
    return code;  
}  
  
static unsigned char readconfig(char *path){  
    char value[512]={0x0};  
    char data[50]={0x0};  
  
    file *fpath=fopen(path,"r");  
    if(fpath==null)  
        return -1;  
    fscanf(fpath,"path=%s\n",value);  
    getdate(data);  
    strcat(data,".log");  
    strcat(value,"/");  
    strcat(value,data);  
    if(strcmp(value,logsetting.filepath)!=0)  
        memcpy(logsetting.filepath,value,strlen(value));  
    memset(value,0,sizeof(value));  
  
    fscanf(fpath,"level=%s\n",value);  
    logsetting.loglevel=getcode(value);  
    fclose(fpath);  
    return 0;  
}  
/* 
 *日志设置信息 
 * */  
static logset *getlogset(){  
    char path[512]={0x0};  
    getcwd(path,sizeof(path));  
    strcat(path,"/log.conf");  
    if(access(path,f_ok)==0){  
        if(readconfig(path)!=0){  
            logsetting.loglevel=info;  
            logsetting.maxfilelen=4096;  
        }  
    }else{  
        logsetting.loglevel=info;  
        logsetting.maxfilelen=4096;  
    }  
    return &logsetting;  
}  
  
/* 
 *获取日期 
 * */  
static char * getdate(char *date){  
    time_t timer=time(null);  
    strftime(date,11,"%y-%m-%d",localtime(&timer));  
    return date;  
}  
  
/* 
 *获取时间 
 * */  
static void settime(){  
    time_t timer=time(null);  
    strftime(loging.logtime,20,"%y-%m-%d %h:%m:%s",localtime(&timer));  
}  
  
/* 
 *不定参打印 
 * */  
static void printflog(char * fromat,va_list args){  
    int d;  
    char c,*s;  
    while(*fromat)  
    {  
        switch(*fromat){  
            case 's':{  
                s = va_arg(args, char *);  
                fprintf(loging.logfile,"%s",s);  
                break;}  
            case 'd':{  
                d = va_arg(args, int);  
                fprintf(loging.logfile,"%d",d);  
                break;}  
            case 'c':{  
                c = (char)va_arg(args, int);  
                fprintf(loging.logfile,"%c",c);  
                break;}  
            default:{  
                if(*fromat!='%'&&*fromat!='\n')  
                    fprintf(loging.logfile,"%c",*fromat);  
                break;}  
        }  
        fromat++;  
    }  
    fprintf(loging.logfile,"%s","]\n");  
}  
  
static int initlog(unsigned char loglevel){  
    char strdate[30]={0x0};  
    logset *logsetting;  
    //获取日志配置信息  
    if((logsetting=getlogset())==null){  
        perror("get log set fail!");  
        return -1;  
    }  
    if((loglevel&(logsetting->loglevel))!=loglevel)  
        return -1;  
  
    memset(&loging,0,sizeof(log));  
    //获取日志时间  
    settime();  
    if(strlen(logsetting->filepath)==0){  
        char *path=getenv("home");  
        memcpy(logsetting->filepath,path,strlen(path));  
  
        getdate(strdate);  
        strcat(strdate,".log");  
        strcat(logsetting->filepath,"/");  
        strcat(logsetting->filepath,strdate);  
    }  
    memcpy(loging.filepath,logsetting->filepath,maxfilepath);  
    //打开日志文件  
    if(loging.logfile==null)  
        loging.logfile=fopen(loging.filepath,"a+");  
    if(loging.logfile==null){  
        perror("open log file fail!");  
        return -1;  
    }  
    //写入日志级别,日志时间  
    fprintf(loging.logfile,"[%s] [%s]:[",logleveltext[loglevel-1],loging.logtime);  
    return 0;  
}  
  
/* 
 *日志写入 
 * */  
int logwrite(unsigned char loglevel,char *fromat,...){  
    va_list args;  
    //初始化日志  
    if(initlog(loglevel)!=0)  
        return -1;  
    //打印日志信息  
    va_start(args,fromat);  
    printflog(fromat,args);  
    va_end(args);  
    //文件刷出  
    fflush(loging.logfile);  
    //日志关闭  
    if(loging.logfile!=null)  
        fclose(loging.logfile);  
    loging.logfile=null;  
    return 0;  
}

test.c 文件:

/** test.c **/  
  
#include "stdio.h"  
#include "stdlib.h"  
#include "log.h"  
int main(int argv,char**argc){  
    printf("%s\n",argc[0]);  
    logwrite(info,"%s","hello world!");  
    logwrite(debug,"%s","h.e.l.l.o w.o.r.l.d!");  
    logwrite(warn,"%s","h e l l o w o r l d!");  
    logwrite(error,"%s","hallo world!");  
    return 0;  
} 

log.conf 文件:

path=./temp  
level=all