Skip to content

Commit

Permalink
Merge pull request #17684 from opensourcerouting/fix/json_time_no_new…
Browse files Browse the repository at this point in the history
…line

bgpd, lib: Use frrstr_time() when using ctime_r()
  • Loading branch information
Jafaral authored Dec 22, 2024
2 parents 0fbda45 + 8f5821e commit e62c2f1
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 26 deletions.
4 changes: 1 addition & 3 deletions bgpd/bgp_labelpool.c
Original file line number Diff line number Diff line change
Expand Up @@ -1125,7 +1125,6 @@ static void show_bgp_nexthop_label_afi(struct vty *vty, afi_t afi,
struct bgp_path_info *path;
struct bgp *bgp_path;
struct bgp_table *table;
time_t tbuf;

vty_out(vty, "Current BGP label nexthop cache for %s, VRF %s\n",
afi2str(afi), bgp->name_pretty);
Expand All @@ -1146,8 +1145,7 @@ static void show_bgp_nexthop_label_afi(struct vty *vty, afi_t afi,
vty_out(vty, " if %s\n",
ifindex2ifname(iter->nh->ifindex,
iter->nh->vrf_id));
tbuf = time(NULL) - (monotime(NULL) - iter->last_update);
vty_out(vty, " Last update: %s", ctime_r(&tbuf, buf));
vty_out(vty, " Last update: %s", time_to_string(iter->last_update, buf));
if (!detail)
continue;
vty_out(vty, " Paths:\n");
Expand Down
4 changes: 1 addition & 3 deletions bgpd/bgp_mplsvpn.c
Original file line number Diff line number Diff line change
Expand Up @@ -4395,7 +4395,6 @@ static void show_bgp_mplsvpn_nh_label_bind_internal(struct vty *vty,
struct bgp_path_info *path;
struct bgp *bgp_path;
struct bgp_table *table;
time_t tbuf;
char buf[32];

vty_out(vty, "Current BGP mpls-vpn nexthop label bind cache, %s\n",
Expand All @@ -4413,8 +4412,7 @@ static void show_bgp_mplsvpn_nh_label_bind_internal(struct vty *vty,
vty_out(vty, " interface %s\n",
ifindex2ifname(iter->nh->ifindex,
iter->nh->vrf_id));
tbuf = time(NULL) - (monotime(NULL) - iter->last_update);
vty_out(vty, " Last update: %s", ctime_r(&tbuf, buf));
vty_out(vty, " Last update: %s", time_to_string(iter->last_update, buf));
if (!detail)
continue;
vty_out(vty, " Paths:\n");
Expand Down
4 changes: 2 additions & 2 deletions bgpd/bgp_nexthop.c
Original file line number Diff line number Diff line change
Expand Up @@ -1079,14 +1079,14 @@ static void bgp_show_nexthop(struct vty *vty, struct bgp *bgp,
json_last_update = json_object_new_object();
json_object_int_add(json_last_update, "epoch", tbuf);
json_object_string_add(json_last_update, "string",
ctime_r(&tbuf, timebuf));
time_to_string_json(bnc->last_update, timebuf));
json_object_object_add(json_nexthop, "lastUpdate",
json_last_update);
} else {
json_object_int_add(json_nexthop, "lastUpdate", tbuf);
}
} else {
vty_out(vty, " Last update: %s", ctime_r(&tbuf, timebuf));
vty_out(vty, " Last update: %s", time_to_string(bnc->last_update, timebuf));
}

/* show paths dependent on nexthop, if needed. */
Expand Down
4 changes: 2 additions & 2 deletions bgpd/bgp_route.c
Original file line number Diff line number Diff line change
Expand Up @@ -11726,11 +11726,11 @@ void route_vty_out_detail(struct vty *vty, struct bgp *bgp, struct bgp_dest *bn,
json_last_update = json_object_new_object();
json_object_int_add(json_last_update, "epoch", tbuf);
json_object_string_add(json_last_update, "string",
ctime_r(&tbuf, timebuf));
time_to_string_json(path->uptime, timebuf));
json_object_object_add(json_path, "lastUpdate",
json_last_update);
} else
vty_out(vty, " Last update: %s", ctime_r(&tbuf, timebuf));
vty_out(vty, " Last update: %s", time_to_string(path->uptime, timebuf));

/* Line 10 display PMSI tunnel attribute, if present */
if (CHECK_FLAG(attr->flag, ATTR_FLAG_BIT(BGP_ATTR_PMSI_TUNNEL))) {
Expand Down
10 changes: 4 additions & 6 deletions bgpd/bgp_updgrp.c
Original file line number Diff line number Diff line change
Expand Up @@ -757,7 +757,7 @@ static int update_group_show_walkcb(struct update_group *updgrp, void *arg)
json_time = json_object_new_object();
json_object_int_add(json_time, "epoch", epoch_tbuf);
json_object_string_add(json_time, "epochString",
ctime_r(&epoch_tbuf, timebuf));
time_to_string_json(updgrp->uptime, timebuf));
json_object_object_add(json_updgrp, "groupCreateTime",
json_time);
json_object_string_add(json_updgrp, "afi",
Expand All @@ -766,8 +766,7 @@ static int update_group_show_walkcb(struct update_group *updgrp, void *arg)
safi2str(updgrp->safi));
} else {
vty_out(vty, "Update-group %" PRIu64 ":\n", updgrp->id);
vty_out(vty, " Created: %s",
timestamp_string(updgrp->uptime, timebuf));
vty_out(vty, " Created: %s", time_to_string(updgrp->uptime, timebuf));
}

filter = &updgrp->conf->filter[updgrp->afi][updgrp->safi];
Expand Down Expand Up @@ -835,15 +834,14 @@ static int update_group_show_walkcb(struct update_group *updgrp, void *arg)
json_object_int_add(json_subgrp_time, "epoch",
epoch_tbuf);
json_object_string_add(json_subgrp_time, "epochString",
ctime_r(&epoch_tbuf, timebuf));
time_to_string_json(subgrp->uptime, timebuf));
json_object_object_add(json_subgrp, "groupCreateTime",
json_subgrp_time);
} else {
vty_out(vty, "\n");
vty_out(vty, " Update-subgroup %" PRIu64 ":\n",
subgrp->id);
vty_out(vty, " Created: %s",
timestamp_string(subgrp->uptime, timebuf));
vty_out(vty, " Created: %s", time_to_string(subgrp->uptime, timebuf));
}

