]>
git.ipfire.org Git - people/ms/strongswan.git/blob - programs/openac/loglite.c
1 /* error logging functions
2 * Copyright (C) 1997 Angelos D. Keromytis.
3 * Copyright (C) 1998-2001 D. Hugh Redelmeier.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 * RCSID $Id: loglite.c,v 1.2 2005/07/11 18:38:16 as Exp $
26 #include <signal.h> /* used only if MSG_NOSIGNAL not defined */
29 #include <sys/types.h>
33 #include "../pluto/constants.h"
34 #include "../pluto/defs.h"
35 #include "../pluto/log.h"
36 #include "../pluto/whack.h"
39 log_to_stderr
= FALSE
, /* should log go to stderr? */
40 log_to_syslog
= TRUE
; /* should log go to syslog? */
43 init_log(const char *program
)
48 openlog(program
, LOG_CONS
| LOG_NDELAY
| LOG_PID
, LOG_AUTHPRIV
);
59 plog(const char *message
, ...)
62 char m
[LOG_WIDTH
]; /* longer messages will be truncated */
64 va_start(args
, message
);
65 vsnprintf(m
, sizeof(m
), message
, args
);
69 fprintf(stderr
, "%s\n", m
);
71 syslog(LOG_WARNING
, "%s", m
);
75 loglog(int mess_no
, const char *message
, ...)
78 char m
[LOG_WIDTH
]; /* longer messages will be truncated */
80 va_start(args
, message
);
81 vsnprintf(m
, sizeof(m
), message
, args
);
85 fprintf(stderr
, "%s\n", m
);
87 syslog(LOG_WARNING
, "%s", m
);
91 log_errno_routine(int e
, const char *message
, ...)
94 char m
[LOG_WIDTH
]; /* longer messages will be truncated */
96 va_start(args
, message
);
97 vsnprintf(m
, sizeof(m
), message
, args
);
101 fprintf(stderr
, "ERROR: %s. Errno %d: %s\n", m
, e
, strerror(e
));
103 syslog(LOG_ERR
, "ERROR: %s. Errno %d: %s", m
, e
, strerror(e
));
107 exit_log(const char *message
, ...)
110 char m
[LOG_WIDTH
]; /* longer messages will be truncated */
112 va_start(args
, message
);
113 vsnprintf(m
, sizeof(m
), message
, args
);
117 fprintf(stderr
, "FATAL ERROR: %s\n", m
);
119 syslog(LOG_ERR
, "FATAL ERROR: %s", m
);
124 exit_log_errno_routine(int e
, const char *message
, ...)
127 char m
[LOG_WIDTH
]; /* longer messages will be truncated */
129 va_start(args
, message
);
130 vsnprintf(m
, sizeof(m
), message
, args
);
134 fprintf(stderr
, "FATAL ERROR: %s. Errno %d: %s\n", m
, e
, strerror(e
));
136 syslog(LOG_ERR
, "FATAL ERROR: %s. Errno %d: %s", m
, e
, strerror(e
));
141 whack_log(int mess_no
, const char *message
, ...)
144 char m
[LOG_WIDTH
]; /* longer messages will be truncated */
146 va_start(args
, message
);
147 vsnprintf(m
, sizeof(m
), message
, args
);
150 fprintf(stderr
, "%s\n", m
);
153 /* Build up a diagnostic in a static buffer.
154 * Although this would be a generally useful function, it is very
155 * hard to come up with a discipline that prevents different uses
156 * from interfering. It is intended that by limiting it to building
157 * diagnostics, we will avoid this problem.
158 * Juggling is performed to allow an argument to be a previous
159 * result: the new string may safely depend on the old one. This
160 * restriction is not checked in any way: violators will produce
161 * confusing results (without crashing!).
163 char diag_space
[sizeof(diag_space
)];
166 builddiag(const char *fmt
, ...)
168 static char diag_space
[LOG_WIDTH
]; /* longer messages will be truncated */
169 char t
[sizeof(diag_space
)]; /* build result here first */
173 t
[0] = '\0'; /* in case nothing terminates string */
174 vsnprintf(t
, sizeof(t
), fmt
, args
);
176 strcpy(diag_space
, t
);
180 /* Debugging message support */
185 switch_fail(int n
, const char *file_str
, unsigned long line_no
)
189 snprintf(buf
, sizeof(buf
), "case %d unexpected", n
);
190 passert_fail(buf
, file_str
, line_no
);
194 passert_fail(const char *pred_str
, const char *file_str
, unsigned long line_no
)
196 /* we will get a possibly unplanned prefix. Hope it works */
197 loglog(RC_LOG_SERIOUS
, "ASSERTION FAILED at %s:%lu: %s", file_str
, line_no
, pred_str
);
198 abort(); /* exiting correctly doesn't always work */
202 base_debugging
= DBG_NONE
, /* default to reporting nothing */
203 cur_debugging
= DBG_NONE
;
206 pexpect_log(const char *pred_str
, const char *file_str
, unsigned long line_no
)
208 /* we will get a possibly unplanned prefix. Hope it works */
209 loglog(RC_LOG_SERIOUS
, "EXPECTATION FAILED at %s:%lu: %s", file_str
, line_no
, pred_str
);
212 /* log a debugging message (prefixed by "| ") */
215 DBG_log(const char *message
, ...)
218 char m
[LOG_WIDTH
]; /* longer messages will be truncated */
220 va_start(args
, message
);
221 vsnprintf(m
, sizeof(m
), message
, args
);
225 fprintf(stderr
, "| %s\n", m
);
227 syslog(LOG_DEBUG
, "| %s", m
);
230 /* dump raw bytes in hex to stderr (for lack of any better destination) */
233 DBG_dump(const char *label
, const void *p
, size_t len
)
235 # define DUMP_LABEL_WIDTH 20 /* arbitrary modest boundary */
236 # define DUMP_WIDTH (4 * (1 + 4 * 3) + 1)
237 char buf
[DUMP_LABEL_WIDTH
+ DUMP_WIDTH
];
239 const unsigned char *cp
= p
;
243 if (label
!= NULL
&& label
[0] != '\0')
245 /* Handle the label. Care must be taken to avoid buffer overrun. */
246 size_t llen
= strlen(label
);
248 if (llen
+ 1 > sizeof(buf
))
250 DBG_log("%s", label
);
255 if (buf
[llen
-1] == '\n')
257 buf
[llen
-1] = '\0'; /* get rid of newline */
260 else if (llen
< DUMP_LABEL_WIDTH
)
274 for (i
= 0; len
!=0 && i
!=4; i
++)
277 for (j
= 0; len
!=0 && j
!=4; len
--, j
++)
279 static const char hexdig
[] = "0123456789abcdef";
282 *bp
++ = hexdig
[(*cp
>> 4) & 0xF];
283 *bp
++ = hexdig
[*cp
& 0xF];
291 # undef DUMP_LABEL_WIDTH