blob: d55da73ff3a958624995537cf06146504f543c96 [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
10#include <stdio.h>
11#include <errno.h>
12#include <string.h>
13#include <stdlib.h>
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000014#include <syslog.h>
Eric Andersenb183dfa2001-03-19 19:24:06 +000015#include "libbb.h"
16
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000017int logmode = LOGMODE_STDIO;
18
19void bb_verror_msg(const char *s, va_list p, const char* strerr)
Eric Andersenb183dfa2001-03-19 19:24:06 +000020{
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000021 /* 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 Vlasenkob750dec2006-09-07 16:03:45 +000035 if (ENABLE_FEATURE_SYSLOG && (logmode & LOGMODE_SYSLOG)) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000036 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 Andersenb183dfa2001-03-19 19:24:06 +000047}