Back to index

nagios-plugins  1.4.16
log.c
Go to the documentation of this file.
00001 /*
00002  * log.c      Logging module.
00003  *
00004  */
00005 
00006 char log_sccsid[] =
00007 "@(#)log.c      1.2 Copyright 1998 Cistron Internet Services B.V,";
00008 
00009 #include      <stdio.h>
00010 #include      <stdlib.h>
00011 #include      <unistd.h>
00012 #include      <stdarg.h>
00013 #include      <time.h>
00014 #include      "radiusd.h"
00015 
00016 extern char   *radlog_dir;
00017 
00018 
00019 /*
00020  *     Log the message to the logfile. Include the severity and
00021  *     a time stamp.
00022  */
00023 static int do_log(int lvl, char *fmt, va_list ap)
00024 {
00025        FILE   *msgfd;
00026        unsigned char *s = ": ";
00027        char   buffer[256];
00028        time_t timeval;
00029        int    len;
00030 
00031        if ((lvl & L_CONS) || radlog_dir == NULL || debug_flag) {
00032               lvl &= ~L_CONS;
00033               if (!debug_flag) fprintf(stderr, "%s: ", progname);
00034               vfprintf(stderr, fmt, ap);
00035               fprintf(stderr, "\n");
00036        }
00037        if (radlog_dir == NULL || debug_flag) return 0;
00038 
00039        if (strcmp(radlog_dir, "stdout") != 0) {
00040               sprintf(buffer, "%.200s/%.50s", radlog_dir, RADIUS_LOG);
00041               if((msgfd = fopen(buffer, "a")) == NULL) {
00042                      fprintf(stderr, "%s: Couldn't open %s for logging\n",
00043                                    progname, buffer);
00044                      return -1;
00045               }
00046        } else {
00047               msgfd = stdout;
00048        }
00049 
00050        timeval = time(0);
00051        strcpy(buffer, ctime(&timeval));
00052        switch(lvl) {
00053               case L_DBG:
00054                      s = ": Debug: ";
00055                      break;
00056               case L_AUTH:
00057                      s = ": Auth: ";
00058                      break;
00059               case L_PROXY:
00060                      s = ": Proxy: ";
00061                      break;
00062               case L_INFO:
00063                      s = ": Info: ";
00064                      break;
00065               case L_ERR:
00066                      s = ": Error: ";
00067                      break;
00068        }
00069        strNcpy(buffer + 24, s, sizeof(buffer) - 24);
00070        len = strlen(buffer);
00071 
00072        vsprintf(buffer + len, fmt, ap);
00073        if (strlen(buffer) >= sizeof(buffer))
00074               /* What else can we do if we don't have vnsprintf */
00075               _exit(1);
00076 
00077        /*
00078         *     Filter out characters not in Latin-1.
00079         */
00080        for (s = buffer; *s; s++) {
00081               if (*s == '\r' || *s == '\n')
00082                      *s = ' ';
00083               else if (*s < 32 || (*s >= 128 && *s <= 160))
00084                      *s = '?';
00085        }
00086        strcat(buffer, "\n");
00087 
00088        fputs(buffer, msgfd);
00089        if (msgfd != stdout) 
00090          fclose(msgfd);
00091        else
00092          fflush(stdout);
00093 
00094        return 0;
00095 }
00096 
00097 int log_debug(char *msg, ...)
00098 {
00099        va_list ap;
00100        int r;
00101 
00102        va_start(ap, msg);
00103        r = do_log(L_DBG, msg, ap);
00104        va_end(ap);
00105 
00106        return r;
00107 }
00108 
00109 int log(int lvl, char *msg, ...)
00110 {
00111        va_list ap;
00112        int r;
00113 
00114        va_start(ap, msg);
00115        r = do_log(lvl, msg, ap);
00116        va_end(ap);
00117 
00118        return r;
00119 }
00120