home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 1999 May / pcp151c.iso / misc / src / install / modutils / insmod / logger.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-01-06  |  2.2 KB  |  106 lines

  1. /* Error logging facilities.
  2.    Copyright 1996, 1997 Linux International.
  3.  
  4.    Contributed by Richard Henderson <rth@tamu.edu>
  5.  
  6.    This file is part of the Linux modutils.
  7.  
  8.    This program is free software; you can redistribute it and/or modify it
  9.    under the terms of the GNU General Public License as published by the
  10.    Free Software Foundation; either version 2 of the License, or (at your
  11.    option) any later version.
  12.  
  13.    This program is distributed in the hope that it will be useful, but
  14.    WITHOUT ANY WARRANTY; without even the implied warranty of
  15.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.    General Public License for more details.
  17.  
  18.    You should have received a copy of the GNU General Public License
  19.    along with this program; if not, write to the Free Software Foundation,
  20.    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
  21.  
  22. #ident "$Id: logger.c,v 1.1.1.1 1998/01/06 20:51:07 ewt Exp $"
  23.  
  24. #include <stdio.h>
  25. #include <stdarg.h>
  26. #include <stdlib.h>
  27. #include <errno.h>
  28. #include <syslog.h>
  29.  
  30. #include "util.h"
  31.  
  32.  
  33. /*======================================================================*/
  34.  
  35. static int log;
  36. static int silent;
  37.  
  38. int errors;
  39. const char *error_file;
  40. const char *program_name;
  41.  
  42. void
  43. error(const char *fmt, ...)
  44. {
  45.   va_list args;
  46.  
  47.   if (silent)
  48.     ;
  49.   else if (log)
  50.     {
  51.       char buf[1024];
  52.       int n;
  53.  
  54.       if (error_file)
  55.         n = snprintf(buf, sizeof(buf), "%s: ", error_file);
  56.       else
  57.     n = 0;
  58.       va_start(args, fmt);
  59.       vsnprintf(buf+n, sizeof(buf)-n, fmt, args);
  60.       va_end(args);
  61.  
  62.       syslog(LOG_ERR, "%s", buf);
  63.     }
  64.   else
  65.     {
  66.       if (error_file)
  67.         fprintf(stderr, "%s: ", error_file);
  68.       va_start(args, fmt);
  69.       vfprintf(stderr, fmt, args);
  70.       va_end(args);
  71.       putc('\n', stderr);
  72.     }
  73.  
  74.   errors++;
  75. }
  76.  
  77. void
  78. lprintf(const char *fmt, ...)
  79. {
  80.   va_list args;
  81.  
  82.   if (silent)
  83.     ;
  84.   else if (log)
  85.     {
  86.       char buf[1024];
  87.       va_start(args, fmt);
  88.       vsnprintf(buf, sizeof(buf), fmt, args);
  89.       va_end(args);
  90.       syslog(LOG_INFO, "%s", buf);
  91.     }
  92.   else
  93.     {
  94.       va_start(args, fmt);
  95.       vfprintf(stdout, fmt, args);
  96.       va_end(args);
  97.       putchar('\n');
  98.     }
  99. }
  100.  
  101. void setsyslog(const char *program)
  102. {
  103.   openlog(program, LOG_CONS, LOG_DAEMON);
  104.   log = 1;
  105. }
  106.