[asterisk-commits] kpfleming: branch kpfleming/dynamic_logger_levels r191993 - in /team/kpflemin...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Mon May 4 05:52:53 CDT 2009


Author: kpfleming
Date: Mon May  4 05:52:47 2009
New Revision: 191993

URL: http://svn.digium.com/svn-view/asterisk?view=rev&rev=191993
Log:
add a test module, and use it to make the code actually work


Added:
    team/kpfleming/dynamic_logger_levels/tests/test_logger.c   (contents, props changed)
      - copied, changed from r191954, team/kpfleming/dynamic_logger_levels/tests/test_skel.c
Modified:
    team/kpfleming/dynamic_logger_levels/include/asterisk/logger.h
    team/kpfleming/dynamic_logger_levels/main/logger.c
    team/kpfleming/dynamic_logger_levels/tests/test_sched.c
    team/kpfleming/dynamic_logger_levels/tests/test_skel.c

Modified: team/kpfleming/dynamic_logger_levels/include/asterisk/logger.h
URL: http://svn.digium.com/svn-view/asterisk/team/kpfleming/dynamic_logger_levels/include/asterisk/logger.h?view=diff&rev=191993&r1=191992&r2=191993
==============================================================================
--- team/kpfleming/dynamic_logger_levels/include/asterisk/logger.h (original)
+++ team/kpfleming/dynamic_logger_levels/include/asterisk/logger.h Mon May  4 05:52:47 2009
@@ -210,6 +210,8 @@
  * \since 1.6.3
  */
 void ast_logger_unregister_level(const char *name);
+
+#define ast_log_dynamic_level(a, ...) ast_log(a, __FILE__, __LINE__, __PRETTY_FUNCTION__, __VA_ARGS__)
 
 /*!
  * \brief Log a DEBUG message

Modified: team/kpfleming/dynamic_logger_levels/main/logger.c
URL: http://svn.digium.com/svn-view/asterisk/team/kpfleming/dynamic_logger_levels/main/logger.c?view=diff&rev=191993&r1=191992&r2=191993
==============================================================================
--- team/kpfleming/dynamic_logger_levels/main/logger.c (original)
+++ team/kpfleming/dynamic_logger_levels/main/logger.c Mon May  4 05:52:47 2009
@@ -54,7 +54,7 @@
 		        from <syslog.h> which is included by logger.h */
 #include <syslog.h>
 
