[Asterisk-code-review] main/cli.c: Refactor function to print seconds formated (asterisk[master])

Rodrigo Ramirez Norambuena asteriskteam at digium.com
Wed Feb 17 23:08:46 CST 2016


Rodrigo Ramirez Norambuena has uploaded a new change for review.

  https://gerrit.asterisk.org/2272

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

main/cli.c: Refactor function to print seconds formated

Refactor and created function ast_cli_print_timestr_fromseconds to print
secondsformated:  year(s) week(s) day(s) hour(s) second(s)

This function not 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, 37 insertions(+), 66 deletions(-)


  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/72/2272/1

diff --git a/addons/cdr_mysql.c b/addons/cdr_mysql.c
index d55f9e0..3879b72 100644
--- a/addons/cdr_mysql.c
+++ b/addons/cdr_mysql.c
@@ -119,7 +119,7 @@
 		return CLI_SHOWUSAGE;
 
 	if (connected) {
-		char status[256], status2[100] = "";
+		char status[256], status2[100] = "", buf[356];
 		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 +132,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..eaa207d 100644
--- a/cdr/cdr_pgsql.c
+++ b/cdr/cdr_pgsql.c
@@ -139,7 +139,7 @@
 		return CLI_SHOWUSAGE;
 
 	if (connected) {
-		char status[256], status2[100] = "";
+		char status[256], status2[100] = "", buf[356];
 		int ctime = time(NULL) - connect_time;
 
 		if (pgdbport) {
@@ -154,17 +154,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..b6d1670 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 inseconds 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..f42a875 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);
@@ -878,9 +878,9 @@
 	else
 		return CLI_SHOWUSAGE;
 	if (ast_startuptime.tv_sec)
-		print_uptimestr(a->fd, ast_tvsub(curtime, ast_startuptime), "System uptime", printsec);
+		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);
+		print_uptimestr(a->fd, ast_tvsub(curtime, ast_lastreloadtime), "Last reload:", printsec);
 	return CLI_SUCCESS;
 }
 
@@ -972,7 +972,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 +2744,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..b801efb 100644
--- a/res/res_config_ldap.c
+++ b/res/res_config_ldap.c
@@ -1837,7 +1837,7 @@
  */
 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], credentials[100] = "", buf[356];
 	int ctimesec = time(NULL) - connect_time;
 
 	switch (cmd) {
@@ -1860,25 +1860,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, ctime, buf);
 
 	return CLI_SUCCESS;
 }
diff --git a/res/res_config_pgsql.c b/res/res_config_pgsql.c
index 77c52aa..0ccc88f 100644
--- a/res/res_config_pgsql.c
+++ b/res/res_config_pgsql.c
@@ -1574,7 +1574,7 @@
 
 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], credentials[100] = "", buf[356];
 	int ctimesec = time(NULL) - connect_time;
 
 	switch (cmd) {
@@ -1601,24 +1601,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);
+	snprintf(buf, sizeof(buf), "Connected to %s%s for ", connection_info, credentials);
 
+	if (pgsqlConn && PQstatus(pgsqlConn) == CONNECTION_OK) {
+		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: newchange
Gerrit-Change-Id: Ibeb8634102cd11d3f8623398b279cb731bcde36c
Gerrit-PatchSet: 1
Gerrit-Project: asterisk
Gerrit-Branch: master
Gerrit-Owner: Rodrigo Ramirez Norambuena <a at rodrigoramirez.com>



More information about the asterisk-code-review mailing list