贡献一下自己写的写日志程序
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <stdarg.h>#include <time.h> #include <sys/stat.h>#include <unistd.h>#define MAX 128 int log_level;void info(FILE *file, const char *format, ...);void init_logger(struct stat *s, FILE **f, const char *path, int level);int main(int argc, char *argv[]){ FILE *f; struct stat s; init_logger(&s, &f, "log", 4); info(f, "hello %s, year %d", "huzilong", 2012); return 0;} void init_logger(struct stat *s, FILE **f, const char *path, int level){//深度传形参f, f是一个临时指针变量 char temp[128] = {0}; time_t timer; struct tm *now; log_level = level; time(&timer); now = localtime(&timer); sprintf(temp, "%s/%d%d%d.log", path, now->tm_year + 1900, now->tm_mon + 1, now->tm_mday); if(-1 == stat(path, s)){ puts("[warn]文件夹不存在"); mkdir(path, 777); } if(!(*f = fopen(temp, "a+"))){ puts("[error]open log file failed"); exit(-1); } }void info(FILE *file, const char *format, ...){ int i, d; unsigned int u; void *p; char c, *s, src[MAX] = {'\0'};//顺序遍历format va_list argp = NULL; va_start(argp, format); for(i = 0; i < strlen(format) + 1; i++){ if(format[i] == '%'){ switch(format[i + 1]){ case 'd': d = va_arg(argp, int); sprintf(src, "%s%d", src, d); i = i + 1; break; case 'c': c = (char)va_arg(argp, int); sprintf(src, "%s%c", src, c); i = i + 1; break; case 's': s = va_arg(argp, char *); sprintf(src, "%s%s", src, s); i = i + 1; break; case 'u': u = va_arg(argp, unsigned int); sprintf(src, "%s%u", src, u); i = i + 1; break; case 'p': p = va_arg(argp, void *); sprintf(src, "%s%p", src, p); i = i + 1; break; default: break; } ++format; }else if('\0' == format[i]){ va_end(argp); break; }else{ sprintf(src, "%s%c", src, format[i]); } } switch(log_level){ case 8: case 4: case 2: case 1: fputs(src, file); fputs("\r\n", file);//Linux回车换行 \r\n fflush(file); default: break; }
#include <stdio.h>#include <stdlib.h>#include <string.h>#ifdef WIN32 #include <windows.h> #include <io.h>#else #include <unistd.h> #include <sys/time.h> #include <pthread.h> #define CRITICAL_SECTION pthread_mutex_t #define _vsnprintf vsnprintf#endif//Log{#define MAXLOGSIZE 100000000#define ARRSIZE(x) (sizeof(x)/sizeof(x[0]))#include <time.h>#include <sys/timeb.h>#include <stdarg.h>char logfilename1[]="MyLog1.log";char logfilename2[]="MyLog2.log";char logstr[16000];char datestr[16];char timestr[16];char mss[4];CRITICAL_SECTION cs_log;FILE *flog;#ifdef WIN32void Lock(CRITICAL_SECTION *l) { EnterCriticalSection(l);}void Unlock(CRITICAL_SECTION *l) { LeaveCriticalSection(l);}#elsevoid Lock(CRITICAL_SECTION *l) { pthread_mutex_lock(l);}void Unlock(CRITICAL_SECTION *l) { pthread_mutex_unlock(l);}#endifvoid LogV(const char *pszFmt,va_list argp) { struct tm *now; struct timeb tb; if (NULL==pszFmt||0==pszFmt[0]) return; if (-1==_vsnprintf(logstr,ARRSIZE(logstr),pszFmt,argp)) logstr[ARRSIZE(logstr)-1]=0; ftime(&tb); now=localtime(&tb.time); sprintf(datestr,"%04d-%02d-%02d",now->tm_year+1900,now->tm_mon+1,now->tm_mday); sprintf(timestr,"%02d:%02d:%02d",now->tm_hour ,now->tm_min ,now->tm_sec ); sprintf(mss,"%03d",tb.millitm); printf("%s %s.%s %s",datestr,timestr,mss,logstr); flog=fopen(logfilename1,"a"); if (NULL!=flog) { fprintf(flog,"%s %s.%s %s",datestr,timestr,mss,logstr); if (ftell(flog)>MAXLOGSIZE) { fclose(flog); if (rename(logfilename1,logfilename2)) { remove(logfilename2); rename(logfilename1,logfilename2); } flog=fopen(logfilename1,"a"); if (NULL==flog) return; } fclose(flog); }}void Log(const char *pszFmt,...) { va_list argp; Lock(&cs_log); va_start(argp,pszFmt); LogV(pszFmt,argp); va_end(argp); Unlock(&cs_log);}//Log}int main(int argc,char * argv[]) { int i;#ifdef WIN32 InitializeCriticalSection(&cs_log);#else pthread_mutex_init(&cs_log,NULL);#endif for (i=0;i<10000;i++) { Log("This is a Log %04d from FILE:%s LINE:%d\n",i, __FILE__, __LINE__); }#ifdef WIN32 DeleteCriticalSection(&cs_log);#else pthread_mutex_destroy(&cs_log);#endif return 0;}
[解决办法]
注释太少了点
[解决办法]
居然自己把那些参数实现了。。。
直接用vsnprintf不就完了?
[解决办法]
PK一下我这个?
#include <stdio.h>#include <stdlib.h>#include <string.h>#ifdef WIN32 #include <windows.h> #include <io.h>#else #include <unistd.h> #include <sys/time.h> #include <pthread.h> #define CRITICAL_SECTION pthread_mutex_t #define _vsnprintf vsnprintf#endif//Log{#define MAXLOGSIZE 100000000#define ARRSIZE(x) (sizeof(x)/sizeof(x[0]))#include <time.h>#include <sys/timeb.h>#include <stdarg.h>char logfilename1[]="MyLog1.log";char logfilename2[]="MyLog2.log";char logstr[16000];char datestr[16];char timestr[16];char mss[4];CRITICAL_SECTION cs_log;FILE *flog;#ifdef WIN32void Lock(CRITICAL_SECTION *l) { EnterCriticalSection(l);}void Unlock(CRITICAL_SECTION *l) { LeaveCriticalSection(l);}#elsevoid Lock(CRITICAL_SECTION *l) { pthread_mutex_lock(l);}void Unlock(CRITICAL_SECTION *l) { pthread_mutex_unlock(l);}#endifvoid LogV(const char *pszFmt,va_list argp) { struct tm *now; struct timeb tb; if (NULL==pszFmt||0==pszFmt[0]) return; if (-1==_vsnprintf(logstr,ARRSIZE(logstr),pszFmt,argp)) logstr[ARRSIZE(logstr)-1]=0; ftime(&tb); now=localtime(&tb.time); sprintf(datestr,"%04d-%02d-%02d",now->tm_year+1900,now->tm_mon+1,now->tm_mday); sprintf(timestr,"%02d:%02d:%02d",now->tm_hour ,now->tm_min ,now->tm_sec ); sprintf(mss,"%03d",tb.millitm); printf("%s %s.%s %s",datestr,timestr,mss,logstr); flog=fopen(logfilename1,"a"); if (NULL!=flog) { fprintf(flog,"%s %s.%s %s",datestr,timestr,mss,logstr); if (ftell(flog)>MAXLOGSIZE) { fclose(flog); if (rename(logfilename1,logfilename2)) { remove(logfilename2); rename(logfilename1,logfilename2); } flog=fopen(logfilename1,"a"); if (NULL==flog) return; } fclose(flog); }}void Log(const char *pszFmt,...) { va_list argp; Lock(&cs_log); va_start(argp,pszFmt); LogV(pszFmt,argp); va_end(argp); Unlock(&cs_log);}//Log}int main(int argc,char * argv[]) { int i;#ifdef WIN32 InitializeCriticalSection(&cs_log);#else pthread_mutex_init(&cs_log,NULL);#endif for (i=0;i<10000;i++) { Log("This is a Log %04d from FILE:%s LINE:%d\n",i, __FILE__, __LINE__); }#ifdef WIN32 DeleteCriticalSection(&cs_log);#else pthread_mutex_destroy(&cs_log);#endif return 0;}
[解决办法]
功能都太弱,不支持日志分割,不支持多级别
[解决办法]
日志用log4j库
[解决办法]
lz,我为什么没看到 您日志文件fclose,难道这个不用关闭?
[解决办法]
#ifndef __GUARD_LOG_H#define __GUARD_LOG_H#include <stdio.h>#include <stdlib.h>#include <string.h>#ifdef WIN32 #include <windows.h> //#include <io.h>#else #include <unistd.h> #include <sys/time.h> #include <pthread.h> #define CRITICAL_SECTION pthread_mutex_t #define _vsnprintf vsnprintf#endif//WIN32#ifndef MAX_PATH#define MAX_PATH 256#endifconst int LOG_HIT = 0;//提示const int LOG_WARN = 1;//警告const int LOG_ERROR = 2;//错误const int LOG_CRITICAL = 3;//严重错误class AutoLock{ public: AutoLock() {#ifdef WIN32 ::InitializeCriticalSection(§ion);#endif } virtual ~AutoLock() {#ifdef WIN32 ::DeleteCriticalSection(§ion);#endif } public: bool Lock() {#ifdef WIN32 ::EnterCriticalSection(§ion);#else pthread_mutex_lock(§ion);#endif return true; } bool Unlock() {#ifdef WIN32 ::LeaveCriticalSection(§ion);#else pthread_mutex_unlock(§ion);#endif return true; } private: CRITICAL_SECTION section;};class LogFile{ //C++类的LOG实现public: LogFile(); virtual ~LogFile();public: void Start(const char* pszFilename); void Write(int nErrorLevel, const char* pFormatStr, ...); void WriteLine(int nErrorLevel, const char* pFormatStr, ...); void Close(); bool CreateDir(const char * pszLogDir);protected: void CreateFile(char* pszFileName);protected: char file_name_[MAX_PATH*2]; char file_title_[MAX_PATH*2]; FILE *pLog_; AutoLock lock_;};#endif//__GUARD_LOG_H
[解决办法]