2 This file is part of systemd.
4 Copyright 2014 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 #include "alloc-util.h"
23 #include "parse-util.h"
24 #include "show-status.h"
25 #include "string-util.h"
26 #include "terminal-util.h"
29 int parse_show_status(const char *v
, ShowStatus
*ret
) {
35 if (streq(v
, "auto")) {
36 *ret
= SHOW_STATUS_AUTO
;
44 *ret
= r
? SHOW_STATUS_YES
: SHOW_STATUS_NO
;
48 int status_vprintf(const char *status
, bool ellipse
, bool ephemeral
, const char *format
, va_list ap
) {
49 static const char status_indent
[] = " "; /* "[" STATUS "] " */
50 _cleanup_free_
char *s
= NULL
;
51 _cleanup_close_
int fd
= -1;
52 struct iovec iovec
[6] = {};
54 static bool prev_ephemeral
;
58 /* This is independent of logging, as status messages are
59 * optional and go exclusively to the console. */
61 if (vasprintf(&s
, format
, ap
) < 0)
64 fd
= open_terminal("/dev/console", O_WRONLY
|O_NOCTTY
|O_CLOEXEC
);
77 sl
= status
? sizeof(status_indent
)-1 : 0;
83 e
= ellipsize(s
, emax
, 50);
91 IOVEC_SET_STRING(iovec
[n
++], "\r" ANSI_ERASE_TO_END_OF_LINE
);
92 prev_ephemeral
= ephemeral
;
95 if (!isempty(status
)) {
96 IOVEC_SET_STRING(iovec
[n
++], "[");
97 IOVEC_SET_STRING(iovec
[n
++], status
);
98 IOVEC_SET_STRING(iovec
[n
++], "] ");
100 IOVEC_SET_STRING(iovec
[n
++], status_indent
);
103 IOVEC_SET_STRING(iovec
[n
++], s
);
105 IOVEC_SET_STRING(iovec
[n
++], "\n");
107 if (writev(fd
, iovec
, n
) < 0)
113 int status_printf(const char *status
, bool ellipse
, bool ephemeral
, const char *format
, ...) {
119 va_start(ap
, format
);
120 r
= status_vprintf(status
, ellipse
, ephemeral
, format
, ap
);