1 /*****************************************************************************\
3 ** Linux Call Router **
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg **
10 \*****************************************************************************/
15 char trace_string[MX_TRACE_ELEMENTS * 100 + 400];
17 static char *spaces[11] = {
32 * initializes a new trace
33 * all values will be reset
35 void start_trace(int port, char *interface, char *caller, char *dialing, int direction, int category, int serial, char *name);
38 PERROR("trace already started (name=%s)\n", trace.name);
39 memset(trace, 0, sizeof(struct trace));
41 if (interface) if (interface[0])
42 SCPY(trace.interface, interface);
43 if (caller) if (caller[0])
44 SCPY(trace.caller, caller);
45 if (dialing) if (dialing[0])
46 SCPY(trace.dialing, dialing);
47 trace.direction = direction;
48 trace.category = category;
49 trace.serial = serial;
50 if (name) if (name[0])
51 SCPY(trace.name, name);
52 trace.sec = now_tv.tv_sec;
53 trace.usec = now_tv.tv_usec;
58 * adds a new element to the trace
59 * if subelement is given, element will also contain a subelement
60 * if multiple subelements belong to same element, name must be equal for all subelements
62 void add_trace(char *name, char *sub, const char *fmt, ...);
67 PERROR("trace not started\n");
69 /* check for required name value */
75 PERROR("trace with name=%s gets element with no string\n", trace->name);
79 /* write name, sub and value */
80 SCPY(trace.element[trace.elements].name, name);
82 SCPY(trace.element[trace.elements].sub, sub);
86 VUNPRINT(trace.element[trace.element].value, sizeof(trace.element[trace.elements].value)-1, fmt, args);
90 /* increment elements */
97 * this function will put the trace to sockets and logfile, if requested
102 PERROR("trace not started\n");
104 /* process log file */
107 string = print_trace(1, 0, NULL, NULL, NULL, -1, "AP", CATEGORY_EP);
108 fwrite(string, strlen(string), 1, fp);
111 memset(trace, 0, sizeof(struct trace));
116 * prints trace to socket or log
117 * detail: 1 = brief, 2=short, 3=long
119 static char *print_trace(int detail, int port, char *interface, char *caller, char *dialing, int category);
121 trace_string[0] = '\0';
129 if (port && trace.port)
130 if (port != trace.port) return;
131 if (interface && interface[0] && trace.interface[0])
132 if (!!strcasecmp(interface, trace.interface)) return;
133 if (caller && caller[0] && trace.caller[0])
134 if (!!strcasecmp(caller, trace.caller)) return;
135 if (dialing && dialing[0] && trace.dialing[0])
136 if (!!strcasecmp(dialing, trace.dialing)) return;
137 if (category && category[0] && trace.category[0])
138 if (!!strcasecmp(category, trace.category)) return;
143 /* "Port: 1 (BRI PTMP TE)" */
146 mISDNport = mISDNport_first;
149 if (mISDNport->number == trace.port)
151 mISDNport = mISDNport->next;
154 SPRINT(buffer, "Port: %d (%s %s %s)", port, (mISDNport->pri)?"PRI":"BRI", (mISDNport->ptp)?"PTP":"PTMP", (mISDNport->nt)?"NT":"TE");
156 SPRINT(buffer, "Port: %d (does not exist}\n", port);
157 SCAT(trace_string, buffer);
159 SCAT(trace_string, "Port: ---");
161 if (trace.interface[0])
163 /* " Interface: 'Ext'" */
164 SPRINT(buffer, " Interface: '%s'", trace.interface);
165 SCAT(trace_string, buffer);
167 SCAT(trace_string, " Interface: ---");
171 /* " Caller: '021256493'" */
172 SPRINT(buffer, " Caller: '%s'\n", trace.caller);
173 SCAT(trace_string, buffer);
175 SCAT(trace_string, " Caller: ---\n");
177 /* "Time: 25.08.73 05:14:39.282" */
178 tm = localtime(&trace.sec);
179 SPRINT(buffer, "Time: %02d.%02d.%02d %02d:%02d:%02d.%03d", tm->tm_mday, tm->tm_mon+1, tm->tm_year%100, tm->tm_hour, tm->tm_min, tm->tm_sec, trace->usec/1000);
180 SCAT(trace_string, buffer);
184 /* " Direction: out" */
185 SPRINT(buffer, " Direction: %s", (trace.direction==DIRECTION_OUT)?"OUT":"IN");
186 SCAT(trace_string, buffer);
188 SCAT(trace_string, " Direction: ---");
190 if (trace.dialing[0])
192 /* " Dialing: '57077'" */
193 SPRINT(buffer, " Dialing: '%s'\n", trace.dialing);
194 SCAT(trace_string, buffer);
196 SCAT(trace_string, " Dialing: ---\n");
198 SCAT(trace_string, "------------------------------------------------------------------------------\n");
201 /* "CH(45): CC_SETUP (net->user)" */
202 switch (trace.category)
204 SCAT(trace_string, "CH");
208 SCAT(trace_string, "EP");
212 SCAT(trace_string, "--");
214 SPRINT(buffer, "(%d): %s", trace.serial, trace.name[0]?trace.name:"<unknown>");
215 SCAT(trace_string, buffer);
222 while(i < trace.elements)
224 SPRINT(buffer, " %s", trace.element[i].name);
225 if (i) if (!strcmp(trace.element[i].name, trace.element[i-1].name))
227 SCAT(trace_string, buffer);
228 if (trace.element[i].sub[0])
229 SPRINT(buffer, " %s=", trace.element[i].sub, value);
231 SPRINT(buffer, " ", value);
232 SCAT(trace_string, buffer);
233 if (strchr(value, ' '))
234 SPRINT(buffer, "'%s'", value);
236 SPRINT(buffer, "%s", value);
237 SCAT(trace_string, buffer);
240 SCAT(trace_string, "\n");
245 SCAT(trace_string, "\n");
247 while(i < trace.elements)
249 SPRINT(buffer, " %s%s", trace.element[i].name, spaces[strlen(trace.element[i].name)]);
250 if (i) if (!strcmp(trace.element[i].name, trace.element[i-1].name))
252 SCAT(trace_string, buffer);
253 if (trace.element[i].sub[0])
254 SPRINT(buffer, " : %s%s = ", trace.element[i].sub, spaces[strlen(trace.element[i].sub)], value);
256 SPRINT(buffer, " : ", value);
257 SCAT(trace_string, buffer);
258 if (strchr(value, ' '))
259 SPRINT(buffer, "'%s'\n", value);
261 SPRINT(buffer, "%s\n", value);
262 SCAT(trace_string, buffer);
270 SCAT(trace_string, "\n");