]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
analyze: split out "timestamp" verb into its own .c/.h files
authorLennart Poettering <lennart@poettering.net>
Mon, 21 Feb 2022 09:14:57 +0000 (10:14 +0100)
committerLennart Poettering <lennart@poettering.net>
Mon, 21 Feb 2022 16:22:23 +0000 (17:22 +0100)
src/analyze/analyze-timestamp.c [new file with mode: 0644]
src/analyze/analyze-timestamp.h [new file with mode: 0644]
src/analyze/analyze.c
src/analyze/meson.build

diff --git a/src/analyze/analyze-timestamp.c b/src/analyze/analyze-timestamp.c
new file mode 100644 (file)
index 0000000..2600aca
--- /dev/null
@@ -0,0 +1,95 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+
+#include "analyze.h"
+#include "analyze-timestamp.h"
+#include "format-table.h"
+#include "terminal-util.h"
+
+static int test_timestamp_one(const char *p) {
+        _cleanup_(table_unrefp) Table *table = NULL;
+        TableCell *cell;
+        usec_t usec;
+        int r;
+
+        r = parse_timestamp(p, &usec);
+        if (r < 0) {
+                log_error_errno(r, "Failed to parse \"%s\": %m", p);
+                time_parsing_hint(p, /* calendar= */ true, /* timestamp= */ false, /* timespan= */ true);
+                return r;
+        }
+
+        table = table_new("name", "value");
+        if (!table)
+                return log_oom();
+
+        table_set_header(table, false);
+
+        assert_se(cell = table_get_cell(table, 0, 0));
+        r = table_set_ellipsize_percent(table, cell, 100);
+        if (r < 0)
+                return r;
+
+        r = table_set_align_percent(table, cell, 100);
+        if (r < 0)
+                return r;
+
+        assert_se(cell = table_get_cell(table, 0, 1));
+        r = table_set_ellipsize_percent(table, cell, 100);
+        if (r < 0)
+                return r;
+
+        r = table_add_many(table,
+                           TABLE_STRING, "Original form:",
+                           TABLE_STRING, p,
+                           TABLE_STRING, "Normalized form:",
+                           TABLE_TIMESTAMP, usec,
+                           TABLE_SET_COLOR, ansi_highlight_blue());
+        if (r < 0)
+                return table_log_add_error(r);
+
+        if (!in_utc_timezone()) {
+                r = table_add_many(table,
+                                   TABLE_STRING, "(in UTC):",
+                                   TABLE_TIMESTAMP_UTC, usec);
+                if (r < 0)
+                        return table_log_add_error(r);
+        }
+
+        r = table_add_cell(table, NULL, TABLE_STRING, "UNIX seconds:");
+        if (r < 0)
+                return table_log_add_error(r);
+
+        if (usec % USEC_PER_SEC == 0)
+                r = table_add_cell_stringf(table, NULL, "@%"PRI_USEC,
+                                           usec / USEC_PER_SEC);
+        else
+                r = table_add_cell_stringf(table, NULL, "@%"PRI_USEC".%06"PRI_USEC"",
+                                           usec / USEC_PER_SEC,
+                                           usec % USEC_PER_SEC);
+        if (r < 0)
+                return r;
+
+        r = table_add_many(table,
+                           TABLE_STRING, "From now:",
+                           TABLE_TIMESTAMP_RELATIVE, usec);
+        if (r < 0)
+                return table_log_add_error(r);
+
+        return table_print(table, NULL);
+}
+
+int test_timestamp(int argc, char *argv[], void *userdata) {
+        int ret = 0, r;
+        char **p;
+
+        STRV_FOREACH(p, strv_skip(argv, 1)) {
+                r = test_timestamp_one(*p);
+                if (ret == 0 && r < 0)
+                        ret = r;
+
+                if (*(p + 1))
+                        putchar('\n');
+        }
+
+        return ret;
+}
diff --git a/src/analyze/analyze-timestamp.h b/src/analyze/analyze-timestamp.h
new file mode 100644 (file)
index 0000000..583d0c7
--- /dev/null
@@ -0,0 +1,4 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+#pragma once
+
+int test_timestamp(int argc, char *argv[], void *userdata);
index 4f26d0c9c79b260640b5cfcfee1e89610ca3c39f..c3980928cba383984444a27ad67f81308d1f7dfc 100644 (file)
@@ -17,6 +17,7 @@
 #include "analyze-elf.h"
 #include "analyze-security.h"
 #include "analyze-timespan.h"
