home *** CD-ROM | disk | FTP | other *** search
/ ftp.wwiv.com / ftp.wwiv.com.zip / ftp.wwiv.com / pub / MISC / MN325SRC.ZIP / makenl-3.2.5 / src / mklog.c < prev    next >
C/C++ Source or Header  |  2005-02-06  |  2KB  |  90 lines

  1. /* $Id: mklog.c,v 1.3 2004/09/05 10:43:57 mbroek Exp $ */
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <stdarg.h>
  7. #include <time.h>
  8. #include <errno.h>
  9.  
  10. #include "makenl.h"
  11. #include "mklog.h"
  12. #include "fileutil.h"
  13.  
  14. #ifdef MALLOC_DEBUG
  15. #include "rmalloc.h"
  16. #endif
  17.  
  18. #ifdef DMALLOC
  19. #include "dmalloc.h"
  20. #endif
  21.  
  22.  
  23. int loglevel = 1;
  24.  
  25. static char *mon[] = {
  26.     (char *)"Jan",(char *)"Feb",(char *)"Mar",
  27.     (char *)"Apr",(char *)"May",(char *)"Jun",
  28.     (char *)"Jul",(char *)"Aug",(char *)"Sep",
  29.     (char *)"Oct",(char *)"Nov",(char *)"Dec"
  30. };
  31.  
  32. static char *logmark = "?+-dD";
  33.  
  34.  
  35.  
  36. char *date(void);
  37. char *date(void)
  38. {
  39.     struct tm   ptm;
  40.     time_t      now;
  41.     static char buf[20];
  42.  
  43.     now = time(NULL);
  44.     ptm = *localtime(&now);
  45.     sprintf(buf,"%02d-%s-%04d %02d:%02d:%02d", ptm.tm_mday, mon[ptm.tm_mon], ptm.tm_year+1900,
  46.                                             ptm.tm_hour, ptm.tm_min, ptm.tm_sec);
  47.     return(buf);
  48. }
  49.  
  50.  
  51.  
  52. void mklog(int level, const char *format, ...)
  53. {
  54.     char    *outstr;
  55.     va_list     va_ptr;
  56.     FILE    *fp;
  57.     
  58.     if ((level > loglevel) || (strlen(LogFile) == 0))
  59.     {
  60.     return;
  61.     }
  62.  
  63.     if ((fp = fopen(LogFile, "a")) == NULL)
  64.     {
  65.     die(0xFF, 1, "Cannot open logfile \"%s\"", LogFile);
  66.     }
  67.  
  68.     outstr = calloc(4096, sizeof(char));
  69.  
  70.     va_start(va_ptr, format);
  71.     vsprintf(outstr, format, va_ptr);
  72.     va_end(va_ptr);
  73.  
  74. #if defined(__unix__)
  75.     fprintf(fp, "%c %s makenl[%d] ", logmark[level], date(), getpid());
  76. #else
  77.     fprintf(fp, "%c %s makenl: ", logmark[level], date());
  78. #endif
  79.     fprintf(fp, *outstr == '$' ? outstr+1 : outstr);
  80.     if (*outstr == '$')
  81.     fprintf(fp, ": %s\n", strerror(errno));
  82.     else
  83.     fprintf(fp, "\n");
  84.     fflush(fp);
  85.     fclose(fp);
  86.  
  87.     free(outstr);
  88. }
  89.  
  90.