home *** CD-ROM | disk | FTP | other *** search
/ PC Press 1997 July / Sezamfile97_1.iso / msdos / c / cbase11.a03 / CBASE11.ZIP / XTEND / ERRLOG.C < prev    next >
C/C++ Source or Header  |  1993-01-01  |  2KB  |  109 lines

  1. /*
  2.  *    Copyright (c) 1989-1992 Citadel Software, Inc.
  3.  *    All Rights Reserved
  4.  */
  5.  
  6. /* #ident    "@(#)errlog.c    1.7 - 93/01/01" */
  7.  
  8. #include <port.h>
  9.  
  10. /* standard headers */
  11. #include <errno.h>
  12. #ifdef AC_STDARG
  13. #include <stdarg.h>
  14. #endif
  15. #include <stdio.h>
  16. #ifdef AC_STRING
  17. #include <string.h>
  18. #endif
  19. #include <time.h>
  20.  
  21. /* local headers */
  22. #include "xtend.h"
  23.  
  24. /* private global data */
  25. static char logfile[PATH_MAX + 1] = "errlog.txt";
  26.  
  27. /*man---------------------------------------------------------------------------
  28. NAME
  29.      errlog - error log
  30.  
  31. SYNOPSIS
  32.      void errlog(file, line, msg, ...)
  33.      const char *file;
  34.      int line;
  35.      const char *msg;
  36.  
  37. DESCRIPTION
  38.      msg = NULL to set logfilename to file.
  39.  
  40. date time file[line]:  msg error n: error string.
  41.  
  42. 92-10-01 14:01:38 example.c[57]:  error 35: File already exists.
  43. default filename is errlog.txt.
  44.  
  45. EXAMPLES
  46.      errlog("errlog.txt", 0, NULL);
  47.  
  48.      errlog(__FILE__, __LINE__, "Error writing to file");
  49.  
  50. ------------------------------------------------------------------------------*/
  51. void errlog(const char *file, int line, const char *msg, ...)
  52. {
  53.     va_list    ap    = NULL;            /* argument list pointer */
  54.     FILE *    fp    = NULL;            /* log file stream */
  55.     int    terrno    = errno;        /* tmp errno */
  56.     struct tm * tp    = NULL;            /* time struct pointer */
  57.     time_t    utc    = 0;            /* universal coord. time */
  58.  
  59.     /* set log file name */
  60.     if (msg == NULL) {
  61.         strncpy(logfile, file, sizeof(logfile));
  62.         logfile[sizeof(logfile) - 1] = NUL;
  63.         errno = terrno;
  64.         return;
  65.     }
  66.  
  67.     /* open log file */
  68.     logfile[sizeof(logfile) - 1] = NUL;
  69.     fp = fopen(logfile, "a");
  70.     if (fp == NULL) {
  71.         errno = terrno;
  72.         return;
  73.     }
  74.  
  75.     /* write message */
  76.     utc = time(NULL);
  77.     tp = localtime(&utc);
  78.     if (fprintf(fp, "%.2d-%.2d-%.2d", tp->tm_year, tp->tm_mon + 1, tp->tm_mday) < 0) {
  79.         errno = terrno;
  80.         return;
  81.     }
  82.     if (fprintf(fp, "% .2d:%.2d:%.2d", tp->tm_hour, tp->tm_min, tp->tm_sec) < 0) {
  83.         errno = terrno;
  84.         return;
  85.     }
  86.     if (fprintf(fp, " %.*s[%d]: ", FILENAME_MAX, file, line) < 0) {
  87.         errno = terrno;
  88.         return;
  89.     }
  90.     va_start(ap, msg);
  91.     vfprintf(fp, msg, ap);
  92.     va_end(ap);
  93.     fprintf(fp, " error %d: %s.", terrno, xstrerror(terrno));
  94.  
  95.     /* newline */
  96.     fputc('\n', fp);
  97.  
  98.     /* close log file */
  99.     if (fclose(fp) == EOF) {
  100.         errno = terrno;
  101.         return;
  102.     }
  103.  
  104.     /* restore errno value */
  105.     errno = terrno;
  106.  
  107.     return;
  108. }
  109.