diff --git a/addons/cdr_mysql.c b/addons/cdr_mysql.c index d55f9e035fc241e37d92ec0624ef45c5fdc6fd99..9873395c2cae10e9d99468b1b40b5f388384f152 100644 --- a/addons/cdr_mysql.c +++ b/addons/cdr_mysql.c @@ -119,7 +119,9 @@ static char *handle_cli_cdr_mysql_status(struct ast_cli_entry *e, int cmd, struc return CLI_SHOWUSAGE; if (connected) { - char status[256], status2[100] = ""; + char status[256]; + char status2[100] = ""; + char buf[362]; /* 256+100+" for "+NULL */ int ctime = time(NULL) - connect_time; if (dbport) snprintf(status, 255, "Connected to %s@%s, port %d", ast_str_buffer(dbname), ast_str_buffer(hostname), dbport); @@ -132,17 +134,10 @@ static char *handle_cli_cdr_mysql_status(struct ast_cli_entry *e, int cmd, struc snprintf(status2, 99, " with username %s", ast_str_buffer(dbuser)); if (ast_str_strlen(dbtable)) snprintf(status2, 99, " using table %s", ast_str_buffer(dbtable)); - if (ctime > 31536000) { - ast_cli(a->fd, "%s%s for %d years, %d days, %d hours, %d minutes, %d seconds.\n", status, status2, ctime / 31536000, (ctime % 31536000) / 86400, (ctime % 86400) / 3600, (ctime % 3600) / 60, ctime % 60); - } else if (ctime > 86400) { - ast_cli(a->fd, "%s%s for %d days, %d hours, %d minutes, %d seconds.\n", status, status2, ctime / 86400, (ctime % 86400) / 3600, (ctime % 3600) / 60, ctime % 60); - } else if (ctime > 3600) { - ast_cli(a->fd, "%s%s for %d hours, %d minutes, %d seconds.\n", status, status2, ctime / 3600, (ctime % 3600) / 60, ctime % 60); - } else if (ctime > 60) { - ast_cli(a->fd, "%s%s for %d minutes, %d seconds.\n", status, status2, ctime / 60, ctime % 60); - } else { - ast_cli(a->fd, "%s%s for %d seconds.\n", status, status2, ctime); - } + + snprintf(buf, sizeof(buf), "%s%s for ", status, status2); + ast_cli_print_timestr_fromseconds(a->fd, ctime, buf); + if (records == totalrecords) ast_cli(a->fd, " Wrote %d records since last restart.\n", totalrecords); else diff --git a/cdr/cdr_pgsql.c b/cdr/cdr_pgsql.c index aec2116503464047bd6d4b55331949d2072adc74..ea38cc9837c7c9975fe685f1c0f662c2929214eb 100644 --- a/cdr/cdr_pgsql.c +++ b/cdr/cdr_pgsql.c @@ -139,7 +139,9 @@ static char *handle_cdr_pgsql_status(struct ast_cli_entry *e, int cmd, struct as return CLI_SHOWUSAGE; if (connected) { - char status[256], status2[100] = ""; + char status[256]; + char status2[100] = ""; + char buf[362]; /* 256+100+" for "+NULL */ int ctime = time(NULL) - connect_time; if (pgdbport) { @@ -154,17 +156,10 @@ static char *handle_cdr_pgsql_status(struct ast_cli_entry *e, int cmd, struct as if (table && *table) { snprintf(status2, 99, " using table %s", table); } - if (ctime > 31536000) { - ast_cli(a->fd, "%s%s for %d years, %d days, %d hours, %d minutes, %d seconds.\n", status, status2, ctime / 31536000, (ctime % 31536000) / 86400, (ctime % 86400) / 3600, (ctime % 3600) / 60, ctime % 60); - } else if (ctime > 86400) { - ast_cli(a->fd, "%s%s for %d days, %d hours, %d minutes, %d seconds.\n", status, status2, ctime / 86400, (ctime % 86400) / 3600, (ctime % 3600) / 60, ctime % 60); - } else if (ctime > 3600) { - ast_cli(a->fd, "%s%s for %d hours, %d minutes, %d seconds.\n", status, status2, ctime / 3600, (ctime % 3600) / 60, ctime % 60); - } else if (ctime > 60) { - ast_cli(a->fd, "%s%s for %d minutes, %d seconds.\n", status, status2, ctime / 60, ctime % 60); - } else { - ast_cli(a->fd, "%s%s for %d seconds.\n", status, status2, ctime); - } + + snprintf(buf, sizeof(buf), "%s%s for ", status, status2); + ast_cli_print_timestr_fromseconds(a->fd, ctime, buf); + if (records == totalrecords) { ast_cli(a->fd, " Wrote %d records since last restart.\n", totalrecords); } else { diff --git a/include/asterisk/cli.h b/include/asterisk/cli.h index 82363c15f467dd134280a0531f3cb343acd8f90f..0bda6665ce3e6c01b32b5fb33c44b2e3a5670768 100644 --- a/include/asterisk/cli.h +++ b/include/asterisk/cli.h @@ -315,6 +315,17 @@ char **ast_cli_completion_matches(const char *, const char *); */ char *ast_complete_channels(const char *line, const char *word, int pos, int state, int rpos); +/*! + * \since 13.8 + * \brief Print on cli a duration in seconds in format + * %s year(s), %s week(s), %s day(s), %s hour(s), %s second(s) + * + * \param ast_cli_args fd to print by ast_cli + * \param duration The time (in seconds) to print + * \param prefix A Prefix string to add before of duration formatted + */ +void ast_cli_print_timestr_fromseconds(int fd, int seconds, const char *prefix); + #if defined(__cplusplus) || defined(c_plusplus) } #endif diff --git a/main/cli.c b/main/cli.c index 917305594a9e6b0c7d428a05d54ec5364e24b374..0ac5d612a0af7c01f13e7af23e6ad8b69cd24da3 100644 --- a/main/cli.c +++ b/main/cli.c @@ -807,7 +807,7 @@ static void print_uptimestr(int fd, struct timeval timeval, const char *prefix, return; if (printsec) { /* plain seconds output */ - ast_cli(fd, "%s: %lu\n", prefix, (u_long)timeval.tv_sec); + ast_cli(fd, "%s%lu\n", prefix, (u_long)timeval.tv_sec); return; } out = ast_str_alloca(256); @@ -841,7 +841,7 @@ static void print_uptimestr(int fd, struct timeval timeval, const char *prefix, /* if there is nothing, print 0 seconds */ ast_str_append(&out, 0, "%d second%s", x, ESS(x)); } - ast_cli(fd, "%s: %s\n", prefix, ast_str_buffer(out)); + ast_cli(fd, "%s%s\n", prefix, ast_str_buffer(out)); } static struct ast_cli_entry *cli_next(struct ast_cli_entry *e) @@ -877,10 +877,12 @@ static char * handle_showuptime(struct ast_cli_entry *e, int cmd, struct ast_cli printsec = 0; else return CLI_SHOWUSAGE; - if (ast_startuptime.tv_sec) - print_uptimestr(a->fd, ast_tvsub(curtime, ast_startuptime), "System uptime", printsec); - if (ast_lastreloadtime.tv_sec) - print_uptimestr(a->fd, ast_tvsub(curtime, ast_lastreloadtime), "Last reload", printsec); + if (ast_startuptime.tv_sec) { + print_uptimestr(a->fd, ast_tvsub(curtime, ast_startuptime), "System uptime: ", printsec); + } + if (ast_lastreloadtime.tv_sec) { + print_uptimestr(a->fd, ast_tvsub(curtime, ast_lastreloadtime), "Last reload: ", printsec); + } return CLI_SUCCESS; } @@ -972,7 +974,7 @@ static char *handle_showcalls(struct ast_cli_entry *e, int cmd, struct ast_cli_a ast_cli(a->fd, "%d call%s processed\n", ast_processed_calls(), ESS(ast_processed_calls())); if (ast_startuptime.tv_sec && showuptime) { - print_uptimestr(a->fd, ast_tvsub(curtime, ast_startuptime), "System uptime", printsec); + print_uptimestr(a->fd, ast_tvsub(curtime, ast_startuptime), "System uptime: ", printsec); } return RESULT_SUCCESS; @@ -2744,3 +2746,8 @@ int ast_cli_command_multiple_full(int uid, int gid, int fd, size_t size, const c } return count; } + +void ast_cli_print_timestr_fromseconds(int fd, int seconds, const char *prefix) +{ + print_uptimestr(fd, ast_tv(seconds, 0), prefix, 0); +} diff --git a/res/res_config_ldap.c b/res/res_config_ldap.c index 95eae29c19bf8a04b36b7903d89a91bd9143ae9c..8454273799a19ab58021a7286ca5d7b8b913602e 100644 --- a/res/res_config_ldap.c +++ b/res/res_config_ldap.c @@ -1837,7 +1837,9 @@ static int ldap_reconnect(void) */ static char *realtime_ldap_status(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) { - char status[256], credentials[100] = ""; + char status[256]; + char credentials[100] = ""; + char buf[362]; /* 256+100+" for "+NULL */ int ctimesec = time(NULL) - connect_time; switch (cmd) { @@ -1860,25 +1862,8 @@ static char *realtime_ldap_status(struct ast_cli_entry *e, int cmd, struct ast_c if (!ast_strlen_zero(user)) snprintf(credentials, sizeof(credentials), " with username %s", user); - if (ctimesec > 31536000) { - ast_cli(a->fd, "%s%s for %d years, %d days, %d hours, %d minutes, %d seconds.\n", - status, credentials, ctimesec / 31536000, - (ctimesec % 31536000) / 86400, (ctimesec % 86400) / 3600, - (ctimesec % 3600) / 60, ctimesec % 60); - } else if (ctimesec > 86400) { - ast_cli(a->fd, "%s%s for %d days, %d hours, %d minutes, %d seconds.\n", - status, credentials, ctimesec / 86400, (ctimesec % 86400) / 3600, - (ctimesec % 3600) / 60, ctimesec % 60); - } else if (ctimesec > 3600) { - ast_cli(a->fd, "%s%s for %d hours, %d minutes, %d seconds.\n", - status, credentials, ctimesec / 3600, (ctimesec % 3600) / 60, - ctimesec % 60); - } else if (ctimesec > 60) { - ast_cli(a->fd, "%s%s for %d minutes, %d seconds.\n", status, credentials, - ctimesec / 60, ctimesec % 60); - } else { - ast_cli(a->fd, "%s%s for %d seconds.\n", status, credentials, ctimesec); - } + snprintf(buf, sizeof(buf), "%s%s for ", status, credentials); + ast_cli_print_timestr_fromseconds(a->fd, ctimesec, buf); return CLI_SUCCESS; } diff --git a/res/res_config_pgsql.c b/res/res_config_pgsql.c index 77c52aa0b631cc8773d83fb57bd304604b6dde32..73f43ee2ccef0d2b6722f1637dd3c317637777e8 100644 --- a/res/res_config_pgsql.c +++ b/res/res_config_pgsql.c @@ -1574,7 +1574,9 @@ static char *handle_cli_realtime_pgsql_cache(struct ast_cli_entry *e, int cmd, s static char *handle_cli_realtime_pgsql_status(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) { - char connection_info[256], credentials[100] = ""; + char connection_info[256]; + char credentials[100] = ""; + char buf[376]; /* 256+100+"Connected to "+" for "+NULL */ int ctimesec = time(NULL) - connect_time; switch (cmd) { @@ -1601,24 +1603,10 @@ static char *handle_cli_realtime_pgsql_status(struct ast_cli_entry *e, int cmd, if (!ast_strlen_zero(dbuser)) snprintf(credentials, sizeof(credentials), " with username %s", dbuser); - if (pgsqlConn && PQstatus(pgsqlConn) == CONNECTION_OK) { - if (ctimesec > 31536000) - ast_cli(a->fd, "Connected to %s%s for %d years, %d days, %d hours, %d minutes, %d seconds.\n", - connection_info, credentials, ctimesec / 31536000, (ctimesec % 31536000) / 86400, - (ctimesec % 86400) / 3600, (ctimesec % 3600) / 60, ctimesec % 60); - else if (ctimesec > 86400) - ast_cli(a->fd, "Connected to %s%s for %d days, %d hours, %d minutes, %d seconds.\n", connection_info, - credentials, ctimesec / 86400, (ctimesec % 86400) / 3600, (ctimesec % 3600) / 60, - ctimesec % 60); - else if (ctimesec > 3600) - ast_cli(a->fd, "Connected to %s%s for %d hours, %d minutes, %d seconds.\n", connection_info, credentials, - ctimesec / 3600, (ctimesec % 3600) / 60, ctimesec % 60); - else if (ctimesec > 60) - ast_cli(a->fd, "Connected to %s%s for %d minutes, %d seconds.\n", connection_info, credentials, ctimesec / 60, - ctimesec % 60); - else - ast_cli(a->fd, "Connected to %s%s for %d seconds.\n", connection_info, credentials, ctimesec); + if (pgsqlConn && PQstatus(pgsqlConn) == CONNECTION_OK) { + snprintf(buf, sizeof(buf), "Connected to %s%s for ", connection_info, credentials); + ast_cli_print_timestr_fromseconds(a->fd, ctimesec, buf); return CLI_SUCCESS; } else { ast_cli(a->fd, "Unable to connect %s%s\n", connection_info, credentials);