diff options
Diffstat (limited to 'log.c')
-rw-r--r-- | log.c | 35 |
1 files changed, 32 insertions, 3 deletions
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: log.c,v 1.41 2008/06/10 04:50:25 dtucker Exp $ */ | 1 | /* $OpenBSD: log.c,v 1.42 2011/06/17 21:44:30 djm Exp $ */ |
2 | /* | 2 | /* |
3 | * Author: Tatu Ylonen <ylo@cs.hut.fi> | 3 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
4 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland | 4 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
@@ -56,6 +56,8 @@ static LogLevel log_level = SYSLOG_LEVEL_INFO; | |||
56 | static int log_on_stderr = 1; | 56 | static int log_on_stderr = 1; |
57 | static int log_facility = LOG_AUTH; | 57 | static int log_facility = LOG_AUTH; |
58 | static char *argv0; | 58 | static char *argv0; |
59 | static log_handler_fn *log_handler; | ||
60 | static void *log_handler_ctx; | ||
59 | 61 | ||
60 | extern char *__progname; | 62 | extern char *__progname; |
61 | 63 | ||
@@ -260,6 +262,9 @@ log_init(char *av0, LogLevel level, SyslogFacility facility, int on_stderr) | |||
260 | exit(1); | 262 | exit(1); |
261 | } | 263 | } |
262 | 264 | ||
265 | log_handler = NULL; | ||
266 | log_handler_ctx = NULL; | ||
267 | |||
263 | log_on_stderr = on_stderr; | 268 | log_on_stderr = on_stderr; |
264 | if (on_stderr) | 269 | if (on_stderr) |
265 | return; | 270 | return; |
@@ -327,6 +332,23 @@ log_init(char *av0, LogLevel level, SyslogFacility facility, int on_stderr) | |||
327 | #define MSGBUFSIZ 1024 | 332 | #define MSGBUFSIZ 1024 |
328 | 333 | ||
329 | void | 334 | void |
335 | set_log_handler(log_handler_fn *handler, void *ctx) | ||
336 | { | ||
337 | log_handler = handler; | ||
338 | log_handler_ctx = ctx; | ||
339 | } | ||
340 | |||
341 | void | ||
342 | do_log2(LogLevel level, const char *fmt,...) | ||
343 | { | ||
344 | va_list args; | ||
345 | |||
346 | va_start(args, fmt); | ||
347 | do_log(level, fmt, args); | ||
348 | va_end(args); | ||
349 | } | ||
350 | |||
351 | void | ||
330 | do_log(LogLevel level, const char *fmt, va_list args) | 352 | do_log(LogLevel level, const char *fmt, va_list args) |
331 | { | 353 | { |
332 | #if defined(HAVE_OPENLOG_R) && defined(SYSLOG_DATA_INIT) | 354 | #if defined(HAVE_OPENLOG_R) && defined(SYSLOG_DATA_INIT) |
@@ -337,6 +359,7 @@ do_log(LogLevel level, const char *fmt, va_list args) | |||
337 | char *txt = NULL; | 359 | char *txt = NULL; |
338 | int pri = LOG_INFO; | 360 | int pri = LOG_INFO; |
339 | int saved_errno = errno; | 361 | int saved_errno = errno; |
362 | log_handler_fn *tmp_handler; | ||
340 | 363 | ||
341 | if (level > log_level) | 364 | if (level > log_level) |
342 | return; | 365 | return; |
@@ -375,7 +398,7 @@ do_log(LogLevel level, const char *fmt, va_list args) | |||
375 | pri = LOG_ERR; | 398 | pri = LOG_ERR; |
376 | break; | 399 | break; |
377 | } | 400 | } |
378 | if (txt != NULL) { | 401 | if (txt != NULL && log_handler == NULL) { |
379 | snprintf(fmtbuf, sizeof(fmtbuf), "%s: %s", txt, fmt); | 402 | snprintf(fmtbuf, sizeof(fmtbuf), "%s: %s", txt, fmt); |
380 | vsnprintf(msgbuf, sizeof(msgbuf), fmtbuf, args); | 403 | vsnprintf(msgbuf, sizeof(msgbuf), fmtbuf, args); |
381 | } else { | 404 | } else { |
@@ -383,7 +406,13 @@ do_log(LogLevel level, const char *fmt, va_list args) | |||
383 | } | 406 | } |
384 | strnvis(fmtbuf, msgbuf, sizeof(fmtbuf), | 407 | strnvis(fmtbuf, msgbuf, sizeof(fmtbuf), |
385 | log_on_stderr ? LOG_STDERR_VIS : LOG_SYSLOG_VIS); | 408 | log_on_stderr ? LOG_STDERR_VIS : LOG_SYSLOG_VIS); |
386 | if (log_on_stderr) { | 409 | if (log_handler != NULL) { |
410 | /* Avoid recursion */ | ||
411 | tmp_handler = log_handler; | ||
412 | log_handler = NULL; | ||
413 | tmp_handler(level, fmtbuf, log_handler_ctx); | ||
414 | log_handler = tmp_handler; | ||
415 | } else if (log_on_stderr) { | ||
387 | snprintf(msgbuf, sizeof msgbuf, "%s\r\n", fmtbuf); | 416 | snprintf(msgbuf, sizeof msgbuf, "%s\r\n", fmtbuf); |
388 | write(STDERR_FILENO, msgbuf, strlen(msgbuf)); | 417 | write(STDERR_FILENO, msgbuf, strlen(msgbuf)); |
389 | } else { | 418 | } else { |