home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume3 / pcmail / part05 / logs.c < prev    next >
C/C++ Source or Header  |  1989-02-03  |  5KB  |  215 lines

  1. /*++
  2. /* NAME
  3. /*      logs 3
  4. /* SUMMARY
  5. /*      error logging, status reports, debugging
  6. /* PROJECT
  7. /*      pc-mail
  8. /* PACKAGE
  9. /*      cico
  10. /* SYNOPSIS
  11. /*      void dbg(fmt[,args]);
  12. /*      char *fmt;
  13. /*
  14. /*      int open_log();
  15. /*
  16. /*      void log(fmt[,args]);
  17. /*      char *fmt;
  18. /*
  19. /*      void trap(code,fmt[,args]);
  20. /*      char *fmt;
  21. /* DESCRIPTION
  22. /*      All functions in this module do some form of logging, and accept
  23. /*      printf-like format strings with %s, %c, and %S, %C. The latter
  24. /*    two cause output mapping of arbitrary byte values to printable codes.
  25. /*
  26. /*      dbg() formats its arguments and writes the result to the standard
  27. /*      output.
  28. /*
  29. /*      open_log() tries to open the logfile for writing. It returns
  30. /*      a status E_WRITERR if the file could not be opened or created.
  31. /*
  32. /*      log() writes status info to the log file. If debugging is enabled,
  33. /*    the message is also written to the standard output.
  34. /*
  35. /*      trap() writes a message to the log file and performs a longjmp call
  36. /*      (systrap) with the status as given in the code parameter. If 
  37. /*    debugging is enabled, the message is also written to the standard 
  38. /*    output.
  39. /* FUNCTIONS AND MACROS
  40. /*      longjmp()
  41. /* FILES
  42. /*      LOGFILE         status reports
  43. /* BUGS
  44. /*      Logfile info may be lost if the program terminates abnormally.
  45. /*      We do not open/close the with each log() call since that would
  46. /*      slow down performance on floppy-based systems dramatically.
  47. /* AUTHOR(S)
  48. /*      W.Z. Venema
  49. /*      Eindhoven University of Technology
  50. /*      Department of Mathematics and Computer Science
  51. /*      Den Dolech 2, P.O. Box 513, 5600 MB Eindhoven, The Netherlands
  52. /* CREATION DATE
  53. /*      Thu Mar 26 17:45:19 GMT+1:00 1987
  54. /* LAST MODIFICATION
  55. /*    Mon Apr  4 23:44:00 MET 1988
  56. /* VERSION/RELEASE
  57. /*    1.3
  58. /*--*/
  59.  
  60. #include <setjmp.h>
  61. #include <ctype.h>
  62. #include <varargs.h>
  63. #include "defs.h"
  64. #include "logs.h"
  65. #include "path.h"
  66. #include "status.h"
  67.  
  68. #define    dbgout    stdout        /* where debugging output should go */
  69.  
  70. hidden FILE *logfp = NULL;    /* log file file pointer */
  71. hidden char *visible();        /* map characters to readable codes */
  72. hidden void dprintf();        /* special-purpose formatting function */
  73.  
  74. /* dbg - write debugging info to the debugging output */
  75.  
  76. /* VARARGS1 */
  77.  
  78. public void dbg(fmt,va_alist)
  79. register char *fmt;
  80. va_dcl
  81. {
  82.     va_list s;
  83.  
  84.     va_start(s);
  85.     dprintf(dbgout,fmt,s);
  86.     va_end(s);
  87. }
  88.  
  89. /* open_log - check the logfile can be written */
  90.  
  91. public int open_log()
  92. {
  93.     if (logfp == NULL && (logfp = fopen(logfile(),"a")) == NULL)
  94.     return(E_WRITERR);
  95.     else
  96.     return(0);
  97. }
  98.  
  99. /* log - write status info to the log file */
  100.  
  101. /* VARARGS1 */
  102.  
  103. public void log(fmt,va_alist)
  104. register char *fmt;
  105. va_dcl
  106. {
  107.     va_list s;
  108.  
  109.     /* log file should be open! */
  110.  
  111.     if (logfp == NULL)
  112.     exit(E_CONFUSED);
  113.  
  114.     /* write status record to log file */
  115.  
  116.     va_start(s);
  117.     dprintf(logfp,fmt,s);
  118.     putc('\n',logfp);
  119.     va_end(s);
  120.  
  121.     /* if debugging on, write also to debugging output */
  122.  
  123.     if (dflag) {
  124.     va_start(s);
  125.     dprintf(dbgout,fmt,s);
  126.     putc('\n',dbgout);
  127.     va_end(s);
  128.    }
  129. }
  130.  
  131. /* trap - exception handler */
  132.  
  133. /* VARARGS2 */
  134.  
  135. public void trap(code,fmt,va_alist)
  136. int code;
  137. char *fmt;
  138. va_dcl
  139. {
  140.     va_list s;
  141.  
  142.     /* write exception record to log file */
  143.  
  144.     va_start(s);
  145.     dprintf(logfp,fmt,s);
  146.     putc('\n',logfp);
  147.     va_end(s);
  148.  
  149.     /* if debugging on, write also to debugging output */
  150.  
  151.     if (dflag) {
  152.     va_start(s);
  153.     dprintf(dbgout,fmt,s);
  154.     putc('\n',logfp);
  155.     va_end(s);
  156.     }
  157.     longjmp(systrap,code);
  158. }
  159.  
  160. /* visible - turn arbitrary character into something visible */
  161.  
  162. static char *visible(c)
  163. register int c;
  164. {
  165.     static char buf[5];
  166.  
  167.     switch(c&=0377) {
  168.     default:
  169.     sprintf(buf,isascii(c) && isprint(c) ? "%c" : "\\%03o",c);
  170.     return(buf);
  171.     case ' ':
  172.     return("\\s");
  173.     case '\b':
  174.     return("\\b");
  175.     case '\t':
  176.     return("\\t");
  177.     case '\r':
  178.     return("\\r");
  179.     case '\n':
  180.     return("\\n");
  181.     case '\f':
  182.     return("\\f");
  183.     case '\\':
  184.     return("\\\\");
  185.     }
  186. }
  187.  
  188. /* dprintf - handle %s, %c, %S and %C format requests */
  189.  
  190. static void dprintf(fp,fmt,s)
  191. register FILE *fp;
  192. register char *fmt;
  193. va_list s;
  194. {
  195.     register int c;
  196.  
  197.     for (/* void */; c = *fmt; fmt++) {
  198.     if (c != '%') {
  199.         putc(c,fp);
  200.     } else if ((c = *++fmt) == 'S') {        /* %S: translated */
  201.         register char *cp = va_arg(s,char *);
  202.         while(*cp)
  203.         fputs(visible(*cp++&0377),fp);
  204.     } else if (c == 'C') {                /* %C: translated */
  205.         fputs(visible(va_arg(s,int)),fp);
  206.     } else if (c == 's') {                /* %s: string, as is */
  207.         fputs(va_arg(s,char *),fp);
  208.     } else if (c == 'c') {                /* %c: char, as is */
  209.         putc(va_arg(s,int),fp);
  210.     } else if (c == '%') {                /* real % character */
  211.         putc(c,fp);
  212.     }
  213.     }
  214. }
  215.