1
0
mirror of https://github.com/systemd/systemd.git synced 2025-03-31 14:50:15 +03:00

Merge pull request #26849 from DaanDeMeyer/fwd-colors

journald-console: Add colors when forwarding to console
This commit is contained in:
Daan De Meyer 2023-03-17 12:02:35 +01:00 committed by GitHub
commit dfb487ef74
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 3 deletions

View File

@ -49,6 +49,7 @@ static volatile unsigned cached_columns = 0;
static volatile unsigned cached_lines = 0;
static volatile int cached_on_tty = -1;
static volatile int cached_on_dev_null = -1;
static volatile int cached_color_mode = _COLOR_INVALID;
static volatile int cached_underline_enabled = -1;
@ -894,6 +895,7 @@ void reset_terminal_feature_caches(void) {
cached_color_mode = _COLOR_INVALID;
cached_underline_enabled = -1;
cached_on_tty = -1;
cached_on_dev_null = -1;
}
bool on_tty(void) {
@ -1224,6 +1226,20 @@ int open_terminal_in_namespace(pid_t pid, const char *name, int mode) {
return receive_one_fd(pair[0], 0);
}
static bool on_dev_null(void) {
struct stat dst, ost, est;
if (cached_on_dev_null >= 0)
return cached_on_dev_null;
if (stat("/dev/null", &dst) < 0 || fstat(STDOUT_FILENO, &ost) < 0 || fstat(STDERR_FILENO, &est) < 0)
cached_on_dev_null = false;
else
cached_on_dev_null = stat_inode_same(&dst, &ost) && stat_inode_same(&dst, &est);
return cached_on_dev_null;
}
static bool getenv_terminal_is_dumb(void) {
const char *e;
@ -1235,7 +1251,7 @@ static bool getenv_terminal_is_dumb(void) {
}
bool terminal_is_dumb(void) {
if (!on_tty())
if (!on_tty() && !on_dev_null())
return true;
return getenv_terminal_is_dumb();

View File

@ -38,13 +38,13 @@ void server_forward_console(
const char *message,
const struct ucred *ucred) {
struct iovec iovec[5];
struct iovec iovec[7];
struct timespec ts;
char tbuf[STRLEN("[] ") + DECIMAL_STR_MAX(ts.tv_sec) + DECIMAL_STR_MAX(ts.tv_nsec)-3 + 1];
char header_pid[STRLEN("[]: ") + DECIMAL_STR_MAX(pid_t)];
_cleanup_free_ char *ident_buf = NULL;
_cleanup_close_ int fd = -EBADF;
const char *tty;
const char *tty, *color_on = "", *color_off = "";
int n = 0;
assert(s);
@ -81,8 +81,12 @@ void server_forward_console(
iovec[n++] = IOVEC_MAKE_STRING(": ");
}
get_log_colors(LOG_PRI(priority), &color_on, &color_off, NULL);
/* Fourth: message */
iovec[n++] = IOVEC_MAKE_STRING(color_on);
iovec[n++] = IOVEC_MAKE_STRING(message);
iovec[n++] = IOVEC_MAKE_STRING(color_off);
iovec[n++] = IOVEC_MAKE_STRING("\n");
tty = s->tty_path ?: "/dev/console";

View File

@ -47,6 +47,7 @@ SystemCallArchitectures=native
SystemCallErrorNumber=EPERM
SystemCallFilter=@system-service
Type=notify
PassEnvironment=TERM
{{SERVICE_WATCHDOG}}
# In case you're wondering why CAP_SYS_PTRACE is needed, access to