]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
tree-wide: add FORMAT_TIMESTAMP_STYLE()
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Mon, 5 Jul 2021 19:41:59 +0000 (21:41 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Fri, 9 Jul 2021 09:03:36 +0000 (11:03 +0200)
src/basic/time-util.h
src/libsystemd/sd-bus/bus-dump.c
src/systemctl/systemctl-logind.c
src/systemctl/systemctl-show.c
src/test/test-calendarspec.c
src/test/test-date.c
src/timesync/wait-sync.c
src/tmpfiles/tmpfiles.c

index acca1d1a2f21f889d735f5296b17e9265d6c2332..a3f9f44797fc4db4e87c12baf42debc916151a83 100644 (file)
@@ -132,7 +132,8 @@ static inline char* format_timestamp(char *buf, size_t l, usec_t t) {
 #define FORMAT_TIMESTAMP_RELATIVE(t)                                    \
         format_timestamp_relative((char[FORMAT_TIMESTAMP_RELATIVE_MAX]){}, FORMAT_TIMESTAMP_RELATIVE_MAX, t)
 #define FORMAT_TIMESPAN(t, accuracy) format_timespan((char[FORMAT_TIMESPAN_MAX]){}, FORMAT_TIMESPAN_MAX, t, accuracy)
-
+#define FORMAT_TIMESTAMP_STYLE(t, style) \
+        format_timestamp_style((char[FORMAT_TIMESTAMP_MAX]){}, FORMAT_TIMESTAMP_MAX, t, style)
 
 int parse_timestamp(const char *t, usec_t *usec);
 
index d5d22b0d8c64f086cd97dd28ecc7dfcde02e1103..11149d8e5bfe33fe43081cf2691bd05c3047c3b2 100644 (file)
@@ -55,15 +55,11 @@ _public_ int sd_bus_message_dump(sd_bus_message *m, FILE *f, uint64_t flags) {
                 f = stdout;
 
         if (flags & SD_BUS_MESSAGE_DUMP_WITH_HEADER) {
-                char buf[FORMAT_TIMESTAMP_MAX];
-                const char *p;
                 usec_t ts = m->realtime;
 
                 if (ts == 0)
                         ts = now(CLOCK_REALTIME);
 
-                p = format_timestamp_style(buf, sizeof(buf), ts, TIMESTAMP_US_UTC);
-
                 fprintf(f,
                         "%s%s%s Type=%s%s%s  Endian=%c  Flags=%u  Version=%u",
                         m->header->type == SD_BUS_MESSAGE_METHOD_ERROR ? ansi_highlight_red() :
@@ -90,9 +86,7 @@ _public_ int sd_bus_message_dump(sd_bus_message *m, FILE *f, uint64_t flags) {
                 if (m->reply_cookie != 0)
                         fprintf(f, "  ReplyCookie=%" PRIu64, m->reply_cookie);
 
-                fprintf(f, "  Timestamp=\"%s\"", strna(p));
-
-                fputs("\n", f);
+                fprintf(f, "  Timestamp=\"%s\"\n", strna(FORMAT_TIMESTAMP_STYLE(ts, TIMESTAMP_US_UTC)));
 
                 if (m->sender)
                         fprintf(f, "  Sender=%s%s%s", ansi_highlight(), m->sender, ansi_normal());
index 28dd25b523dffb5e6d0887e39cbaaef9be1aa775..b1c0e13f2cd716bbf90f3e9392e33b4956800371 100644 (file)
@@ -296,7 +296,6 @@ int logind_schedule_shutdown(void) {
 
 #if ENABLE_LOGIND
         _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
-        char date[FORMAT_TIMESTAMP_MAX];
         const char *action;
         const char *log_action;
         sd_bus *bus;
@@ -340,7 +339,9 @@ int logind_schedule_shutdown(void) {
                 return log_warning_errno(r, "Failed to call ScheduleShutdown in logind, proceeding with immediate shutdown: %s", bus_error_message(&error, r));
 
         if (!arg_quiet)
-                log_info("%s scheduled for %s, use 'shutdown -c' to cancel.", log_action, format_timestamp_style(date, sizeof(date), arg_when, arg_timestamp_style));
+                log_info("%s scheduled for %s, use 'shutdown -c' to cancel.",
+                         log_action,
+                         FORMAT_TIMESTAMP_STYLE(arg_when, arg_timestamp_style));
         return 0;
 #else
         return log_error_errno(SYNTHETIC_ERRNO(ENOSYS),
index 3b5f2a432099517f63736e3391f169d3553c066b..621631c6010e2a23d618c6133a7b7af98555279a 100644 (file)
@@ -300,7 +300,6 @@ static void print_status_info(
                 UnitStatusInfo *i,
                 bool *ellipsized) {
 
-        char since2[FORMAT_TIMESTAMP_MAX];
         const char *s1, *s2, *active_on, *active_off, *on, *off, *ss, *fs;
         _cleanup_free_ char *formatted_path = NULL;
         ExecStatusInfo *p;
@@ -420,8 +419,7 @@ static void print_status_info(
                                                                             i->active_exit_timestamp;
 
         s1 = FORMAT_TIMESTAMP_RELATIVE(timestamp);
-        s2 = format_timestamp_style(since2, sizeof(since2), timestamp, arg_timestamp_style);
-
+        s2 = FORMAT_TIMESTAMP_STYLE(timestamp, arg_timestamp_style);
         if (s1)
                 printf(" since %s; %s\n", s2, s1);
         else if (s2)
@@ -442,23 +440,18 @@ static void print_status_info(
         }
 
         if (endswith(i->id, ".timer")) {
-                char tstamp2[FORMAT_TIMESTAMP_MAX];
-                const char *next_rel_time, *next_time;
-                dual_timestamp nw, next = {i->next_elapse_real,
-                                           i->next_elapse_monotonic};
+                const char *next_time;
+                dual_timestamp nw, next = {i->next_elapse_real, i->next_elapse_monotonic};
                 usec_t next_elapse;
 
-                printf("    Trigger: ");
-
                 dual_timestamp_get(&nw);
                 next_elapse = calc_next_elapse(&nw, &next);
-                next_rel_time = FORMAT_TIMESTAMP_RELATIVE(next_elapse);
-                next_time = format_timestamp_style(tstamp2, sizeof tstamp2, next_elapse, arg_timestamp_style);
+                next_time = FORMAT_TIMESTAMP_STYLE(next_elapse, arg_timestamp_style);
 
-                if (next_time && next_rel_time)
-                        printf("%s; %s\n", next_time, next_rel_time);
-                else
-                        printf("n/a\n");
+                printf("    Trigger: %s%s%s\n",
+                       strna(next_time),
+                       next_time ? "; " : "",
+                       next_time ? strna(FORMAT_TIMESTAMP_RELATIVE(next_elapse)) : "");
         }
 
         STRV_FOREACH(t, i->triggers) {
@@ -476,13 +469,13 @@ static void print_status_info(
         if (!i->condition_result && i->condition_timestamp > 0) {
                 UnitCondition *c;
                 int n = 0;
+                const char *rel;
 
-                s1 = FORMAT_TIMESTAMP_RELATIVE(i->condition_timestamp);
-                s2 = format_timestamp_style(since2, sizeof(since2), i->condition_timestamp, arg_timestamp_style);
-
+                rel = FORMAT_TIMESTAMP_RELATIVE(i->condition_timestamp);
                 printf("  Condition: start %scondition failed%s at %s%s%s\n",
                        ansi_highlight_yellow(), ansi_normal(),
-                       s2, s1 ? "; " : "", strempty(s1));
+                       FORMAT_TIMESTAMP_STYLE(i->condition_timestamp, arg_timestamp_style),
+                       rel ? "; " : "", strempty(rel));
 
                 LIST_FOREACH(conditions, c, i->conditions)
                         if (c->tristate < 0)
@@ -499,12 +492,13 @@ static void print_status_info(
         }
 
         if (!i->assert_result && i->assert_timestamp > 0) {
-                s1 = FORMAT_TIMESTAMP_RELATIVE(i->assert_timestamp);
-                s2 = format_timestamp_style(since2, sizeof(since2), i->assert_timestamp, arg_timestamp_style);
+                const char *rel;
 
+                rel = FORMAT_TIMESTAMP_RELATIVE(i->assert_timestamp);
                 printf("     Assert: start %sassertion failed%s at %s%s%s\n",
                        ansi_highlight_red(), ansi_normal(),
-                       s2, s1 ? "; " : "", strempty(s1));
+                       FORMAT_TIMESTAMP_STYLE(i->assert_timestamp, arg_timestamp_style),
+                       rel ? "; " : "", strempty(rel));
                 if (i->failed_assert_trigger)
                         printf("             none of the trigger assertions were met\n");
                 else if (i->failed_assert)
@@ -1255,13 +1249,10 @@ static int print_property(const char *name, const char *expected_value, sd_bus_m
                         if (r < 0)
                                 return bus_log_parse_error(r);
 
-                        while ((r = sd_bus_message_read(m, "(sst)", &base, &spec, &next_elapse)) > 0) {
-                                char timestamp[FORMAT_TIMESTAMP_MAX] = "n/a";
-
-                                (void) format_timestamp_style(timestamp, sizeof(timestamp), next_elapse, arg_timestamp_style);
+                        while ((r = sd_bus_message_read(m, "(sst)", &base, &spec, &next_elapse)) > 0)
                                 bus_print_property_valuef(name, expected_value, flags,
-                                                          "{ %s=%s ; next_elapse=%s }", base, spec, timestamp);
-                        }
+                                                          "{ %s=%s ; next_elapse=%s }", base, spec,
+                                                          FORMAT_TIMESTAMP_STYLE(next_elapse, arg_timestamp_style));
                         if (r < 0)
                                 return bus_log_parse_error(r);
 
@@ -1280,7 +1271,6 @@ static int print_property(const char *name, const char *expected_value, sd_bus_m
                                 return bus_log_parse_error(r);
 
                         while ((r = exec_status_info_deserialize(m, &info, is_ex_prop)) > 0) {
-                                char timestamp1[FORMAT_TIMESTAMP_MAX], timestamp2[FORMAT_TIMESTAMP_MAX];
                                 _cleanup_strv_free_ char **optv = NULL;
                                 _cleanup_free_ char *tt = NULL, *o = NULL;
 
@@ -1298,8 +1288,8 @@ static int print_property(const char *name, const char *expected_value, sd_bus_m
                                                                   strna(info.path),
                                                                   strna(tt),
                                                                   strna(o),
-                                                                  strna(format_timestamp_style(timestamp1, sizeof(timestamp1), info.start_timestamp, arg_timestamp_style)),
-                                                                  strna(format_timestamp_style(timestamp2, sizeof(timestamp2), info.exit_timestamp, arg_timestamp_style)),
+                                                                  strna(FORMAT_TIMESTAMP_STYLE(info.start_timestamp, arg_timestamp_style)),
+                                                                  strna(FORMAT_TIMESTAMP_STYLE(info.exit_timestamp, arg_timestamp_style)),
                                                                   info.pid,
                                                                   sigchld_code_to_string(info.code),
                                                                   info.status,
@@ -1311,8 +1301,8 @@ static int print_property(const char *name, const char *expected_value, sd_bus_m
                                                                   strna(info.path),
                                                                   strna(tt),
                                                                   yes_no(info.ignore),
-                                                                  strna(format_timestamp_style(timestamp1, sizeof(timestamp1), info.start_timestamp, arg_timestamp_style)),
-                                                                  strna(format_timestamp_style(timestamp2, sizeof(timestamp2), info.exit_timestamp, arg_timestamp_style)),
+                                                                  strna(FORMAT_TIMESTAMP_STYLE(info.start_timestamp, arg_timestamp_style)),
+                                                                  strna(FORMAT_TIMESTAMP_STYLE(info.exit_timestamp, arg_timestamp_style)),
                                                                   info.pid,
                                                                   sigchld_code_to_string(info.code),
                                                                   info.status,
@@ -2014,7 +2004,6 @@ static int show_all(
 }
 
 static int show_system_status(sd_bus *bus) {
-        char since2[FORMAT_TIMESTAMP_MAX];
         _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
         _cleanup_(machine_info_clear) struct machine_info mi = {};
         _cleanup_free_ char *hn = NULL;
@@ -2057,7 +2046,7 @@ static int show_system_status(sd_bus *bus) {
         printf("   Failed: %" PRIu32 " units\n", mi.n_failed_units);
 
         printf("    Since: %s; %s\n",
-               format_timestamp_style(since2, sizeof(since2), mi.timestamp, arg_timestamp_style),
+               FORMAT_TIMESTAMP_STYLE(mi.timestamp, arg_timestamp_style),
                FORMAT_TIMESTAMP_RELATIVE(mi.timestamp));
 
         printf("   CGroup: %s\n", mi.control_group ?: "/");
index d6ff7b7b218ab93f84d254a36ac56f24704c2469..5d680f9b9a469e9d2b250daebc2f73442789fc10 100644 (file)
@@ -36,7 +36,6 @@ static void _test_next(int line, const char *input, const char *new_tz, usec_t a
         CalendarSpec *c;
         usec_t u;
         char *old_tz;
-        char buf[FORMAT_TIMESTAMP_MAX];
         int r;
 
         old_tz = getenv("TZ");
@@ -55,7 +54,7 @@ static void _test_next(int line, const char *input, const char *new_tz, usec_t a
 
         u = after;
         r = calendar_spec_next_usec(c, after, &u);
-        log_info("At: %s", r < 0 ? strerror_safe(r) : format_timestamp_style(buf, sizeof buf, u, TIMESTAMP_US));
+        log_info("At: %s", r < 0 ? strerror_safe(r) : FORMAT_TIMESTAMP_STYLE(u, TIMESTAMP_US));
         if (expect != USEC_INFINITY)
                 assert_se(r >= 0 && u == expect);
         else
@@ -92,18 +91,17 @@ static void test_timestamp(void) {
 static void test_hourly_bug_4031(void) {
         CalendarSpec *c;
         usec_t n, u, w;
-        char buf[FORMAT_TIMESTAMP_MAX], zaf[FORMAT_TIMESTAMP_MAX];
         int r;
 
         assert_se(calendar_spec_from_string("hourly", &c) >= 0);
         n = now(CLOCK_REALTIME);
         assert_se((r = calendar_spec_next_usec(c, n, &u)) >= 0);
 
-        log_info("Now: %s (%"PRIu64")", format_timestamp_style(buf, sizeof buf, n, TIMESTAMP_US), n);
-        log_info("Next hourly: %s (%"PRIu64")", r < 0 ? strerror_safe(r) : format_timestamp_style(buf, sizeof buf, u, TIMESTAMP_US), u);
+        log_info("Now: %s (%"PRIu64")", FORMAT_TIMESTAMP_STYLE(n, TIMESTAMP_US), n);
+        log_info("Next hourly: %s (%"PRIu64")", r < 0 ? strerror_safe(r) : FORMAT_TIMESTAMP_STYLE(u, TIMESTAMP_US), u);
 
         assert_se((r = calendar_spec_next_usec(c, u, &w)) >= 0);
-        log_info("Next hourly: %s (%"PRIu64")", r < 0 ? strerror_safe(r) : format_timestamp_style(zaf, sizeof zaf, w, TIMESTAMP_US), w);
+        log_info("Next hourly: %s (%"PRIu64")", r < 0 ? strerror_safe(r) : FORMAT_TIMESTAMP_STYLE(w, TIMESTAMP_US), w);
 
         assert_se(n < u);
         assert_se(u <= n + USEC_PER_HOUR);
index 47b70967385554e6b73de64be07325dbe4e6d8f8..355f4e17de5d99cff148fa5c8ab27606453825b1 100644 (file)
@@ -15,12 +15,9 @@ static void test_should_pass(const char *p) {
         log_info("\"%s\" → \"%s\"", p, buf);
 
         assert_se(parse_timestamp(buf, &q) >= 0);
-        if (q != t) {
-                char tmp[FORMAT_TIMESTAMP_MAX];
-
+        if (q != t)
                 log_error("round-trip failed: \"%s\" → \"%s\"",
-                          buf, format_timestamp_style(tmp, sizeof(tmp), q, TIMESTAMP_US));
-        }
+                          buf, FORMAT_TIMESTAMP_STYLE(q, TIMESTAMP_US));
         assert_se(q == t);
 
         assert_se(format_timestamp_relative(buf_relative, sizeof(buf_relative), t));
index 2a9b113ff484128840d6b090666826b3a8926ea3..892746d5ee62be3380009fddecc85a0861220c01 100644 (file)
@@ -100,9 +100,7 @@ static int clock_state_update(
                 ClockState *sp,
                 sd_event *event) {
 
-        char buf[MAX((size_t)FORMAT_TIMESTAMP_MAX, STRLEN("unrepresentable"))];
         struct timex tx = {};
-        const char * ts;
         usec_t t;
         int r;
 
@@ -149,10 +147,9 @@ static int clock_state_update(
         if (tx.status & STA_NANO)
                 tx.time.tv_usec /= 1000;
         t = timeval_load(&tx.time);
-        ts = format_timestamp_style(buf, sizeof(buf), t, TIMESTAMP_US_UTC);
-        if (!ts)
-                strcpy(buf, "unrepresentable");
-        log_info("adjtime state %d status %x time %s", sp->adjtime_state, tx.status, ts);
+
+        log_info("adjtime state %d status %x time %s", sp->adjtime_state, tx.status,
+                 FORMAT_TIMESTAMP_STYLE(t, TIMESTAMP_US_UTC) ?: "unrepresentable");
 
         sp->has_watchfile = access("/run/systemd/timesync/synchronized", F_OK) >= 0;
         if (sp->has_watchfile)
index b32b709ef5b055f2402531a8144ff4cc17dff7cb..41e3c934beef57d4b73fb7b230221ee155a6ecb3 100644 (file)
@@ -544,22 +544,20 @@ static bool needs_cleanup(
                 bool is_dir) {
 
         if (FLAGS_SET(age_by, AGE_BY_MTIME) && mtime != NSEC_INFINITY && mtime >= cutoff) {
-                char a[FORMAT_TIMESTAMP_MAX];
                 /* Follows spelling in stat(1). */
                 log_debug("%s \"%s\": modify time %s is too new.",
                           is_dir ? "Directory" : "File",
                           sub_path,
-                          format_timestamp_style(a, sizeof(a), mtime / NSEC_PER_USEC, TIMESTAMP_US));
+                          FORMAT_TIMESTAMP_STYLE(mtime / NSEC_PER_USEC, TIMESTAMP_US));
 
                 return false;
         }
 
         if (FLAGS_SET(age_by, AGE_BY_ATIME) && atime != NSEC_INFINITY && atime >= cutoff) {
-                char a[FORMAT_TIMESTAMP_MAX];
                 log_debug("%s \"%s\": access time %s is too new.",
                           is_dir ? "Directory" : "File",
                           sub_path,
-                          format_timestamp_style(a, sizeof(a), atime / NSEC_PER_USEC, TIMESTAMP_US));
+                          FORMAT_TIMESTAMP_STYLE(atime / NSEC_PER_USEC, TIMESTAMP_US));
 
                 return false;
         }
@@ -569,21 +567,19 @@ static bool needs_cleanup(
          * by default for directories, because we change it when deleting.
          */
         if (FLAGS_SET(age_by, AGE_BY_CTIME) && ctime != NSEC_INFINITY && ctime >= cutoff) {
-                char a[FORMAT_TIMESTAMP_MAX];
                 log_debug("%s \"%s\": change time %s is too new.",
                           is_dir ? "Directory" : "File",
                           sub_path,
-                          format_timestamp_style(a, sizeof(a), ctime / NSEC_PER_USEC, TIMESTAMP_US));
+                          FORMAT_TIMESTAMP_STYLE(ctime / NSEC_PER_USEC, TIMESTAMP_US));
 
                 return false;
         }
 
         if (FLAGS_SET(age_by, AGE_BY_BTIME) && btime != NSEC_INFINITY && btime >= cutoff) {
-                char a[FORMAT_TIMESTAMP_MAX];
                 log_debug("%s \"%s\": birth time %s is too new.",
                           is_dir ? "Directory" : "File",
                           sub_path,
-                          format_timestamp_style(a, sizeof(a), btime / NSEC_PER_USEC, TIMESTAMP_US));
+                          FORMAT_TIMESTAMP_STYLE(btime / NSEC_PER_USEC, TIMESTAMP_US));
 
                 return false;
         }
@@ -810,13 +806,12 @@ static int dir_cleanup(
 
 finish:
         if (deleted) {
-                char a[FORMAT_TIMESTAMP_MAX], m[FORMAT_TIMESTAMP_MAX];
                 struct timespec ts[2];
 
                 log_debug("Restoring access and modification time on \"%s\": %s, %s",
                           p,
-                          format_timestamp_style(a, sizeof(a), self_atime_nsec / NSEC_PER_USEC, TIMESTAMP_US),
-                          format_timestamp_style(m, sizeof(m), self_mtime_nsec / NSEC_PER_USEC, TIMESTAMP_US));
+                          FORMAT_TIMESTAMP_STYLE(self_atime_nsec / NSEC_PER_USEC, TIMESTAMP_US),
+                          FORMAT_TIMESTAMP_STYLE(self_mtime_nsec / NSEC_PER_USEC, TIMESTAMP_US));
 
                 timespec_store_nsec(ts + 0, self_atime_nsec);
                 timespec_store_nsec(ts + 1, self_mtime_nsec);
@@ -2503,7 +2498,6 @@ static char *age_by_to_string(AgeBy ab, bool is_dir) {
 }
 
 static int clean_item_instance(Item *i, const char* instance) {
-        char timestamp[FORMAT_TIMESTAMP_MAX];
         _cleanup_closedir_ DIR *d = NULL;
         STRUCT_STATX_DEFINE(sx);
         int mountpoint, r;
@@ -2562,7 +2556,7 @@ static int clean_item_instance(Item *i, const char* instance) {
                 log_debug("Cleanup threshold for %s \"%s\" is %s; age-by: %s%s",
                           mountpoint ? "mount point" : "directory",
                           instance,
-                          format_timestamp_style(timestamp, sizeof(timestamp), cutoff, TIMESTAMP_US),
+                          FORMAT_TIMESTAMP_STYLE(cutoff, TIMESTAMP_US),
                           ab_f, ab_d);
         }