log.c

来自「公司业务而自己写的网关通讯程序,网关是平台的.不过对OCI有兴趣的不妨看看. O」· C语言 代码 · 共 55 行

C
55
字号
#include <stdio.h>#include "log.h"#include <time.h>#include <pthread.h>
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
char lastdate[20];FILE *pLogFile=NULL;void log(char* loglevel,char* logmsg,int toscreen){    struct tm *tm_ptr, timestruct;    time_t the_time;    char tmpbuf[1024];    char *result;    (void) time(&the_time);    tm_ptr = localtime(&the_time);    strftime(tmpbuf, 50, "%Y-%m-%d", tm_ptr);        strcat(tmpbuf,".");    strcat(tmpbuf,loglevel);
	pthread_mutex_lock(&mutex);	    if(!pLogFile || strcmp(lastdate,tmpbuf)!=0){			if(pLogFile){				fclose(pLogFile);			}			strcpy(lastdate,tmpbuf);			pLogFile = fopen(tmpbuf,"a");		}	    if(!pLogFile){	    	printf("can not open logfile:%s\n",tmpbuf);			pthread_mutex_unlock(&mutex);    		    	return;	    }		strftime( tmpbuf, 1024,			"[%Y-%m-%d %H:%M:%S] ", tm_ptr );		strcat(tmpbuf,logmsg);		strcat(tmpbuf,"\n");		if(toscreen)printf(tmpbuf);		fwrite(tmpbuf,sizeof(char),strlen(tmpbuf),pLogFile);		pthread_mutex_unlock(&mutex);	    return;}void closelog(){	if(!pLogFile){		fclose(pLogFile);	}	}

⌨️ 快捷键说明

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