leafnode  1.12.0
About: Leafnode is a store & forward NNTP proxy for small (dialup) sites.
  Fossies Dox: leafnode-1.12.0.tar.xz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

ln_log.c
Go to the documentation of this file.
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
29static int maylog_console = 1; /* if 0, ln_log* stuff will not log to consoles */
30
31void ln_log_use_console(int en) {
32 maylog_console = en;
33}
34
35static 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 */
43static void
44vln_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
102void
103ln_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
112void
113ln_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
122void
123ln_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
132void
133ln_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}
int verbose
Definition: applyfilter.c:31
int debugmode
Definition: configutil.c:67
#define vsnprintf
Definition: leafnode.h:75
static int maylog_console
Definition: ln_log.c:29
void ln_log_prt(int sev, int ctx, const char *format,...)
Definition: ln_log.c:123
void ln_log_so(int sev, int ctx, const char *format,...)
Definition: ln_log.c:113
static void vln_log_core(int slg, FILE *console, int severity, int context, const char *format, va_list ap)
Definition: ln_log.c:44
void ln_log_use_console(int en)
Definition: ln_log.c:31
void ln_log_sys(int sev, int ctx, const char *format,...)
Definition: ln_log.c:133
void ln_log(int sev, int ctx, const char *format,...)
Definition: ln_log.c:103
#define LNLOG_SMIN
Definition: ln_log.h:18
#define LNLOG_SERR
Definition: ln_log.h:13
#define LNLOG_SDEBUG
Definition: ln_log.h:17