-static int syslog_level_map[] = {
+static const int syslog_level_map[] = {
 	LOG_DEBUG,
 	LOG_INFO,    /* arbitrary equivalent of LOG_EVENT */
 	LOG_NOTICE,
@@ -89,7 +89,6 @@
 #else
 #define GETTID() getpid()
 #endif
-
 static char dateformat[256] = "%b %e %T";		/* Original Asterisk Format */
 
 static char queue_log_name[256] = QUEUELOG;
@@ -154,26 +153,57 @@
 
 static FILE *qlog;
 
-/*! \brief Logging channels used in the Asterisk logging system */
-static char *levels[] = {
+/*! \brief Logging channels used in the Asterisk logging system
+ *
+ * The first 16 levels are reserved for system usage, and the remaining
+ * levels are reserved for usage by dynamic levels registered via
+ * ast_logger_register_level.
+ */
+
+static const char *levels[32] = {
 	"DEBUG",
 	"---EVENT---",		/* no longer used */
 	"NOTICE",
 	"WARNING",
 	"ERROR",
 	"VERBOSE",
-	"DTMF"
+	"DTMF",
 };
 
 /*! \brief Colors used in the console for logging */
-static int colors[] = {
+static const int colors[32] = {
 	COLOR_BRGREEN,
-	COLOR_BRBLUE,
+	COLOR_BRBLUE,		/* no longer used */
 	COLOR_YELLOW,
 	COLOR_BRRED,
 	COLOR_RED,
 	COLOR_GREEN,
-	COLOR_BRGREEN
+	COLOR_BRGREEN,
+	0,
+	0,
+	0,
+	0,
+	0,
+	0,
+	0,
+	0,
+	0,
+	COLOR_BRBLUE,
+	COLOR_BRBLUE,
+	COLOR_BRBLUE,
+	COLOR_BRBLUE,
+	COLOR_BRBLUE,
+	COLOR_BRBLUE,
+	COLOR_BRBLUE,
+	COLOR_BRBLUE,
+	COLOR_BRBLUE,
+	COLOR_BRBLUE,
+	COLOR_BRBLUE,
+	COLOR_BRBLUE,
+	COLOR_BRBLUE,
+	COLOR_BRBLUE,
+	COLOR_BRBLUE,
+	COLOR_BRBLUE,
 };
 
 AST_THREADSTORAGE(verbose_buf);
@@ -181,8 +211,6 @@
 
 AST_THREADSTORAGE(log_buf);
 #define LOG_BUF_INIT_SIZE       256
-
-static const char *dynamic_levels[16];
 
 AST_MUTEX_DEFINE_STATIC(dynamic_levels_lock);
 
@@ -194,33 +222,20 @@
 	unsigned int x;
 
 	while ((w = strsep(&stringp, ","))) {
+		int found = 0;
+
 		w = ast_skip_blanks(w);
-		if (!strcasecmp(w, "error")) 
-			res |= (1 << __LOG_ERROR);
-		else if (!strcasecmp(w, "warning"))
-			res |= (1 << __LOG_WARNING);
-		else if (!strcasecmp(w, "notice"))
-			res |= (1 << __LOG_NOTICE);
-		else if (!strcasecmp(w, "debug"))
-			res |= (1 << __LOG_DEBUG);
-		else if (!strcasecmp(w, "verbose"))
-			res |= (1 << __LOG_VERBOSE);
-		else if (!strcasecmp(w, "dtmf"))
-			res |= (1 << __LOG_DTMF);
-		else {
-			int found = 0;
-
-			for (x = 0; x < ARRAY_LEN(dynamic_levels); x++) {
-				if (!strcasecmp(w, dynamic_levels[x])) {
-					res |= (1 << (x + 16));
-					found = 1;
-					break;
-				}
+
+		for (x = 0; x < ARRAY_LEN(levels); x++) {
+			if (levels[x] && !strcasecmp(w, levels[x])) {
+				res |= (1 << x);
+				found = 1;
+				break;
 			}
-			if (!found) {
-				fprintf(stderr, "Logfile Warning: Unknown keyword '%s' at line %d of logger.conf\n",
-					w, lineno);
-			}
+		}
+		if (!found) {
+			fprintf(stderr, "Logfile Warning: Unknown keyword '%s' at line %d of logger.conf\n",
+				w, lineno);
 		}
 	}
 
@@ -740,7 +755,7 @@
 		return CLI_SHOWUSAGE;
 
 	for (x = 0; x < ARRAY_LEN(levels); x++) {
-		if (!strcasecmp(a->argv[3], levels[x])) {
+		if (levels[x] && !strcasecmp(a->argv[3], levels[x])) {
 			level = x;
 			break;
 		}
@@ -783,21 +798,9 @@
 		ast_cli(a->fd, FORMATL, chan->filename, chan->type == LOGTYPE_CONSOLE ? "Console" : (chan->type == LOGTYPE_SYSLOG ? "Syslog" : "File"),
 			chan->disabled ? "Disabled" : "Enabled");
 		ast_cli(a->fd, " - ");
-		if (chan->logmask & (1 << __LOG_DEBUG)) 
-			ast_cli(a->fd, "Debug ");
-		if (chan->logmask & (1 << __LOG_DTMF)) 
-			ast_cli(a->fd, "DTMF ");
-		if (chan->logmask & (1 << __LOG_VERBOSE)) 
-			ast_cli(a->fd, "Verbose ");
-		if (chan->logmask & (1 << __LOG_WARNING)) 
-			ast_cli(a->fd, "Warning ");
-		if (chan->logmask & (1 << __LOG_NOTICE)) 
-			ast_cli(a->fd, "Notice ");
-		if (chan->logmask & (1 << __LOG_ERROR)) 
-			ast_cli(a->fd, "Error ");
-		for (level = 0; level < ARRAY_LEN(dynamic_levels); level++) {
-			if (chan->logmask & (1 << (level + 16))) {
-				ast_cli(a->fd, "%s ", dynamic_levels[level]);
+		for (level = 0; level < ARRAY_LEN(levels); level++) {
+			if (chan->logmask & (1 << level)) {
+				ast_cli(a->fd, "%s ", levels[level]);
 			}
 		}
 		ast_cli(a->fd, "\n");
@@ -1334,30 +1337,20 @@
 	unsigned int level;
 	unsigned int available = 0;
 
-	for (level = 0; level < ARRAY_LEN(levels); level++) {
-		if (!strcasecmp(levels[level], name)) {
+	ast_mutex_lock(&dynamic_levels_lock);
+
+	for (level = 16; level < ARRAY_LEN(levels); level++) {
+		if (!available && !levels[level]) {
+			available = level;
+			continue;
+		}
+
+		if (levels[level] && !strcasecmp(levels[level], name)) {
 			ast_log(LOG_WARNING,
 				"Unable to register dynamic logger level '%s': a standard logger level uses that name.\n",
 				name);
-			
-			return -1;
-		}
-	}
-
-	ast_mutex_lock(&dynamic_levels_lock);
-
-	for (level = 0; level < ARRAY_LEN(dynamic_levels); level++) {
-		if (!dynamic_levels[level]) {
-			available = level + 16;
-			continue;
-		}
-
-		if (!strcasecmp(dynamic_levels[level], name)) {
-			ast_log(LOG_WARNING,
-				"Unable to register dynamic logger level '%s': a level is already registered with that name.\n",
-				name);
 			ast_mutex_unlock(&dynamic_levels_lock);
-			
+
 			return -1;
 		}
 	}
@@ -1371,7 +1364,7 @@
 		return -1;
 	}
 
-	dynamic_levels[available - 16] = name;
+	levels[available] = name;
 
 	ast_mutex_unlock(&dynamic_levels_lock);
 
@@ -1388,18 +1381,22 @@
 
 	ast_mutex_lock(&dynamic_levels_lock);
 
-	for (x = 0; x < ARRAY_LEN(dynamic_levels); x++) {
-		if (strcasecmp(dynamic_levels[x], name)) {
+	for (x = 16; x < ARRAY_LEN(levels); x++) {
+		if (!levels[x]) {
 			continue;
 		}
 
-		dynamic_levels[x] = NULL;
+		if (strcasecmp(levels[x], name)) {
+			continue;
+		}
+
+		levels[x] = NULL;
 		break;
 	}
 
 	ast_mutex_unlock(&dynamic_levels_lock);
 
-	if (x < ARRAY_LEN(dynamic_levels)) {
+	if (x < ARRAY_LEN(levels)) {
 		ast_debug(1, "Unregistered dynamic logger level '%s' with index %d.\n", name, x);
 
 		update_logchannels();

Copied: team/kpfleming/dynamic_logger_levels/tests/test_logger.c (from r191954, team/kpfleming/dynamic_logger_levels/tests/test_skel.c)
URL: http://svn.digium.com/svn-view/asterisk/team/kpfleming/dynamic_logger_levels/tests/test_logger.c?view=diff&rev=191993&p1=team/kpfleming/dynamic_logger_levels/tests/test_skel.c&r1=191954&p2=team/kpfleming/dynamic_logger_levels/tests/test_logger.c&r2=191993
==============================================================================
--- team/kpfleming/dynamic_logger_levels/tests/test_skel.c (original)
+++ team/kpfleming/dynamic_logger_levels/tests/test_logger.c Mon May  4 05:52:47 2009
@@ -1,9 +1,9 @@
 /*
  * Asterisk -- An open source telephony toolkit.
  *
- * Copyright (C) <Year>, <Your Name Here>
+ * Copyright (C) 2009, Digium, Inc.
  *
- * <Your Name Here> <<Your Email Here>>
+ * Kevin P. Fleming <kpfleming at digium.com>
  *
  * See http://www.asterisk.org for more information about
  * the Asterisk project. Please do not directly contact
@@ -18,11 +18,10 @@
 
 /*! \file
  *
- * \brief Skeleton Test
+ * \brief Test module for the logging subsystem
  *
- * \author\verbatim <Your Name Here> <<Your Email Here>> \endverbatim
+ * \author\verbatim Kevin P. Fleming <kpfleming at digium.com> \endverbatim
  * 
- * This is a skeleton for development of an Asterisk test module
  * \ingroup tests
  */
 
@@ -40,15 +39,50 @@
 #include "asterisk/module.h"
 #include "asterisk/lock.h"
 #include "asterisk/app.h"
+#include "asterisk/cli.h"
+
+static char *handle_cli_dynamic_level_test(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
+{
+	unsigned int level;
+
+	switch (cmd) {
+	case CLI_INIT:
+		e->command = "logger test dynamic";
+		e->usage = ""
+			"Usage: logger test dynamic\n"
+			"";
+		return NULL;
+	case CLI_GENERATE:
+		return NULL;
+	}
+
+	ast_cli(a->fd, "Test 1: Register level 'test', send a message, unregister.\n");
+
+	if ((level = ast_logger_register_level("test")) != -1) {
+		ast_cli(a->fd, "Test 1: got level %d\n", level);
+		ast_log_dynamic_level(level, "Logger Dynamic Test: Test 1\n");
+		ast_logger_unregister_level("test");
+	} else {
+		ast_cli(a->fd, "Test 1: Failed, could not register level 'test'.\n");
+	}
+
+	return CLI_SUCCESS;
+}
+
+static struct ast_cli_entry cli_logger[] = {
+	AST_CLI_DEFINE(handle_cli_dynamic_level_test, "Test the dynamic logger level implementation"),
+};
 
 static int unload_module(void)
 {
+	ast_cli_unregister_multiple(cli_logger, ARRAY_LEN(cli_logger));
 	return 0;
 }
 
 static int load_module(void)
 {
+	ast_cli_register_multiple(cli_logger, ARRAY_LEN(cli_logger));
 	return AST_MODULE_LOAD_SUCCESS;
 }
 
-AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Skeleton (sample) Test");
+AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Logger Test Module");

Propchange: team/kpfleming/dynamic_logger_levels/tests/test_logger.c
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: team/kpfleming/dynamic_logger_levels/tests/test_logger.c
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Propchange: team/kpfleming/dynamic_logger_levels/tests/test_logger.c
------------------------------------------------------------------------------
    svn:mergeinfo = 

Propchange: team/kpfleming/dynamic_logger_levels/tests/test_logger.c
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: team/kpfleming/dynamic_logger_levels/tests/test_sched.c
URL: http://svn.digium.com/svn-view/asterisk/team/kpfleming/dynamic_logger_levels/tests/test_sched.c?view=diff&rev=191993&r1=191992&r2=191993
==============================================================================
--- team/kpfleming/dynamic_logger_levels/tests/test_sched.c (original)
+++ team/kpfleming/dynamic_logger_levels/tests/test_sched.c Mon May  4 05:52:47 2009
@@ -168,7 +168,7 @@
 	case CLI_INIT:
 		e->command = "sched benchmark";
 		e->usage = ""
-			"Usage: sched test <num>\n"
+			"Usage: sched benchmark <num>\n"
 			"";
 		return NULL;
 	case CLI_GENERATE:

Modified: team/kpfleming/dynamic_logger_levels/tests/test_skel.c
URL: http://svn.digium.com/svn-view/asterisk/team/kpfleming/dynamic_logger_levels/tests/test_skel.c?view=diff&rev=191993&r1=191992&r2=191993
==============================================================================
--- team/kpfleming/dynamic_logger_levels/tests/test_skel.c (original)
+++ team/kpfleming/dynamic_logger_levels/tests/test_skel.c Mon May  4 05:52:47 2009
@@ -40,6 +40,7 @@
 #include "asterisk/module.h"
 #include "asterisk/lock.h"
 #include "asterisk/app.h"
+#include "asterisk/cli.h"
 
 static int unload_module(void)
 {




More information about the asterisk-commits mailing list