- void fileLog(char *fn,char * formatStr,...)
- {
- FILE *file;
- time_t lcurtime = time(NULL);
- char lsBuf1[512]={0},lsBuf2[512]={0};
- va_list args;
- _mkdir("logs");
- va_start(args,formatStr);
- sprintf(lsBuf1,"./logs/%s",fn);
- strcat(lsBuf1,"%Y-%m-%d.txt");
- strftime(lsBuf2,sizeof(lsBuf2),lsBuf1,localtime(&lcurtime));
- if((file=fopen(lsBuf2,"a")))
- {
- strftime(lsBuf2,sizeof(lsBuf2),"%Y%m%d %H:%M:%S ",localtime(&lcurtime));
- fprintf(file,"%s ",lsBuf2);
- printf( lsBuf2 );
- vfprintf(file,formatStr,args);
- vprintf(formatStr,args);
- fprintf(file,"/n");
- printf("/n");
- fclose(file);
- }
- va_end(args);
- }
- 记得包含头文件
- #include <stdarg.h>
- #include <time.h>
- #include <string>
- #include <direct.h>
- 使用例子:
- fileLog("mylog","class:%s,line:%d error!","classA",__LINE__);
按日期分类的 log
最新推荐文章于 2024-01-17 17:56:46 发布