"Fossies" - the Fresh Open Source Software Archive

Member "logrotate-3.18.1/log.c" (21 May 2021, 2129 Bytes) of package /linux/privat/logrotate-3.18.1.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "log.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 3.18.0_vs_3.18.1.

    1 #include <stdarg.h>
    2 #include <stdio.h>
    3 #include <stdlib.h>
    4 #include <unistd.h>
    5 #ifdef HAVE_VSYSLOG
    6 #include <syslog.h>
    7 #endif
    8 
    9 #include "log.h"
   10 
   11 static int logLevel = MESS_DEBUG;
   12 static FILE *messageFile = NULL;
   13 static int _logToSyslog = 0;
   14 
   15 void logSetLevel(int level)
   16 {
   17     logLevel = level;
   18 }
   19 
   20 void logSetMessageFile(FILE * f)
   21 {
   22     messageFile = f;
   23 }
   24 
   25 void logToSyslog(int enable) {
   26     _logToSyslog = enable;
   27 
   28 #ifdef HAVE_VSYSLOG
   29     if (_logToSyslog) {
   30         openlog("logrotate", 0, LOG_USER);
   31     }
   32     else {
   33         closelog();
   34     }
   35 #endif
   36 }
   37 
   38 __attribute__((format (printf, 3, 0)))
   39 static void log_once(FILE *where, int level, const char *format, va_list args)
   40 {
   41     switch (level) {
   42         case MESS_DEBUG:
   43         case MESS_NORMAL:
   44         case MESS_VERBOSE:
   45             break;
   46         default:
   47             fprintf(where, "error: ");
   48             break;
   49     }
   50 
   51     vfprintf(where, format, args);
   52     fflush(where);
   53 }
   54 
   55 __attribute__((format (printf, 2, 3)))
   56 void message(int level, const char *format, ...)
   57 {
   58     va_list args;
   59 
   60     if (level >= logLevel) {
   61         va_start(args, format);
   62         log_once(stderr, level, format, args);
   63         va_end(args);
   64     }
   65 
   66     if (messageFile != NULL) {
   67         va_start(args, format);
   68         log_once(messageFile, level, format, args);
   69         va_end(args);
   70     }
   71 
   72 #ifdef HAVE_VSYSLOG
   73     if (_logToSyslog) {
   74         int priority = LOG_USER;
   75 
   76         switch(level) {
   77             case MESS_REALDEBUG:
   78                 priority |= LOG_DEBUG;
   79                 break;
   80             case MESS_DEBUG:
   81             case MESS_VERBOSE:
   82             case MESS_NORMAL:
   83                 priority |= LOG_INFO;
   84                 break;
   85             case MESS_ERROR:
   86                 priority |= LOG_ERR;
   87                 break;
   88             case MESS_FATAL:
   89                 priority |= LOG_CRIT;
   90                 break;
   91             default:
   92                 priority |= LOG_INFO;
   93                 break;
   94         }
   95 
   96         va_start(args, format);
   97         vsyslog(priority, format, args);
   98         va_end(args);
   99     }
  100 #endif
  101 
  102     if (level == MESS_FATAL)
  103         exit(1);
  104 }
  105 
  106 /* vim: set et sw=4 ts=4: */