贡献一下自己写的写日志程序,该如何解决

贡献一下自己写的写日志程序
C/C++ code

#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;      
      }



有意见提啊。环境Red Hat 4.1.2-46, gcc version 4.1.2

------解决方案--------------------
......
我要怎么控制log_level呢?
info 里面的循环不知道干嘛...
给解释解释...
------解决方案--------------------
PK一下我这个:
C/C++ code
#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 WIN32
void Lock(CRITICAL_SECTION *l) {
    EnterCriticalSection(l);
}
void Unlock(CRITICAL_SECTION *l) {
    LeaveCriticalSection(l);
}
#else
void Lock(CRITICAL_SECTION *l) {
    pthread_mutex_lock(l);
}
void Unlock(CRITICAL_SECTION *l) {
    pthread_mutex_unlock(l);
}
#endif
void 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;
}