Browse code

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")

Andrei Pelinescu-Onciul authored on 09/08/2010 15:13:40
Showing 1 changed files
... ...
@@ -125,24 +125,52 @@ struct module_exports exports= {
125 125
 };
126 126
 
127 127
 
128
+
129
+/** parse the the script_counter modparam.
130
+ *  Format:   [grp.]name[( |:)desc]
131
+ *  E.g.:
132
+ *           "name" => new counter: *cnt_script_grp."name"
133
+ *           "grp.name" => new counter: "grp"."name"
134
+ *           "name desc" => new counter "name", desc = "desc"
135
+ *           "grp.name desc" => "grp"."name", desc = "desc".
136
+ */
128 137
 static int add_script_counter(modparam_t type, void* val)
129 138
 {
130 139
 	char* name;
131 140
 	counter_handle_t h;
132 141
 	int ret;
142
+	char* grp;
143
+	char* desc;
144
+	char* p;
133 145
 
134 146
 	if ((type & PARAM_STRING) == 0) {
135 147
 		BUG("bad parameter type %d\n", type);
136 148
 		goto error;
137 149
 	}
138 150
 	name = (char*) val;
139
-	ret = counter_register(&h, cnt_script_grp, name, 0, 0, 0, "script counter", 0);
151
+	grp = cnt_script_grp; /* default group */
152
+	desc = "custom script counter."; /* default desc. */
153
+	if ((p = strchr(name, ':')) != 0 ||
154
+			(p = strchr(name, ' ')) != 0) {
155
+		/* found desc. */
156
+		*p = 0;
157
+		for(p = p+1; *p && (*p == ' ' || *p == '\t'); p++);
158
+		if (*p)
159
+			desc = p;
160
+	}
161
+	if ((p = strchr(name, '.')) != 0) {
162
+		/* found group */
163
+		grp = name;
164
+		*p = 0;
165
+		name = p+1;
166
+	}
167
+	ret = counter_register(&h, grp, name, 0, 0, 0, desc, 0);
140 168
 	if (ret < 0) {
141 169
 		if (ret == -2) {
142
-			ERR("counter %s.%s already registered\n", cnt_script_grp, name);
170
+			ERR("counter %s.%s already registered\n", grp, name);
143 171
 			return 0;
144 172
 		}
145
-		ERR("failed to register counter %s.%s\n", cnt_script_grp, name);
173
+		ERR("failed to register counter %s.%s\n", grp, name);
146 174
 		goto error;
147 175
 	}
148 176
 	return 0;
... ...
@@ -155,12 +183,21 @@ error:
155 183
 static int cnt_fixup1(void** param, int param_no)
156 184
 {
157 185
 	char* name;
186
+	char* grp;
187
+	char* p;
158 188
 	counter_handle_t h;
159 189
 
160 190
 	name = (char*)*param;
161
-	if (counter_lookup(&h, cnt_script_grp, name) < 0) {
191
+	grp = cnt_script_grp; /* default group */
192
+	if ((p = strchr(name, '.')) != 0) {
193
+		/* found group */
194
+		grp = name;
195
+		name = p+1;
196
+		*p = 0;
197
+	}
198
+	if (counter_lookup(&h, grp, name) < 0) {
162 199
 		ERR("counter %s.%s does not exist (forgot to define it?)\n",
163
-				cnt_script_grp, name);
200
+				grp, name);
164 201
 		return -1;
165 202
 	}
166 203
 	*param = (void*)(long)h.id;
... ...
@@ -172,13 +209,22 @@ static int cnt_fixup1(void** param, int param_no)
172 209
 static int cnt_int_fixup(void** param, int param_no)
173 210
 {
174 211
 	char* name;
212
+	char* grp;
213
+	char* p;
175 214
 	counter_handle_t h;
176 215
 
177 216
 	if (param_no == 1) {
178 217
 		name = (char*)*param;
179
-		if (counter_lookup(&h, cnt_script_grp, name) < 0) {
218
+		grp = cnt_script_grp; /* default group */
219
+		if ((p = strchr(name, '.')) != 0) {
220
+			/* found group */
221
+			grp = name;
222
+			name = p+1;
223
+			*p = 0;
224
+		}
225
+		if (counter_lookup(&h, grp, name) < 0) {
180 226
 			ERR("counter %s.%s does not exist (forgot to define it?)\n",
181
-					cnt_script_grp, name);
227
+					grp, name);
182 228
 			return -1;
183 229
 		}
184 230
 		*param = (void*)(long)h.id;