+#include "analyze-timestamp.h"
 #include "analyze-verify.h"
 #include "bus-error.h"
 #include "bus-locator.h"
@@ -2042,95 +2043,6 @@ void time_parsing_hint(const char *p, bool calendar, bool timestamp, bool timesp
                            "Use 'systemd-analyze timespan \"%s\"' instead?", p);
 }
 
-static int test_timestamp_one(const char *p) {
-        _cleanup_(table_unrefp) Table *table = NULL;
-        TableCell *cell;
-        usec_t usec;
-        int r;
-
-        r = parse_timestamp(p, &usec);
-        if (r < 0) {
-                log_error_errno(r, "Failed to parse \"%s\": %m", p);
-                time_parsing_hint(p, /* calendar= */ true, /* timestamp= */ false, /* timespan= */ true);
-                return r;
-        }
-
-        table = table_new("name", "value");
-        if (!table)
-                return log_oom();
-
-        table_set_header(table, false);
-
-        assert_se(cell = table_get_cell(table, 0, 0));
-        r = table_set_ellipsize_percent(table, cell, 100);
-        if (r < 0)
-                return r;
-
-        r = table_set_align_percent(table, cell, 100);
-        if (r < 0)
-                return r;
-
-        assert_se(cell = table_get_cell(table, 0, 1));
-        r = table_set_ellipsize_percent(table, cell, 100);
-        if (r < 0)
-                return r;
-
-        r = table_add_many(table,
-                           TABLE_STRING, "Original form:",
-                           TABLE_STRING, p,
-                           TABLE_STRING, "Normalized form:",
-                           TABLE_TIMESTAMP, usec,
-                           TABLE_SET_COLOR, ansi_highlight_blue());
-        if (r < 0)
-                return table_log_add_error(r);
-
-        if (!in_utc_timezone()) {
-                r = table_add_many(table,
-                                   TABLE_STRING, "(in UTC):",
-                                   TABLE_TIMESTAMP_UTC, usec);
-                if (r < 0)
-                        return table_log_add_error(r);
-        }
-
-        r = table_add_cell(table, NULL, TABLE_STRING, "UNIX seconds:");
-        if (r < 0)
-                return table_log_add_error(r);
-
-        if (usec % USEC_PER_SEC == 0)
-                r = table_add_cell_stringf(table, NULL, "@%"PRI_USEC,
-                                           usec / USEC_PER_SEC);
-        else
-                r = table_add_cell_stringf(table, NULL, "@%"PRI_USEC".%06"PRI_USEC"",
-                                           usec / USEC_PER_SEC,
-                                           usec % USEC_PER_SEC);
-        if (r < 0)
-                return r;
-
-        r = table_add_many(table,
-                           TABLE_STRING, "From now:",
-                           TABLE_TIMESTAMP_RELATIVE, usec);
-        if (r < 0)
-                return table_log_add_error(r);
-
-        return table_print(table, NULL);
-}
-
-static int test_timestamp(int argc, char *argv[], void *userdata) {
-        int ret = 0, r;
-        char **p;
-
-        STRV_FOREACH(p, strv_skip(argv, 1)) {
-                r = test_timestamp_one(*p);
-                if (ret == 0 && r < 0)
-                        ret = r;
-
-                if (*(p + 1))
-                        putchar('\n');
-        }
-
-        return ret;
-}
-
 static int test_calendar_one(usec_t n, const char *p) {
         _cleanup_(calendar_spec_freep) CalendarSpec *spec = NULL;
         _cleanup_(table_unrefp) Table *table = NULL;
index df5fa216cca3295c954875491fb162c412464381..0988f4a57f5e03d45b09ec96c42779d6dfaaa65b 100644 (file)
@@ -9,6 +9,8 @@ systemd_analyze_sources = files('''
         analyze-security.h
         analyze-timespan.c
         analyze-timespan.h
+        analyze-timestamp.c
+        analyze-timestamp.h
         analyze-verify.c
         analyze-verify.h
         analyze.c