📄 log.c
字号:
#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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -