blob: 0f018c5171382ae0831d4b29324111f98579fc85 [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 Vlasenko3538b9a2006-09-06 18:36:50 +000013int 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
23 if (logmode & LOGMODE_STDIO) {
24 fflush(stdout);
Denis Vlasenko8f8f2682006-10-03 21:00:43 +000025 fprintf(stderr, "%s: ", applet_name);
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000026 vfprintf(stderr, s, p);
27 if (!strerr)
Denis Vlasenkoa9801652006-09-07 16:20:03 +000028 fputs(msg_eol, stderr);
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000029 else
Denis Vlasenko27ee7ba2006-09-22 14:53:41 +000030 fprintf(stderr, "%s%s%s",
31 s ? ": " : "",
32 strerr, msg_eol);
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000033 }
Denis Vlasenkob750dec2006-09-07 16:03:45 +000034 if (ENABLE_FEATURE_SYSLOG && (logmode & LOGMODE_SYSLOG)) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000035 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 Andersenb183dfa2001-03-19 19:24:06 +000046}