if (subgrp->split_from.update_group_id
Expand Down
8 changes: 0 additions & 8 deletions bgpd/bgpd.h
Original file line number Diff line number Diff line change
Expand Up @@ -2714,14 +2714,6 @@ static inline int peer_group_af_configured(struct peer_group *group)
return 0;
}

static inline char *timestamp_string(time_t ts, char *timebuf)
{
time_t tbuf;

tbuf = time(NULL) - (monotime(NULL) - ts);
return ctime_r(&tbuf, timebuf);
}

static inline bool peer_established(struct peer_connection *connection)
{
return connection->status == Established;
Expand Down
16 changes: 16 additions & 0 deletions lib/monotime.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,22 @@ static inline char *time_to_string(time_t ts, char *buf)
return ctime_r(&tbuf, buf);
}

/* A wrapper for time_to_string() which removes newline at the end.
* This is needed for JSON outputs, where newline is not expected.
*/
static inline char *time_to_string_json(time_t ts, char *buf)
{
size_t len;

time_to_string(ts, buf);
len = strlen(buf);

if (len && buf[len - 1] == '\n')
buf[len - 1] = '\0';

return buf;
}

/* Convert interval to human-friendly string, used in cli output e.g. */
static inline const char *frrtime_to_interval(time_t t, char *buf,
size_t buflen)
Expand Down
3 changes: 1 addition & 2 deletions pathd/path_pcep_debug.c
Original file line number Diff line number Diff line change
Expand Up @@ -1321,8 +1321,7 @@ void _format_pcep_event(int ps, pcep_event *event)
PATHD_FORMAT("\n");
PATHD_FORMAT("%*sevent_type: %s\n", ps2, "",
pcep_event_type_name(event->event_type));
PATHD_FORMAT("%*sevent_time: %s", ps2, "",
ctime_r(&event->event_time, buf));
PATHD_FORMAT("%*sevent_time: %s", ps2, "", time_to_string(event->event_time, buf));
if (event->session == NULL) {
PATHD_FORMAT("%*ssession: NULL\n", ps2, "");
} else {
Expand Down

0 comments on commit e62c2f1

Please sign in to comment.