988a7a29338f9390479199b2c62aeb96eeb4750c
[oweals/busybox.git] / libbb / verror_msg.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Utility routines.
4  *
5  * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
6  *
7  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
8  */
9
10 #include <stdio.h>
11 #include <errno.h>
12 #include <string.h>
13 #include <stdlib.h>
14 #include <syslog.h>
15 #include "libbb.h"
16
17 int logmode = LOGMODE_STDIO;
18 const char *msg_eol = "\n";
19
20 void bb_verror_msg(const char *s, va_list p, const char* strerr)
21 {
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)
32                         fputs(msg_eol, stderr);
33                 else
34                         fprintf(stderr, ": %s%s", strerr, msg_eol);
35         }
36         if (ENABLE_FEATURE_SYSLOG && (logmode & LOGMODE_SYSLOG)) {
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);
48 }