mirror of
https://github.com/asterisk/asterisk.git
synced 2025-09-03 11:25:35 +00:00
main/cli.c: Refactor function to print seconds formatted
Refactor and created function ast_cli_print_timestr_fromseconds to print seconds formatted: year(s) week(s) day(s) hour(s) second(s) This function now is used in addons/cdr_mysql.c,cdr_pgsql.c, main/cli.c, res_config_ldap.c, res_config_pgsql.c. Change-Id: Ibeb8634102cd11d3f8623398b279cb731bcde36c
This commit is contained in:
@@ -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
|
||||
|
@@ -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 {
|
||||
|
@@ -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
|
||||
|
21
main/cli.c
21
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);
|
||||
}
|
||||
|
@@ -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;
|
||||
}
|
||||
|
@@ -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);
|
||||
|
Reference in New Issue
Block a user