forked from cleech/open-isns
-
Notifications
You must be signed in to change notification settings - Fork 22
/
logging.c
234 lines (203 loc) · 4 KB
/
logging.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*
* Logging related utility functions.
*
* Copyright (C) 2004-2007 Olaf Kirch <[email protected]>
*/
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <libisns/util.h>
static unsigned int log_stdout = 1;
static unsigned int debugging = 0;
/*
* When backgrounding, any logging output should
* go to syslog instead of stdout
*/
void
isns_log_background(void)
{
log_stdout = 0;
}
/*
* For output to syslog, sanitize the format string
* by removing newlines.
*/
static const char *
sanitize_format(const char *fmt)
{
static char __fmt[1024];
unsigned int len;
/* Don't bother unless there's a newline */
if (!strchr(fmt, '\n'))
return fmt;
len = strlen(fmt);
/* Decline if the buffer would overflow */
if (len >= sizeof(__fmt))
return fmt;
strcpy(__fmt, fmt);
while (len > 0) {
len = len - 1;
if (__fmt[len] == '\n')
__fmt[len] = '\0';
else
break;
}
while (len > 0) {
len = len - 1;
if (__fmt[len] == '\n')
__fmt[len] = ' ';
}
return __fmt;
}
/*
* Output to stderr or syslog
*/
static void
voutput(int severity, const char *fmt, va_list ap)
{
if (log_stdout) {
switch (severity) {
case LOG_ERR:
fprintf(stderr, "Error: ");
break;
case LOG_WARNING:
fprintf(stderr, "Warning: ");
break;
case LOG_DEBUG:
fprintf(stderr, " ");
break;
}
vfprintf(stderr, fmt, ap);
} else {
fmt = sanitize_format(fmt);
if (!fmt || !*fmt)
return;
vsyslog(severity, fmt, ap);
}
}
void
isns_assert_failed(const char *condition, const char *file, unsigned int line)
{
isns_error("Assertion failed (%s:%d): %s\n",
file, line, condition);
abort();
}
void
isns_fatal(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
if (log_stdout)
fprintf(stderr, "** FATAL ERROR **\n");
voutput(LOG_ERR, fmt, ap);
va_end(ap);
exit(1);
}
void
isns_error(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
voutput(LOG_WARNING, fmt, ap);
va_end(ap);
}
void
isns_warning(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
voutput(LOG_NOTICE, fmt, ap);
va_end(ap);
}
void
isns_notice(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
voutput(LOG_INFO, fmt, ap);
va_end(ap);
}
void
isns_enable_debugging(const char *what)
{
char *copy, *s, *next;
if (!strcmp(what, "all")) {
debugging = ~0U;
return;
}
copy = isns_strdup(what);
for (s = copy; s; s = next) {
if ((next = strchr(s, ',')) != NULL)
*next++ = '\0';
if (!strcmp(s, "general"))
debugging |= (1 << DBG_GENERAL);
else if (!strcmp(s, "socket"))
debugging |= (1 << DBG_SOCKET);
else if (!strcmp(s, "protocol"))
debugging |= (1 << DBG_PROTOCOL);
else if (!strcmp(s, "state"))
debugging |= (1 << DBG_STATE);
else if (!strcmp(s, "message"))
debugging |= (1 << DBG_MESSAGE);
else if (!strcmp(s, "auth"))
debugging |= (1 << DBG_AUTH);
else if (!strcmp(s, "scn"))
debugging |= (1 << DBG_SCN);
else if (!strcmp(s, "esi"))
debugging |= (1 << DBG_ESI);
else if (!strcmp(s, "all"))
debugging = (unsigned int)-1;
else {
isns_error("Ignoring unknown isns_debug facility <<%s>>\n",
s);
}
}
isns_free(copy);
}
#define DEFINE_DEBUG_FUNC(name, NAME) \
void \
isns_debug_##name(const char *fmt, ...) \
{ \
va_list ap; \
\
if (!(debugging & (1 << DBG_##NAME))) \
return; \
\
va_start(ap, fmt); \
voutput(LOG_DEBUG, fmt, ap); \
va_end(ap); \
}
DEFINE_DEBUG_FUNC(general, GENERAL)
DEFINE_DEBUG_FUNC(socket, SOCKET)
DEFINE_DEBUG_FUNC(protocol, PROTOCOL)
DEFINE_DEBUG_FUNC(message, MESSAGE)
DEFINE_DEBUG_FUNC(auth, AUTH)
DEFINE_DEBUG_FUNC(state, STATE)
DEFINE_DEBUG_FUNC(scn, SCN)
DEFINE_DEBUG_FUNC(esi, ESI)
int
isns_debug_enabled(int fac)
{
return (debugging & (1 << fac)) != 0;
}
/*
* Misc isns_print_fn_t implementations
*/
void
isns_print_stdout(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vfprintf(stdout, fmt, ap);
va_end(ap);
}
void
isns_print_stderr(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
}