summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuca Boccassi <luca.boccassi@microsoft.com>2020-12-09 14:30:32 +0000
committerGitHub <noreply@github.com>2020-12-09 14:30:32 +0000
commit0727a75a3bd321dce2e8f8ed66f845c901cd9cf5 (patch)
tree1dc4c920db9c0b1879556ae518df18ab6808ddd2
parent141d3a14d484a747490d0409b6ca45ad03f7d812 (diff)
parent13ee93978479b80e980d8551c689a7087bf1817e (diff)
downloadsystemd-0727a75a3bd321dce2e8f8ed66f845c901cd9cf5.tar.gz
Merge pull request #17911 from yuwata/log-cli-program-use-journal
log: open journal when cli program run in a service unit
-rw-r--r--src/basic/log.c21
1 files changed, 11 insertions, 10 deletions
diff --git a/src/basic/log.c b/src/basic/log.c
index d4054cf46a..dd9c7db18b 100644
--- a/src/basic/log.c
+++ b/src/basic/log.c
@@ -51,7 +51,7 @@ static int journal_fd = -1;
static bool syslog_is_stream = false;
-static bool show_color = false;
+static int show_color = -1; /* tristate */
static bool show_location = false;
static bool show_time = false;
static bool show_tid = false;
@@ -387,11 +387,10 @@ static int write_to_console(
iovec[n++] = IOVEC_MAKE_STRING(prefix);
}
- if (show_time) {
- if (format_timestamp(header_time, sizeof(header_time), now(CLOCK_REALTIME))) {
- iovec[n++] = IOVEC_MAKE_STRING(header_time);
- iovec[n++] = IOVEC_MAKE_STRING(" ");
- }
+ if (show_time &&
+ format_timestamp(header_time, sizeof(header_time), now(CLOCK_REALTIME))) {
+ iovec[n++] = IOVEC_MAKE_STRING(header_time);
+ iovec[n++] = IOVEC_MAKE_STRING(" ");
}
if (show_tid) {
@@ -399,12 +398,12 @@ static int write_to_console(
iovec[n++] = IOVEC_MAKE_STRING(tid_string);
}
- if (show_color)
+ if (log_get_show_color())
get_log_colors(LOG_PRI(level), &on, &off, NULL);
if (show_location) {
const char *lon = "", *loff = "";
- if (show_color) {
+ if (log_get_show_color()) {
lon = ANSI_HIGHLIGHT_YELLOW4;
loff = ANSI_NORMAL;
}
@@ -1222,7 +1221,7 @@ void log_show_color(bool b) {
}
bool log_get_show_color(void) {
- return show_color;
+ return show_color > 0; /* Defaults to false. */
}
void log_show_location(bool b) {
@@ -1485,7 +1484,9 @@ void log_setup_service(void) {
void log_setup_cli(void) {
/* Sets up logging the way it is most appropriate for running a program as a CLI utility. */
- log_show_color(true);
+ log_set_target(LOG_TARGET_AUTO);
log_parse_environment_cli();
(void) log_open();
+ if (log_on_console() && show_color < 0)
+ log_show_color(true);
}