[Asterisk-Dev] Time stamps

Steve Brown sbrown at cortland.com
Sun Apr 13 18:02:56 MST 2003


Tilghman Lesher wrote:

>A couple thoughts on your patches:
>
>I think the prompt might could be better addressed as an asterisk
>command line option and/or environmental variable with
>formatting.  For example, username and hostname of the running
>process may be beneficial to people running multiple asterisk
>servers.  Instead of hardcoding the prompt, it should probably be
>customizable from outside the source.
>
>Ditto for the logger.  For some active servers, coding the time
>is just going to be clutter in the logs.  For other less active
>servers, it might even be good to have the date, in addition to
>the time.
>
>Also, you should probably use the POSIX gettimeofday(2),
>instead of the obsolete time(2) call.
>
>-Tilghman
>
>  
>
Is this closer to what you had in mind?

Steve


-------------- next part --------------
Index: asterisk.c
===================================================================
RCS file: /usr/cvsroot/asterisk/asterisk.c,v
retrieving revision 1.2
diff -u -r1.2 asterisk.c
--- asterisk.c	8 Apr 2003 13:45:36 -0000	1.2
+++ asterisk.c	14 Apr 2003 00:55:47 -0000
@@ -53,6 +53,8 @@
 int option_console=0;
 int option_highpriority=0;
 int option_remote=0;
+int option_timestamp=0;
+char option_timestamp_format[20] = "[%H:%M:%S]";
 int option_exec=0;
 int option_initcrypto=0;
 int option_nocolor;
@@ -687,11 +689,19 @@
 static char *cli_prompt(EditLine *el)
 {
 	static char prompt[80];
+	struct timeval tvl;
+
+	if (option_timestamp) {
+		gettimeofday(&tvl, NULL);
+		strftime(prompt, sizeof(prompt), option_timestamp_format, localtime((time_t*) &tvl.tv_sec));
+	}
+	else
+		prompt[0] = '\0';
 
 	if (remotehostname)
-		snprintf(prompt, sizeof(prompt), ASTERISK_PROMPT2, remotehostname);
+		snprintf(prompt + strlen(prompt), sizeof(prompt), ASTERISK_PROMPT2, remotehostname);
 	else
-		snprintf(prompt, sizeof(prompt), ASTERISK_PROMPT);
+		snprintf(prompt + strlen(prompt), sizeof(prompt), ASTERISK_PROMPT);
 
 	return(prompt);	
 }
@@ -1122,7 +1132,7 @@
 	}
 	*/
 	/* Check for options */
-	while((c=getopt(argc, argv, "hfdvqprgcinx:C:")) != EOF) {
+	while((c=getopt(argc, argv, "hfdvqprtgcinx:C:")) != EOF) {
 		switch(c) {
 		case 'd':
 			option_debug++;
@@ -1144,6 +1154,9 @@
 			break;
 		case 'p':
 			option_highpriority++;
+			break;
+		case 't':
+			option_timestamp++;
 			break;
 		case 'v':
 			option_verbose++;
Index: cli.c
===================================================================
RCS file: /usr/cvsroot/asterisk/cli.c,v
retrieving revision 1.3
diff -u -r1.3 cli.c
--- cli.c	9 Apr 2003 04:00:43 -0000	1.3
+++ cli.c	14 Apr 2003 00:55:48 -0000
@@ -82,6 +82,12 @@
 "       Sets level of verbose messages to be displayed.  0 means\n"
 "       no messages should be displayed.\n";
 
+static char set_timestamp_help[] = 
+"Usage: set timestamp [on|off|format <dateformat>]\n"
+"       Turns logging timestamps on/off.\n"
+"       The format option sets the strftime format string.\n"
+"       A missing argument inverts the current state.\n";
+
 static char softhangup_help[] =
 "Usage: soft hangup <channel>\n"
 "       Request that a channel be hung up.  The hangup takes effect\n"
@@ -106,6 +112,37 @@
 	return RESULT_SUCCESS;
 }
 
+
+static int handle_set_timestamp(int fd, int argc, char *argv[])
+{
+	if ((argc < 2) || (argc > 4))
+		return RESULT_SHOWUSAGE;
+	if (argc == 2) {
+		if (option_timestamp) 
+			option_timestamp = 0;
+		else
+			option_timestamp = 1;
+
+		return RESULT_SUCCESS;
+	}
+	if (argc == 3)  {
+		if (!strcasecmp(argv[2], "on"))
+			option_timestamp = 1;
+		else if (!strcasecmp(argv[2], "off"))
+			option_timestamp = 0;
+		else
+			return RESULT_SHOWUSAGE;
+
+		return RESULT_SUCCESS;
+	}
+	if ((argc == 4) && !strcasecmp(argv[2], "format"))
+		strcpy(option_timestamp_format, argv[3]);
+	else
+		return RESULT_SHOWUSAGE;
+
+	return RESULT_SUCCESS;
+}	
+
 static int handle_set_verbose(int fd, int argc, char *argv[])
 {
 	int val;
@@ -472,6 +509,7 @@
 	{ { "load", NULL }, handle_load, "Load a dynamic module by name", load_help, complete_fn },
 	{ { "no", "debug", "channel", NULL }, handle_nodebugchan, "Disable debugging on a channel", nodebugchan_help, complete_ch },
 	{ { "reload", NULL }, handle_reload, "Reload configuration", reload_help },
+	{ { "set", "timestamp", NULL }, handle_set_timestamp, "Logger timestamp", set_timestamp_help },
 	{ { "set", "verbose", NULL }, handle_set_verbose, "Set level of verboseness", set_verbose_help },
 	{ { "show", "channel", NULL }, handle_showchan, "Display information on a specific channel", showchan_help, complete_ch },
 	{ { "show", "channels", NULL }, handle_chanlist, "Display information on channels", chanlist_help },
Index: logger.c
===================================================================
RCS file: /usr/cvsroot/asterisk/logger.c,v
retrieving revision 1.2
diff -u -r1.2 logger.c
--- logger.c	31 Mar 2003 03:19:34 -0000	1.2
+++ logger.c	14 Apr 2003 00:55:48 -0000
@@ -296,6 +296,14 @@
 	struct msglist *m;
 	struct verb *v;
 	va_list ap;
+	struct timeval tvl;
+
+	if (option_timestamp) {
+		gettimeofday(&tvl, NULL);
+		strftime(stuff, sizeof(stuff), option_timestamp_format, localtime((time_t*) &tvl.tv_sec));
+		pos = strlen(stuff);
+	}
+
 	va_start(ap, fmt);
 	ast_pthread_mutex_lock(&msglist_lock);
 	vsnprintf(stuff + pos, sizeof(stuff) - pos, fmt, ap);
Index: include/asterisk/options.h
===================================================================
RCS file: /usr/cvsroot/asterisk/include/asterisk/options.h,v
retrieving revision 1.1.1.1
diff -u -r1.1.1.1 options.h
--- include/asterisk/options.h	12 Feb 2003 13:59:15 -0000	1.1.1.1
+++ include/asterisk/options.h	14 Apr 2003 00:55:48 -0000
@@ -22,6 +22,8 @@
 extern int option_debug;
 extern int option_nofork;
 extern int option_quiet;
+extern int option_timestamp;
+extern char option_timestamp_format[];
 extern int option_console;
 extern int option_initcrypto;
 extern int option_nocolor;


More information about the asterisk-dev mailing list