chiark / gitweb /
systemd-delta: Only print colors when on a tty
[elogind.git] / src / journal / journalctl.c
index 2f043a48d544f25af757e87e511c0d6caeccec52..feea6bf19e3bb9f81e0f73866b6bb73ea6b7cace 100644 (file)
@@ -45,6 +45,7 @@
 #include "logs-show.h"
 #include "util.h"
 #include "path-util.h"
+#include "fileio.h"
 #include "build.h"
 #include "pager.h"
 #include "logs-show.h"
@@ -133,7 +134,7 @@ static int help(void) {
                "  -n --lines[=INTEGER]     Number of journal entries to show\n"
                "     --no-tail             Show all lines, even in follow mode\n"
                "  -r --reverse             Show the newest entries first\n"
-               "  -o --output=STRING       Change journal output mode (short, short-monotonic,\n"
+               "  -o --output=STRING       Change journal output mode (short, short-monotonic, short-iso\n"
                "                           verbose, export, json, json-pretty, json-sse, cat)\n"
                "  -x --catalog             Add message explanations where available\n"
                "  -l --full                Do not ellipsize fields\n"
@@ -627,8 +628,9 @@ static int add_matches(sd_journal *j, char **args) {
                 if (streq(*i, "+"))
                         r = sd_journal_add_disjunction(j);
                 else if (path_is_absolute(*i)) {
-                        _cleanup_free_ char *p, *t = NULL;
+                        _cleanup_free_ char *p, *t = NULL, *t2 = NULL;
                         const char *path;
+                        _cleanup_free_ char *interpreter = NULL;
                         struct stat st;
 
                         p = canonicalize_file_name(*i);
@@ -639,9 +641,27 @@ static int add_matches(sd_journal *j, char **args) {
                                 return -errno;
                         }
 
-                        if (S_ISREG(st.st_mode) && (0111 & st.st_mode))
-                                t = strappend("_EXE=", path);
-                        else if (S_ISCHR(st.st_mode))
+                        if (S_ISREG(st.st_mode) && (0111 & st.st_mode)) {
+                                if (executable_is_script(path, &interpreter) > 0) {
+                                        _cleanup_free_ char *comm;
+
+                                        comm = strndup(path_get_file_name(path), 15);
+                                        if (!comm)
+                                                return log_oom();
+
+                                        t = strappend("_COMM=", comm);
+
+                                        /* Append _EXE only if the interpreter is not a link.
+                                           Otherwise it might be outdated often. */
+                                        if (lstat(interpreter, &st) == 0 &&
+                                            !S_ISLNK(st.st_mode)) {
+                                                t2 = strappend("_EXE=", interpreter);
+                                                if (!t2)
+                                                        return log_oom();
+                                        }
+                                } else
+                                        t = strappend("_EXE=", path);
+                        } else if (S_ISCHR(st.st_mode))
                                 asprintf(&t, "_KERNEL_DEVICE=c%u:%u", major(st.st_rdev), minor(st.st_rdev));
                         else if (S_ISBLK(st.st_mode))
                                 asprintf(&t, "_KERNEL_DEVICE=b%u:%u", major(st.st_rdev), minor(st.st_rdev));
@@ -654,6 +674,8 @@ static int add_matches(sd_journal *j, char **args) {
                                 return log_oom();
 
                         r = sd_journal_add_match(j, t, 0);
+                        if (t2)
+                                r = sd_journal_add_match(j, t2, 0);
                 } else
                         r = sd_journal_add_match(j, *i, 0);
 
@@ -1583,14 +1605,13 @@ int main(int argc, char *argv[]) {
 
                         if (!arg_merge) {
                                 sd_id128_t boot_id;
-                                const char *color_on = on_tty() ? ANSI_HIGHLIGHT_ON : "",
-                                           *color_off = on_tty() ? ANSI_HIGHLIGHT_OFF : "";
 
                                 r = sd_journal_get_monotonic_usec(j, NULL, &boot_id);
                                 if (r >= 0) {
                                         if (previous_boot_id_valid &&
                                             !sd_id128_equal(boot_id, previous_boot_id))
-                                                printf("%s-- Reboot --%s\n", color_on, color_off);
+                                                printf("%s-- Reboot --%s\n",
+                                                       ansi_highlight(), ansi_highlight_off());
 
                                         previous_boot_id = boot_id;
                                         previous_boot_id_valid = true;