blob: 082e7bf3c8445d60a4cd4b073c50e042301236ed [file] [log] [blame]
Eric Andersenb183dfa2001-03-19 19:24:06 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +00005 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
Eric Andersenb183dfa2001-03-19 19:24:06 +00006 *
Bernhard Reutner-Fischerb1629b12006-05-19 19:29:19 +00007 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Eric Andersenb183dfa2001-03-19 19:24:06 +00008 */
9
Eric Andersenb183dfa2001-03-19 19:24:06 +000010#include "libbb.h"
Bernhard Reutner-Fischerb5f50ea2006-09-12 13:27:55 +000011#include <syslog.h>
Eric Andersenb183dfa2001-03-19 19:24:06 +000012
Denis Vlasenko04c99eb2007-04-07 00:44:31 +000013smallint logmode = LOGMODE_STDIO;
Denis Vlasenkoa9801652006-09-07 16:20:03 +000014const char *msg_eol = "\n";
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000015
16void bb_verror_msg(const char *s, va_list p, const char* strerr)
Eric Andersenb183dfa2001-03-19 19:24:06 +000017{
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000018 /* 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
Denis Vlasenko4ef7d3a2007-05-29 21:41:28 +000023 if (!s) /* nomsg[_and_die] uses NULL fmt */
24 s = ""; /* some libc don't like printf(NULL) */
25
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000026 if (logmode & LOGMODE_STDIO) {
27 fflush(stdout);
Denis Vlasenko8f8f2682006-10-03 21:00:43 +000028 fprintf(stderr, "%s: ", applet_name);
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000029 vfprintf(stderr, s, p);
30 if (!strerr)
Denis Vlasenkoa9801652006-09-07 16:20:03 +000031 fputs(msg_eol, stderr);
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000032 else
Denis Vlasenko27ee7ba2006-09-22 14:53:41 +000033 fprintf(stderr, "%s%s%s",
Denis Vlasenko4ef7d3a2007-05-29 21:41:28 +000034 s[0] ? ": " : "",
Denis Vlasenko27ee7ba2006-09-22 14:53:41 +000035 strerr, msg_eol);
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000036 }
Denis Vlasenkob750dec2006-09-07 16:03:45 +000037 if (ENABLE_FEATURE_SYSLOG && (logmode & LOGMODE_SYSLOG)) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000038 if (!strerr)
39 vsyslog(LOG_ERR, s, p2);
40 else {
41 char *msg;
Denis Vlasenko4ef7d3a2007-05-29 21:41:28 +000042 if (vasprintf(&msg, s, p2) < 0) {
43 fprintf(stderr, "%s: %s\n", applet_name, bb_msg_memory_exhausted);
44 xfunc_die();
45 }
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000046 syslog(LOG_ERR, "%s: %s", msg, strerr);
47 free(msg);
48 }
49 }
50 va_end(p2);
Eric Andersenb183dfa2001-03-19 19:24:06 +000051}