[asterisk-commits] main/cli.c: Refactor function to print seconds formatted (asterisk[master])

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Tue Mar 8 11:29:45 CST 2016


Joshua Colp has submitted this change and it was merged.

Change subject: main/cli.c: Refactor function to print seconds formatted
......................................................................


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
---
M addons/cdr_mysql.c
M cdr/cdr_pgsql.c
M include/asterisk/cli.h
M main/cli.c
M res/res_config_ldap.c
M res/res_config_pgsql.c
6 files changed, 50 insertions(+), 69 deletions(-)

Approvals:
  Richard Mudgett: Looks good to me, but someone else must approve
  Joshua Colp: Looks good to me, approved; Verified



diff --git a/addons/cdr_mysql.c b/addons/cdr_mysql.c
index d55f9e0..9873395 100644
--- a/addons/cdr_mysql.c
+++ b/addons/cdr_mysql.c
@@ -119,7 +119,9 @@
 		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 @@
 			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 aec2116..ea38cc9 100644
--- a/cdr/cdr_pgsql.c
+++ b/cdr/cdr_pgsql.c
@@ -139,7 +139,9 @@
 		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 @@
 		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 82363c1..0bda666 100644
--- a/include/asterisk/cli.h
+++ b/include/asterisk/cli.h
@@ -315,6 +315,17 @@
  */
 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 9173055..0ac5d61 100644
--- a/main/cli.c
+++ b/main/cli.c
@@ -807,7 +807,7 @@
 		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 @@
 		/* 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 @@
 		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 @@
 	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 @@
 	}
 	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 95eae29..8454273 100644
--- a/res/res_config_ldap.c
+++ b/res/res_config_ldap.c
@@ -1837,7 +1837,9 @@
  */
 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 @@
 	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 77c52aa..73f43ee 100644
--- a/res/res_config_pgsql.c
+++ b/res/res_config_pgsql.c
@@ -1574,7 +1574,9 @@
 
 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 @@
 	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);

-- 
To view, visit https://gerrit.asterisk.org/2272
To unsubscribe, visit https://gerrit.asterisk.org/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: Ibeb8634102cd11d3f8623398b279cb731bcde36c
Gerrit-PatchSet: 6
Gerrit-Project: asterisk
Gerrit-Branch: master
Gerrit-Owner: Rodrigo Ramirez Norambuena <a at rodrigoramirez.com>
Gerrit-Reviewer: Anonymous Coward #1000019
Gerrit-Reviewer: George Joseph <george.joseph at fairview5.com>
Gerrit-Reviewer: Joshua Colp <jcolp at digium.com>
Gerrit-Reviewer: Richard Mudgett <rmudgett at digium.com>
Gerrit-Reviewer: Rodrigo Ramirez Norambuena <a at rodrigoramirez.com>
Gerrit-Reviewer: Walter Doekes <walter+asterisk at wjd.nu>



More information about the asterisk-commits mailing list