[sr-dev] git:andrei/counters: counters: extended declaration syntax

Andrei Pelinescu-Onciul andrei at iptel.org
Mon Aug 9 17:45:13 CEST 2010


Module: sip-router
Branch: andrei/counters
Commit: e43c19c98cc2c6c26cab2da66e85803e95ff521c
URL:    http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=e43c19c98cc2c6c26cab2da66e85803e95ff521c

Author: Andrei Pelinescu-Onciul <andrei at iptel.org>
Committer: Andrei Pelinescu-Onciul <andrei at iptel.org>
Date:   Mon Aug  9 17:13:40 2010 +0200

counters: extended declaration syntax

Counters declarations can now include a group and a description.
E.g.:
modparam("counters", "script_counter", "test.foo example counter)
(declares counter foo in group test with the description
 "example counter")

The format for the declarations is: [grp.]name[( |:)desc]
'.' separates the group from the counter name and ' ' or ':' the
name from the description.
If the group is missing, the group defined by script_cnt_grp_name
will be used (default "script"). If the description is missing,
 "custom script counter." will be used.

Counter manipulating script functions can now include a group.
The format for the script functions counter parameters is:
 [grp.]name.
E.g.:
cnt.inc("test.foo")

---

 modules/counters/counters.c |   60 ++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 53 insertions(+), 7 deletions(-)

diff --git a/modules/counters/counters.c b/modules/counters/counters.c
index 3c77fef..1755308 100644
--- a/modules/counters/counters.c
+++ b/modules/counters/counters.c
@@ -125,24 +125,52 @@ struct module_exports exports= {
 };
 
 
+
+/** parse the the script_counter modparam.
+ *  Format:   [grp.]name[( |:)desc]
+ *  E.g.:
+ *           "name" => new counter: *cnt_script_grp."name"
+ *           "grp.name" => new counter: "grp"."name"
+ *           "name desc" => new counter "name", desc = "desc"
+ *           "grp.name desc" => "grp"."name", desc = "desc".
+ */
 static int add_script_counter(modparam_t type, void* val)
 {
 	char* name;
 	counter_handle_t h;
 	int ret;
+	char* grp;
+	char* desc;
+	char* p;
 
 	if ((type & PARAM_STRING) == 0) {
 		BUG("bad parameter type %d\n", type);
 		goto error;
 	}
 	name = (char*) val;
-	ret = counter_register(&h, cnt_script_grp, name, 0, 0, 0, "script counter", 0);
+	grp = cnt_script_grp; /* default group */
+	desc = "custom script counter."; /* default desc. */
+	if ((p = strchr(name, ':')) != 0 ||
+			(p = strchr(name, ' ')) != 0) {
+		/* found desc. */
+		*p = 0;
+		for(p = p+1; *p && (*p == ' ' || *p == '\t'); p++);
+		if (*p)
+			desc = p;
+	}
+	if ((p = strchr(name, '.')) != 0) {
+		/* found group */
+		grp = name;
+		*p = 0;
+		name = p+1;
+	}
+	ret = counter_register(&h, grp, name, 0, 0, 0, desc, 0);
 	if (ret < 0) {
 		if (ret == -2) {
-			ERR("counter %s.%s already registered\n", cnt_script_grp, name);
+			ERR("counter %s.%s already registered\n", grp, name);
 			return 0;
 		}
-		ERR("failed to register counter %s.%s\n", cnt_script_grp, name);
+		ERR("failed to register counter %s.%s\n", grp, name);
 		goto error;
 	}
 	return 0;
@@ -155,12 +183,21 @@ error:
 static int cnt_fixup1(void** param, int param_no)
 {
 	char* name;
+	char* grp;
+	char* p;
 	counter_handle_t h;
 
 	name = (char*)*param;
-	if (counter_lookup(&h, cnt_script_grp, name) < 0) {
+	grp = cnt_script_grp; /* default group */
+	if ((p = strchr(name, '.')) != 0) {
+		/* found group */
+		grp = name;
+		name = p+1;
+		*p = 0;
+	}
+	if (counter_lookup(&h, grp, name) < 0) {
 		ERR("counter %s.%s does not exist (forgot to define it?)\n",
-				cnt_script_grp, name);
+				grp, name);
 		return -1;
 	}
 	*param = (void*)(long)h.id;
@@ -172,13 +209,22 @@ static int cnt_fixup1(void** param, int param_no)
 static int cnt_int_fixup(void** param, int param_no)
 {
 	char* name;
+	char* grp;
+	char* p;
 	counter_handle_t h;
 
 	if (param_no == 1) {
 		name = (char*)*param;
-		if (counter_lookup(&h, cnt_script_grp, name) < 0) {
+		grp = cnt_script_grp; /* default group */
+		if ((p = strchr(name, '.')) != 0) {
+			/* found group */
+			grp = name;
+			name = p+1;
+			*p = 0;
+		}
+		if (counter_lookup(&h, grp, name) < 0) {
 			ERR("counter %s.%s does not exist (forgot to define it?)\n",
-					cnt_script_grp, name);
+					grp, name);
 			return -1;
 		}
 		*param = (void*)(long)h.id;




More information about the sr-dev mailing list