home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / ip / ppp / dp-2.3 / dpd / log.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-12-22  |  5.0 KB  |  190 lines

  1. /*
  2. **  Logging routines.
  3. **  Copyright (c) 1991 Bolt Beranek and Newman, Inc.
  4. **  All rights reserved.
  5. **
  6. **  Redistribution and use in source and binary forms are permitted
  7. **  provided that: (1) source distributions retain this entire copyright
  8. **  notice and comment, and (2) distributions including binaries display
  9. **  the following acknowledgement:  ``This product includes software
  10. **  developed by Bolt Beranek and Newman, Inc. and CREN/CSNET'' in the
  11. **  documentation or other materials provided with the distribution and in
  12. **  all advertising materials mentioning features or use of this software.
  13. **  Neither the name of Bolt Beranek and Newman nor CREN/CSNET may be used
  14. **  to endorse or promote products derived from this software without
  15. **  specific prior written permission.
  16. **
  17. **  THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  18. **  WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  19. **  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  20. */
  21. /*
  22.  * Copyright (c) 1992 Purdue University
  23.  * All rights reserved.
  24.  *
  25.  * Redistribution and use in source and binary forms are permitted
  26.  * provided that the above copyright notice and this paragraph are
  27.  * duplicated in all such forms and that any documentation,
  28.  * advertising materials, and other materials related to such
  29.  * distribution and use acknowledge that the software was developed
  30.  * by Purdue University.  The name of the University may not be used
  31.  * to endorse or promote products derived * from this software without
  32.  * specific prior written permission.
  33.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  34.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  35.  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  36.  *
  37.  * Note: this copyright applies to portions of this software developed
  38.  * at Purdue beyond the software covered by the original copyright.
  39.  */
  40. #include <stdio.h>
  41. #include <varargs.h>
  42. #include <time.h>
  43. #include <sys/types.h>
  44. #include "dp.h"
  45.  
  46.  
  47. /*
  48. **  Current logging level.
  49. */
  50. int    log_level = DLOG_GENERAL;
  51.  
  52.  
  53. /*
  54. **  Internal logging routine.
  55. */
  56. void
  57. d_vlog(level, where, format, ap)
  58.     int            level;
  59.     char        *where;
  60.     char        *format;
  61.     va_list        ap;
  62. {
  63.     extern time_t    time();
  64.     extern struct tm    *localtime();
  65.     extern char        *strerror();
  66.     extern int        errno;
  67.     static char        CONSOLE[] = "/dev/console";
  68.     static char        *LOG;
  69.     FILE        *F;
  70.     struct tm        *tp;
  71.     time_t        t;
  72.     char        form[30];
  73.     char        *fp;
  74.     char        c;
  75.     int            oerrno;
  76.     int            mvalue;
  77.     int            islong;
  78.     int            done;
  79.  
  80.     if (level > log_level)
  81.     return;
  82.  
  83.     if (!LOG)
  84.     LOG = expand_dir_file("$DPLOG_DIR", LOG_FILE);
  85.  
  86.     /* Save errno for %m. */
  87.     mvalue = errno;
  88.  
  89.     /* Open the log file */
  90.     if ((F = fopen(LOG, "a")) == NULL) {
  91.     oerrno = errno;
  92.     if ((F = fopen("/dev/console", "a")) == NULL) {
  93.         (void)fprintf(stderr, "%s:  Can't open logfile \"%s\",\n\t%s\n",
  94.             progname, LOG, strerror(oerrno));
  95.         return;
  96.     }
  97.     (void)fprintf(stderr,
  98.         "%s:  Can't open logfile \"%s\",\n\t%s, using \"%s\"\n",
  99.         progname, LOG, strerror(oerrno), CONSOLE);
  100.     }
  101.  
  102.     /* Get the timestamp, write the message header. */
  103.     (void)time(&t);
  104.     tp = localtime(&t);
  105.     (void)fprintf(F, "%02d/%02d %02d:%02d:%02d\t%d\t%s: ",
  106.     tp->tm_mon + 1, tp->tm_mday, tp->tm_hour, tp->tm_min, tp->tm_sec,
  107.     getpid(), where);
  108.  
  109.     while (c = *format++) {
  110.     if (c != '%') {
  111.         (void)fputc(c, F);
  112.         continue;
  113.     }
  114.     if (*format == '%') {
  115.         (void)fputc('%', F);
  116.         format++;
  117.         continue;
  118.     }
  119.  
  120.     /* A standard printf format. copy it into form. */
  121.     for (islong = 0, fp = form, *fp++ = c, done = 0; done == 0; )
  122.         switch (c = *format++) {
  123.         default:
  124.         *fp++ = c;
  125.         break;
  126.         case 'o': case 'c': case 'd': case 's': case 'u': case 'x':
  127.         case 'e': case 'E': case 'f': case 'g': case 'G':
  128.         case 'm':
  129.         done++;
  130.         /* FALLTHROUGH */
  131.         case ' ': case '+': case '-': case '.': case '#':
  132.         case '0': case '1': case '2': case '3': case '4':
  133.         case '5': case '6': case '7': case '8': case '9':
  134.         *fp++ = c;
  135.         break;
  136.         case 'l':
  137.         islong++;
  138.         *fp++ = c;
  139.         break;
  140.         }
  141.     *fp = '\0';
  142.  
  143.     /* We have the format string, get the argument and print it. */
  144.     switch (fp[-1]) {
  145.     case 'x': case 'o': case 'c': case 'd': case 'u':
  146.         if (islong)
  147.         (void)fprintf(F, form, va_arg(ap, long));
  148.         else
  149.         (void)fprintf(F, form, va_arg(ap, int));
  150.         break;
  151.     case 'e': case 'E': case 'f': case 'g': case 'G':
  152.         (void)fprintf(F, form, va_arg(ap, double));
  153.         break;
  154.     case 's':
  155.         (void)fprintf(F, form, va_arg(ap, char*));
  156.         break;
  157.     case 'm':
  158.         fp[-1] = 's';
  159.         (void)fprintf(F, form, strerror(mvalue));
  160.         break;
  161.     }
  162.     }
  163.     (void)fputc('\n', F);
  164.     (void)fclose(F);
  165. }
  166.  
  167.  
  168.  
  169. /*
  170. **  Write debug log entries.  Calling sequence:
  171. **    d_log(level, where, format, args...)
  172. */
  173. /* VARARGS0 */
  174. void
  175. d_log(va_alist)
  176.     va_dcl
  177. {
  178.     int        level;
  179.     char    *where;
  180.     char    *format;
  181.     va_list    ap;
  182.  
  183.     va_start(ap);
  184.     level = va_arg(ap, int);
  185.     where = va_arg(ap, char*);
  186.     format = va_arg(ap, char*);
  187.     d_vlog(level, where, format, ap);
  188.     va_end(ap);
  189. }
  190.