home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / fax-3.2.1 / lib / libfax / log.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-07-31  |  3.2 KB  |  152 lines

  1. /*
  2.   This file is part of the NetFax system.
  3.  
  4.   (c) Copyright 1989 by David M. Siegel and Sundar Narasimhan.
  5.       All rights reserved.
  6.  
  7.     This program is free software; you can redistribute it and/or modify
  8.     it under the terms of the GNU General Public License as published by
  9.     the Free Software Foundation.
  10.  
  11.     This program is distributed in the hope that it will be useful, 
  12.     but WITHOUT ANY WARRANTY; without even the implied warranty of 
  13.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.     GNU General Public License for more details.
  15.  
  16.     You should have received a copy of the GNU General Public License
  17.     along with this program; if not, write to the Free Software
  18.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20.  
  21. #include <stdio.h>
  22. #include <errno.h>
  23. #include <varargs.h>
  24. #include <strings.h>
  25. #include <syslog.h>
  26.  
  27. #include "log.h"
  28.  
  29. extern char *sys_errlist[];
  30. extern int sys_nerr;
  31. static int log_level = LOG_DEBUG;
  32. static int use_syslog = 0;
  33.  
  34. /*
  35.  * We break this routine out from below to avoid performing
  36.  * this scan if we're not using the format string.  That is,
  37.  * the loglevel is to low to force the printout.
  38.  */
  39. static void fix_format(format_in, format_out, len, errno_save)
  40.      char *format_in;
  41.      char *format_out;
  42.      int len;
  43.      int errno_save;
  44. {
  45.     char *f = format_in;
  46.     char *b = format_out;
  47.     char *b_end = &format_out[len];
  48.     char c;
  49.  
  50.     while ((c = *f++) != '\0' && b < b_end) {
  51.     switch (c) {
  52.       case '%':
  53.         c = *f++;
  54.         switch (c) {
  55.           case 'm':
  56.         if ((unsigned)errno_save > sys_nerr)
  57.           sprintf(b, "error %d", errno_save);
  58.         else
  59.           strcpy(b, sys_errlist[errno_save]);
  60.         b += strlen(b);
  61.         break;
  62.           default:
  63.         *b++ = '%';
  64.         *b++ = c;
  65.         break;
  66.         }
  67.         break;
  68.       default:
  69.         *b++ = c;
  70.         break;
  71.     }
  72.     }
  73.  
  74.     *b = '\0';
  75. }
  76.  
  77. /* VARARGS */
  78. int log(va_alist)
  79.      va_dcl
  80. {
  81.     va_list ap;
  82.     int level;
  83.     char *file;
  84.     int line;
  85.     char *format;
  86.     char formatbuf[BUFSIZ];
  87.     char finalbuf[BUFSIZ];
  88.     char outbuf[BUFSIZ];
  89.     char *outbuf_ptr = outbuf;
  90.     char *slash, *ptr;
  91.     int errno_save = errno;
  92.  
  93.     va_start(ap);
  94.  
  95.     level = va_arg(ap, int);
  96.  
  97.     if (level > log_level)
  98.       return (0);
  99.  
  100.     file = va_arg(ap, char *);
  101.     line = va_arg(ap, int);
  102.     format = va_arg(ap, char *);
  103.  
  104.     if ((slash = rindex(file, '/')) != NULL)
  105.       file = slash+1;
  106.  
  107.     fix_format(format, formatbuf, sizeof(formatbuf), errno_save);
  108.     if (!use_syslog)
  109.       fprintf(stderr, "(%s:%d) ", file, line);
  110.     vsprintf(finalbuf, formatbuf, ap);
  111.  
  112.     for (ptr = finalbuf; *ptr != NULL; ptr++) {
  113.     switch (*ptr) {
  114.       case '\r':
  115.         *(outbuf_ptr++) = '\\';
  116.         *(outbuf_ptr++) = 'r';
  117.         break;
  118.       case '\n':
  119.         *(outbuf_ptr++) = '\\';
  120.         *(outbuf_ptr++) = 'n';
  121.         break;
  122.       default:
  123.         *(outbuf_ptr++) = *ptr;
  124.         break;
  125.     }
  126.     }
  127.     *(outbuf_ptr++) = '\n';
  128.     *(outbuf_ptr++) = '\0';
  129.  
  130.     if (use_syslog)
  131.       syslog(level, outbuf);
  132.     else
  133.       fputs(outbuf, stderr);
  134.     
  135.     va_end(ap);
  136.  
  137.     return (0);
  138. }
  139.  
  140. void log_set_level(level)
  141.      int level;
  142. {
  143.     log_level = level;
  144. }
  145.  
  146. void log_enable_syslog(facility)
  147.      int facility;
  148. {
  149.     openlog("fax", LOG_PID|LOG_NOWAIT, facility);
  150.     use_syslog = 1;
  151. }
  152.