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 | |
Eric Andersen | b183dfa | 2001-03-19 19:24:06 +0000 | [diff] [blame] | 10 | #include "libbb.h" |
Bernhard Reutner-Fischer | b5f50ea | 2006-09-12 13:27:55 +0000 | [diff] [blame] | 11 | #include <syslog.h> |
Eric Andersen | b183dfa | 2001-03-19 19:24:06 +0000 | [diff] [blame] | 12 | |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 13 | int logmode = LOGMODE_STDIO; |
Denis Vlasenko | a980165 | 2006-09-07 16:20:03 +0000 | [diff] [blame] | 14 | const char *msg_eol = "\n"; |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 15 | |
| 16 | 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] | 17 | { |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 18 | /* va_copy is used because it is not portable |
| 19 | * to use va_list p twice */ |
| 20 | va_list p2; |
| 21 | va_copy(p2, p); |
| 22 | |
| 23 | if (logmode & LOGMODE_STDIO) { |
| 24 | fflush(stdout); |
| 25 | fprintf(stderr, "%s: ", bb_applet_name); |
| 26 | vfprintf(stderr, s, p); |
| 27 | if (!strerr) |
Denis Vlasenko | a980165 | 2006-09-07 16:20:03 +0000 | [diff] [blame] | 28 | fputs(msg_eol, stderr); |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 29 | else |
Denis Vlasenko | 27ee7ba | 2006-09-22 14:53:41 +0000 | [diff] [blame] | 30 | fprintf(stderr, "%s%s%s", |
| 31 | s ? ": " : "", |
| 32 | strerr, msg_eol); |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 33 | } |
Denis Vlasenko | b750dec | 2006-09-07 16:03:45 +0000 | [diff] [blame] | 34 | if (ENABLE_FEATURE_SYSLOG && (logmode & LOGMODE_SYSLOG)) { |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 35 | if (!strerr) |
| 36 | vsyslog(LOG_ERR, s, p2); |
| 37 | else { |
| 38 | char *msg; |
| 39 | if (vasprintf(&msg, s, p2) < 0) |
| 40 | bb_error_msg_and_die(bb_msg_memory_exhausted); |
| 41 | syslog(LOG_ERR, "%s: %s", msg, strerr); |
| 42 | free(msg); |
| 43 | } |
| 44 | } |
| 45 | va_end(p2); |
Eric Andersen | b183dfa | 2001-03-19 19:24:06 +0000 | [diff] [blame] | 46 | } |