From: Renato Westphal Date: Sat, 10 Aug 2019 20:57:43 +0000 (-0300) Subject: isisd: split vty_out_timestr() into two helper functions X-Git-Tag: frr-7.4~8^2~9 X-Git-Url: https://git.puffer.fish/?a=commitdiff_plain;h=9cd06cd7b9ee0a966d7ae9a7d23385a2d287845d;p=mirror%2Ffrr.git isisd: split vty_out_timestr() into two helper functions The new log_uptime() function logs an UNIX timestamp to a buffer provided by the user. It's very flexibile and can be used in a variety of contexts, different from vty_out_timestr() which is too tied to the VTY code. Signed-off-by: Renato Westphal --- diff --git a/isisd/isis_misc.c b/isisd/isis_misc.c index 25f7f8609b..27d06e8da7 100644 --- a/isisd/isis_misc.c +++ b/isisd/isis_misc.c @@ -538,6 +538,26 @@ void log_multiline(int priority, const char *prefix, const char *format, ...) XFREE(MTYPE_TMP, p); } +char *log_uptime(time_t uptime, char *buf, size_t nbuf) +{ + struct tm *tm; + time_t difftime = time(NULL); + difftime -= uptime; + tm = gmtime(&difftime); + + if (difftime < ONE_DAY_SECOND) + snprintf(buf, nbuf, "%02d:%02d:%02d", tm->tm_hour, tm->tm_min, + tm->tm_sec); + else if (difftime < ONE_WEEK_SECOND) + snprintf(buf, nbuf, "%dd%02dh%02dm", tm->tm_yday, tm->tm_hour, + tm->tm_min); + else + snprintf(buf, nbuf, "%02dw%dd%02dh", tm->tm_yday / 7, + tm->tm_yday - ((tm->tm_yday / 7) * 7), tm->tm_hour); + + return buf; +} + void vty_multiline(struct vty *vty, const char *prefix, const char *format, ...) { char shortbuf[256]; diff --git a/isisd/isis_misc.h b/isisd/isis_misc.h index fbfabaf24f..5cdbbfb058 100644 --- a/isisd/isis_misc.h +++ b/isisd/isis_misc.h @@ -80,6 +80,7 @@ enum { ISIS_UI_LEVEL_BRIEF, #include "lib/log.h" void log_multiline(int priority, const char *prefix, const char *format, ...) PRINTFRR(3, 4); +char *log_uptime(time_t uptime, char *buf, size_t nbuf); struct vty; void vty_multiline(struct vty *vty, const char *prefix, const char *format, ...) PRINTFRR(3, 4);