"Fossies" - the Fresh Open Source Software Archive

Member "leafnode-1.12.0/ln_log.c" (28 Dec 2021, 3006 Bytes) of package /linux/misc/leafnode-1.12.0.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 "ln_log.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 1.11.12_vs_1.12.0.

    1 /* (C) Copyright 2002 - 2005 by Matthias Andree. See COPYING for license. */
    2 
    3 #include "wantassert.h"
    4 #include "leafnode.h"
    5 #include "ln_log.h"
    6 
    7 #include <stdarg.h>
    8 #include <assert.h>
    9 #include <syslog.h>
   10 #include <stdio.h>
   11 #include <errno.h>
   12 #include <string.h>
   13 #include <stdlib.h>     /* for getenv */
   14 
   15 #ifdef WITH_DMALLOC
   16 #include <dmalloc.h>
   17 #endif
   18 
   19 #include <signal.h>     /* for raise */
   20 /* add LOG_NEWS where it doesn't exist */
   21 #if !defined(LOG_NEWS)
   22 #define LOG_NEWS LOG_DAEMON
   23 #endif
   24 /* if LOG_CONS isn't supported, do without */
   25 #if !defined(LOG_CONS)
   26 #define LOG_CONS 0
   27 #endif
   28 
   29 static int maylog_console = 1; /* if 0, ln_log* stuff will not log to consoles */
   30 
   31 void ln_log_use_console(int en) {
   32     maylog_console = en;
   33 }
   34 
   35 static void vln_log_core(int slg, FILE /*@null@*/ * console,
   36              int severity,
   37              int context, const char *format, va_list ap);
   38 
   39 /* log to syslog if slg != 0
   40  * log to stream console if console != 0
   41  * ctx to have verbosity like context
   42  * other arguments like syslog */
   43 static void
   44 vln_log_core(int slg, FILE * console, int severity,
   45          int context, const char *format, va_list ap)
   46 {
   47     char buf[2048], fmt[2048], *y;
   48     const char *x;
   49     int errno_save = errno;
   50 
   51     assert(severity >= LNLOG_SMIN);
   52     assert(context >= 0);
   53     assert(verbose >= 0);
   54     /* support %m */
   55     for (x = format, y = fmt; *x && y < fmt + sizeof(fmt) - 2; x++) {
   56     if (*x == '%') {
   57         x++;
   58         if (*x == '%') {
   59         *y++ = *x;
   60         *y++ = *x;
   61         } else if (*x == 'm') {
   62         const char *z = strerror(errno_save);
   63 
   64         while (*z && y < fmt + sizeof(fmt)) {
   65             if (*z == '%')
   66             *(y++) = *z;
   67             *(y++) = *(z++);
   68         }
   69         } else {
   70         *y++ = '%';
   71         *y++ = *x;
   72         }
   73     } else {
   74         *y++ = *x;
   75     }
   76     }
   77     *y = '\0';
   78     vsnprintf(buf, sizeof(buf), fmt, ap);
   79 #ifndef TESTMODE
   80     if (slg != 0 && (severity < LNLOG_SDEBUG || debugmode)) {
   81     syslog(severity, "%s", buf);
   82     }
   83 #endif /* not TESTMODE */
   84 
   85     if (severity <= LNLOG_SERR) {
   86     /* check if environment demands kill on first error */
   87     char *k = getenv("LN_LOG_ABORT_ON_ERROR");
   88     if (k)
   89         abort();
   90     }
   91 
   92     if (console && maylog_console) {
   93     /* always log LNLOG_SERR and more severe,
   94        regardless of verbosity */
   95     if (context <= verbose+1 || severity <= LNLOG_SERR) {
   96         (void)fputs(buf, console);
   97         (void)fputc('\n', console);
   98     }
   99     }
  100 }
  101 
  102 void
  103 ln_log(int sev, int ctx, const char *format, ...)
  104 {
  105     va_list ap;
  106 
  107     va_start(ap, format);
  108     vln_log_core(1, stderr, sev, ctx, format, ap);
  109     va_end(ap);
  110 }
  111 
  112 void
  113 ln_log_so(int sev, int ctx, const char *format, ...)
  114 {
  115     va_list ap;
  116 
  117     va_start(ap, format);
  118     vln_log_core(1, stdout, sev, ctx, format, ap);
  119     va_end(ap);
  120 }
  121 
  122 void
  123 ln_log_prt(int sev, int ctx, const char *format, ...)
  124 {
  125     va_list ap;
  126 
  127     va_start(ap, format);
  128     vln_log_core(0, stderr, sev, ctx, format, ap);
  129     va_end(ap);
  130 }
  131 
  132 void
  133 ln_log_sys(int sev, int ctx, const char *format, ...)
  134 {
  135     va_list ap;
  136 
  137     va_start(ap, format);
  138     vln_log_core(1, NULL, sev, ctx, format, ap);
  139     va_end(ap);
  140 }