[systemd-devel] [RFC PATCH 3/3] journalctl: allow the user to specify the file to use

Zbigniew Jędrzejewski-Szmek zbyszek at in.waw.pl
Mon Mar 18 18:29:11 PDT 2013


This is useful for debugging and feels pretty natural. For example
answering the question "is this big .journal file worth keeping?"
is made easier.
---
 man/journalctl.xml       | 31 ++++++++++++++++++++++---------
 src/journal/journalctl.c | 33 ++++++++++++++++++++++++++++++++-
 2 files changed, 54 insertions(+), 10 deletions(-)

diff --git a/man/journalctl.xml b/man/journalctl.xml
index 8883da2..02b28a6 100644
--- a/man/journalctl.xml
+++ b/man/journalctl.xml
@@ -434,15 +434,28 @@
                         </varlistentry>
 
                         <varlistentry>
-                                <term><option>-D</option></term>
-                                <term><option>--directory=</option></term>
-
-                                <listitem><para>Takes a
-                                directory path as argument. If
-                                specified journalctl will operate on the
-                                specified journal directory instead of
-                                the default runtime and system journal
-                                paths.</para></listitem>
+                                <term><option>-D <replaceable>DIR</replaceable></option></term>
+                                <term><option>--directory=<replaceable>DIR</replaceable></option></term>
+
+                                <listitem><para>Takes a directory path
+                                as argument. If specified journalctl
+                                will operate on the specified journal
+                                directory
+                                <replaceable>DIR</replaceable> instead
+                                of the default runtime and system
+                                journal paths.</para></listitem>
+                        </varlistentry>
+
+                        <varlistentry>
+                                <term><option>--file=<replaceable>FILE</replaceable></option></term>
+
+                                <listitem><para>Takes a file path as
+                                argument. If specified journalctl will
+                                operate on the specified journal file
+                                <replaceable>FILE</replaceable>
+                                instead of the default runtime and
+                                system journal paths. May be specified
+                                multiple times.</para></listitem>
                         </varlistentry>
 
                         <varlistentry>
diff --git a/src/journal/journalctl.c b/src/journal/journalctl.c
index a6ad055..1f707cf 100644
--- a/src/journal/journalctl.c
+++ b/src/journal/journalctl.c
@@ -69,6 +69,7 @@ static bool arg_merge = false;
 static bool arg_this_boot = false;
 static const char *arg_cursor = NULL;
 static const char *arg_directory = NULL;
+static char **arg_files = NULL;
 static int arg_priorities = 0xFF;
 static const char *arg_verify_key = NULL;
 #ifdef HAVE_GCRYPT
@@ -119,6 +120,7 @@ static int help(void) {
                "     --no-pager          Do not pipe output into a pager\n"
                "  -m --merge             Show entries from all available journals\n"
                "  -D --directory=PATH    Show journal files from directory\n"
+               "     --file=PATH         Show journal file\n"
 #ifdef HAVE_GCRYPT
                "     --interval=TIME     Time interval for changing the FSS sealing key\n"
                "     --verify-key=KEY    Specify FSS verification key\n"
@@ -151,6 +153,7 @@ static int parse_argv(int argc, char *argv[]) {
                 ARG_HEADER,
                 ARG_FULL,
                 ARG_SETUP_KEYS,
+                ARG_FILE,
                 ARG_INTERVAL,
                 ARG_VERIFY,
                 ARG_VERIFY_KEY,
@@ -178,6 +181,7 @@ static int parse_argv(int argc, char *argv[]) {
                 { "merge",        no_argument,       NULL, 'm'              },
                 { "this-boot",    no_argument,       NULL, 'b'              },
                 { "directory",    required_argument, NULL, 'D'              },
+                { "file",         required_argument, NULL, ARG_FILE         },
                 { "header",       no_argument,       NULL, ARG_HEADER       },
                 { "priority",     required_argument, NULL, 'p'              },
                 { "setup-keys",   no_argument,       NULL, ARG_SETUP_KEYS   },
@@ -309,6 +313,14 @@ static int parse_argv(int argc, char *argv[]) {
                         arg_directory = optarg;
                         break;
 
+                case ARG_FILE:
+                        r = strv_extend(&arg_files, optarg);
+                        if (r < 0) {
+                                log_error("Failed to extend set: %s", strerror(-r));
+                                return r;
+                        }
+                        break;
+
                 case 'c':
                         arg_cursor = optarg;
                         break;
@@ -462,6 +474,11 @@ static int parse_argv(int argc, char *argv[]) {
         if (arg_follow && !arg_no_tail && arg_lines < 0)
                 arg_lines = 10;
 
+        if (arg_directory && arg_files) {
+                log_error("Please specify either -D/--directory= or --files=, not both.");
+                return -EINVAL;
+        }
+
         if (arg_since_set && arg_until_set && arg_since > arg_until) {
                 log_error("--since= must be before --until=.");
                 return -EINVAL;
@@ -902,6 +919,7 @@ int main(int argc, char *argv[]) {
         sd_id128_t previous_boot_id;
         bool previous_boot_id_valid = false, first_line = true;
         int n_shown = 0;
+        char **file;
 
         setlocale(LC_ALL, "");
         log_parse_environment();
@@ -941,13 +959,26 @@ int main(int argc, char *argv[]) {
 
         if (arg_directory)
                 r = sd_journal_open_directory(&j, arg_directory, 0);
+        else if (arg_files)
+                r = sd_journal_open(&j, SD_JOURNAL_WITHOUT_FILES);
         else
                 r = sd_journal_open(&j, arg_merge ? 0 : SD_JOURNAL_LOCAL_ONLY);
         if (r < 0) {
-                log_error("Failed to open journal: %s", strerror(-r));
+                log_error("Failed to open %s: %s",
+                          arg_directory ? arg_directory : "journal",
+                          strerror(-r));
                 return EXIT_FAILURE;
         }
 
+        STRV_FOREACH(file, arg_files) {
+                r = sd_journal_add_file(j, *file);
+                if (r < 0) {
+                        log_error("Failed to add file %s: %s",
+                                  *file, strerror(-r));
+                        goto finish;
+                }
+        }
+
         if (arg_action == ACTION_VERIFY) {
                 r = verify(j);
                 goto finish;
-- 
1.8.1.4



More information about the systemd-devel mailing list