blob: 988a7a29338f9390479199b2c62aeb96eeb4750c [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;
Denis Vlasenkoa9801652006-09-07 16:20:03 +000018const char *msg_eol = "\n";
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000019
20void bb_verror_msg(const char *s, va_list p, const char* strerr)
Eric Andersenb183dfa2001-03-19 19:24:06 +000021{
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000022 /* 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 Vlasenkoa9801652006-09-07 16:20:03 +000032 fputs(msg_eol, stderr);
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000033 else
Denis Vlasenkoa9801652006-09-07 16:20:03 +000034 fprintf(stderr, ": %s%s", strerr, msg_eol);
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000035 }
Denis Vlasenkob750dec2006-09-07 16:03:45 +000036 if (ENABLE_FEATURE_SYSLOG && (logmode & LOGMODE_SYSLOG)) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000037 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 Andersenb183dfa2001-03-19 19:24:06 +000048}