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