Eric Andersen | b183dfa | 2001-03-19 19:24:06 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Utility routines. |
| 4 | * |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> |
Eric Andersen | b183dfa | 2001-03-19 19:24:06 +0000 | [diff] [blame] | 6 | * |
Bernhard Reutner-Fischer | b1629b1 | 2006-05-19 19:29:19 +0000 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Eric Andersen | b183dfa | 2001-03-19 19:24:06 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <stdio.h> |
| 11 | #include <errno.h> |
| 12 | #include <string.h> |
| 13 | #include <stdlib.h> |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 14 | #include <syslog.h> |
Eric Andersen | b183dfa | 2001-03-19 19:24:06 +0000 | [diff] [blame] | 15 | #include "libbb.h" |
| 16 | |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 17 | int logmode = LOGMODE_STDIO; |
| 18 | |
| 19 | void bb_verror_msg(const char *s, va_list p, const char* strerr) |
Eric Andersen | b183dfa | 2001-03-19 19:24:06 +0000 | [diff] [blame] | 20 | { |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 21 | /* va_copy is used because it is not portable |
| 22 | * to use va_list p twice */ |
| 23 | va_list p2; |
| 24 | va_copy(p2, p); |
| 25 | |
| 26 | if (logmode & LOGMODE_STDIO) { |
| 27 | fflush(stdout); |
| 28 | fprintf(stderr, "%s: ", bb_applet_name); |
| 29 | vfprintf(stderr, s, p); |
| 30 | if (!strerr) |
| 31 | fputc('\n', stderr); |
| 32 | else |
| 33 | fprintf(stderr, ": %s\n", strerr); |
| 34 | } |
Denis Vlasenko | b750dec | 2006-09-07 16:03:45 +0000 | [diff] [blame^] | 35 | if (ENABLE_FEATURE_SYSLOG && (logmode & LOGMODE_SYSLOG)) { |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 36 | if (!strerr) |
| 37 | vsyslog(LOG_ERR, s, p2); |
| 38 | else { |
| 39 | char *msg; |
| 40 | if (vasprintf(&msg, s, p2) < 0) |
| 41 | bb_error_msg_and_die(bb_msg_memory_exhausted); |
| 42 | syslog(LOG_ERR, "%s: %s", msg, strerr); |
| 43 | free(msg); |
| 44 | } |
| 45 | } |
| 46 | va_end(p2); |
Eric Andersen | b183dfa | 2001-03-19 19:24:06 +0000 | [diff] [blame] | 47 | } |