Denis Vlasenko | 3da6aa8 | 2006-09-07 05:09:22 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Utility routines. |
| 4 | * |
| 5 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> |
| 6 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Denis Vlasenko | 3da6aa8 | 2006-09-07 05:09:22 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
Denis Vlasenko | 3da6aa8 | 2006-09-07 05:09:22 +0000 | [diff] [blame] | 10 | #include "libbb.h" |
Nguyễn Thái Ngọc Duy | 125d015 | 2010-04-09 14:34:28 +0200 | [diff] [blame] | 11 | #if ENABLE_FEATURE_SYSLOG |
| 12 | # include <syslog.h> |
| 13 | #endif |
Denis Vlasenko | 3da6aa8 | 2006-09-07 05:09:22 +0000 | [diff] [blame] | 14 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 15 | void FAST_FUNC bb_info_msg(const char *s, ...) |
Denis Vlasenko | 3da6aa8 | 2006-09-07 05:09:22 +0000 | [diff] [blame] | 16 | { |
Denis Vlasenko | 517413f | 2008-12-10 11:16:47 +0000 | [diff] [blame] | 17 | #ifdef THIS_ONE_DOESNT_DO_SINGLE_WRITE |
Denis Vlasenko | 3da6aa8 | 2006-09-07 05:09:22 +0000 | [diff] [blame] | 18 | va_list p; |
Denis Vlasenko | dbe6e66 | 2007-08-14 16:43:01 +0000 | [diff] [blame] | 19 | /* va_copy is used because it is not portable |
| 20 | * to use va_list p twice */ |
| 21 | va_list p2; |
Denis Vlasenko | 3da6aa8 | 2006-09-07 05:09:22 +0000 | [diff] [blame] | 22 | |
| 23 | va_start(p, s); |
Denis Vlasenko | dbe6e66 | 2007-08-14 16:43:01 +0000 | [diff] [blame] | 24 | va_copy(p2, p); |
| 25 | if (logmode & LOGMODE_STDIO) { |
| 26 | vprintf(s, p); |
| 27 | fputs(msg_eol, stdout); |
| 28 | } |
Nguyễn Thái Ngọc Duy | 125d015 | 2010-04-09 14:34:28 +0200 | [diff] [blame] | 29 | # if ENABLE_FEATURE_SYSLOG |
| 30 | if (logmode & LOGMODE_SYSLOG) |
Denis Vlasenko | dbe6e66 | 2007-08-14 16:43:01 +0000 | [diff] [blame] | 31 | vsyslog(LOG_INFO, s, p2); |
Nguyễn Thái Ngọc Duy | 125d015 | 2010-04-09 14:34:28 +0200 | [diff] [blame] | 32 | # endif |
Denis Vlasenko | dbe6e66 | 2007-08-14 16:43:01 +0000 | [diff] [blame] | 33 | va_end(p2); |
Denis Vlasenko | 3da6aa8 | 2006-09-07 05:09:22 +0000 | [diff] [blame] | 34 | va_end(p); |
Denis Vlasenko | 517413f | 2008-12-10 11:16:47 +0000 | [diff] [blame] | 35 | #else |
| 36 | int used; |
| 37 | char *msg; |
| 38 | va_list p; |
| 39 | |
| 40 | if (logmode == 0) |
| 41 | return; |
| 42 | |
| 43 | va_start(p, s); |
| 44 | used = vasprintf(&msg, s, p); |
Denys Vlasenko | 78fcec4 | 2009-12-13 17:42:49 +0100 | [diff] [blame] | 45 | va_end(p); |
Denis Vlasenko | 517413f | 2008-12-10 11:16:47 +0000 | [diff] [blame] | 46 | if (used < 0) |
| 47 | return; |
| 48 | |
Nguyễn Thái Ngọc Duy | 125d015 | 2010-04-09 14:34:28 +0200 | [diff] [blame] | 49 | # if ENABLE_FEATURE_SYSLOG |
| 50 | if (logmode & LOGMODE_SYSLOG) |
Denis Vlasenko | 517413f | 2008-12-10 11:16:47 +0000 | [diff] [blame] | 51 | syslog(LOG_INFO, "%s", msg); |
Nguyễn Thái Ngọc Duy | 125d015 | 2010-04-09 14:34:28 +0200 | [diff] [blame] | 52 | # endif |
Denis Vlasenko | 517413f | 2008-12-10 11:16:47 +0000 | [diff] [blame] | 53 | if (logmode & LOGMODE_STDIO) { |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 54 | fflush_all(); |
Denis Vlasenko | 517413f | 2008-12-10 11:16:47 +0000 | [diff] [blame] | 55 | /* used = strlen(msg); - must be true already */ |
| 56 | msg[used++] = '\n'; |
| 57 | full_write(STDOUT_FILENO, msg, used); |
| 58 | } |
| 59 | |
| 60 | free(msg); |
Denis Vlasenko | 517413f | 2008-12-10 11:16:47 +0000 | [diff] [blame] | 61 | #endif |
Denis Vlasenko | 3da6aa8 | 2006-09-07 05:09:22 +0000 | [diff] [blame] | 62 | } |