Fixes and improves parsing of config file.
[lcr.git] / trace.c
1 /*****************************************************************************\
2 **                                                                           **
3 ** Linux Call Router                                                         **
4 **                                                                           **
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg                                              **
7 **                                                                           **
8 ** trace functions                                                           **
9 **                                                                           **
10 \*****************************************************************************/ 
11
12 #include "main.h"
13
14 struct trace trace;
15 char trace_string[MAX_TRACE_ELEMENTS * 100 + 400];
16
17 static const char *spaces = "          ";
18
19 /*
20  * initializes a new trace
21  * all values will be reset
22  */
23 void _start_trace(const char *__file, int __line, int port, struct interface *interface, const char *caller, const char *dialing, int direction, int category, int serial, const char *name)
24 {
25         if (trace.name[0])
26                 PERROR("trace already started (name=%s) in file %s line %d\n", trace.name, __file, __line);
27         memset(&trace, 0, sizeof(struct trace));
28         trace.port = port;
29         if (interface)
30                 SCPY(trace.interface, interface->name);
31         if (caller) if (caller[0])
32                 SCPY(trace.caller, caller);
33         if (dialing) if (dialing[0])
34                 SCPY(trace.dialing, dialing);
35         trace.direction = direction;
36         trace.category = category;
37         trace.serial = serial;
38         if (name) if (name[0])
39                 SCPY(trace.name, name);
40         if (!trace.name[0])
41                 SCPY(trace.name, "<unknown>");
42         trace.sec = now_tv.tv_sec;
43         trace.usec = now_tv.tv_usec;
44 }
45
46
47 /*
48  * adds a new element to the trace
49  * if subelement is given, element will also contain a subelement
50  * if multiple subelements belong to same element, name must be equal for all subelements
51  */
52 void _add_trace(const char *__file, int __line, const char *name, const char *sub, const char *fmt, ...)
53 {
54         va_list args;
55
56         if (!trace.name[0])
57                 PERROR("trace not started in file %s line %d\n", __file, __line);
58         
59         /* check for required name value */
60         if (!name)
61                 goto nostring;
62         if (!name[0]) {
63                 nostring:
64                 PERROR("trace with name=%s gets element with no string\n", trace.name);
65                 return;
66         }
67         
68         /* write name, sub and value */
69         SCPY(trace.element[trace.elements].name, name);
70         if (sub) if (sub[0])
71                 SCPY(trace.element[trace.elements].sub, sub);
72         if (fmt) if (fmt[0]) {
73                 va_start(args, fmt);
74                 VUNPRINT(trace.element[trace.elements].value, sizeof(trace.element[trace.elements].value)-1, fmt, args);
75                 va_end(args);
76         }
77
78         /* increment elements */
79         trace.elements++;
80 }
81
82
83 /*
84  * prints trace to socket or log
85  * detail: 1 = brief, 2=short, 3=long
86  */
87 static char *print_trace(int detail, int port, char *interface, char *caller, char *dialing, int category)
88 {
89         char buffer[256];
90         time_t ti = trace.sec;
91         struct tm *tm;
92         struct mISDNport *mISDNport;
93         int i;
94
95         trace_string[0] = '\0'; // always clear string
96
97         if (detail < 1)
98                 return(NULL);
99
100         /* filter trace */
101         if (port >= 0 && trace.port >= 0)
102                 if (port != trace.port) return(NULL);
103         if (interface) if (interface[0] && trace.interface[0])
104                 if (!!strcasecmp(interface, trace.interface)) return(NULL);
105         if (caller) if (caller[0] && trace.caller[0])
106                 if (!!strncasecmp(caller, trace.caller, strlen(trace.caller))) return(NULL);
107         if (dialing) if (dialing[0] && trace.dialing[0])
108                 if (!!strncasecmp(dialing, trace.dialing, strlen(trace.dialing))) return(NULL);
109         if (category && trace.category)
110                 if (!(category & trace.category)) return(NULL);
111
112         /* head */
113         if (detail >= 3) {
114                 SCAT(trace_string, "------------------------------------------------------------------------------\n");
115                 /* "Port: 1 (BRI PTMP TE)" */
116                 if (trace.port >= 0) {
117                         mISDNport = mISDNport_first;
118                         while(mISDNport) {
119                                 if (mISDNport->portnum == trace.port)
120                                         break;
121                                 mISDNport = mISDNport->next;
122                         }
123                         if (mISDNport) {
124                                 SPRINT(buffer, "Port: %d (%s %s %s)", trace.port, (mISDNport->pri)?"PRI":"BRI", (mISDNport->ptp)?"PTP":"PTMP", (mISDNport->ntmode)?"NT":"TE");
125                                 /* copy interface, if we have a port */
126                                 if (mISDNport->ifport) if (mISDNport->ifport->interface)
127                                 SCPY(trace.interface, mISDNport->ifport->interface->name);
128                         } else
129                                 SPRINT(buffer, "Port: %d (does not exist)\n", trace.port);
130                         SCAT(trace_string, buffer);
131                 } else
132                         SCAT(trace_string, "Port: ---");
133
134                 if (trace.interface[0]) {
135                         /* "  Interface: 'Ext'" */
136                         SPRINT(buffer, "  Interface: '%s'", trace.interface);
137                         SCAT(trace_string, buffer);
138                 } else
139                         SCAT(trace_string, "  Interface: ---");
140                         
141                 if (trace.caller[0]) {
142                         /* "  Caller: '021256493'" */
143                         SPRINT(buffer, "  Caller: '%s'\n", trace.caller);
144                         SCAT(trace_string, buffer);
145                 } else
146                         SCAT(trace_string, "  Caller: ---\n");
147
148                 /* "Time: 25.08.73 05:14:39.282" */
149                 tm = localtime(&ti);
150                 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);
151                 SCAT(trace_string, buffer);
152
153                 if (trace.direction) {
154                         /* "  Direction: out" */
155                         SPRINT(buffer, "  Direction: %s", (trace.direction==DIRECTION_OUT)?"OUT":"IN");
156                         SCAT(trace_string, buffer);
157                 } else
158                         SCAT(trace_string, "  Direction: ---");
159
160                 if (trace.dialing[0]) {
161                         /* "  Dialing: '57077'" */
162                         SPRINT(buffer, "  Dialing: '%s'\n", trace.dialing);
163                         SCAT(trace_string, buffer);
164                 } else
165                         SCAT(trace_string, "  Dialing: ---\n");
166
167                 SCAT(trace_string, "------------------------------------------------------------------------------\n");
168         }
169
170         if (detail < 3) {
171                 tm = localtime(&ti);
172                 SPRINT(buffer, "%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);
173                 SCAT(trace_string, buffer);
174         }
175
176         /* "CH(45): CC_SETUP (net->user)" */
177         switch (trace.category) {
178                 case CATEGORY_CH:
179                 SCAT(trace_string, "CH");
180                 break;
181
182                 case CATEGORY_EP:
183                 SCAT(trace_string, "EP");
184                 break;
185
186                 default:
187                 SCAT(trace_string, "--");
188         }
189         if (trace.serial)
190                 SPRINT(buffer, "(%lu): %s", trace.serial, trace.name[0]?trace.name:"<unknown>");
191         else
192                 SPRINT(buffer, ": %s", trace.name[0]?trace.name:"<unknown>");
193         SCAT(trace_string, buffer);
194
195         /* elements */
196         switch(detail) {
197                 case 1: /* brief */
198                 if (trace.port >= 0) {
199                         SPRINT(buffer, "  port %d", trace.port);
200                         SCAT(trace_string, buffer);
201                 }
202                 i = 0;
203                 while(i < trace.elements) {
204                         SPRINT(buffer, "  %s", trace.element[i].name);
205                         if (i) if (!strcmp(trace.element[i].name, trace.element[i-1].name))
206                                 buffer[0] = '\0';
207                         SCAT(trace_string, buffer);
208                         if (trace.element[i].sub[0])
209                                 SPRINT(buffer, " %s=", trace.element[i].sub);
210                         else
211                                 SPRINT(buffer, " ");
212                         SCAT(trace_string, buffer);
213                         if (strchr(trace.element[i].value, ' '))
214                                 SPRINT(buffer, "'%s'", trace.element[i].value);
215                         else
216                                 SPRINT(buffer, "%s", trace.element[i].value);
217                         SCAT(trace_string, buffer);
218                         i++;
219                 }
220                 SCAT(trace_string, "\n");
221                 break;
222
223                 case 2: /* short */
224                 case 3: /* long */
225                 SCAT(trace_string, "\n");
226                 i = 0;
227                 while(i < trace.elements) {
228                         SPRINT(buffer, " %s%s", trace.element[i].name, &spaces[strlen(trace.element[i].name)]);
229                         if (i) if (!strcmp(trace.element[i].name, trace.element[i-1].name))
230                                 SPRINT(buffer, "           ");
231                         SCAT(trace_string, buffer);
232                         if (trace.element[i].sub[0])
233                                 SPRINT(buffer, " : %s%s = ", trace.element[i].sub, &spaces[strlen(trace.element[i].sub)]);
234                         else
235                                 SPRINT(buffer, " :              ");
236                         SCAT(trace_string, buffer);
237                         if (strchr(trace.element[i].value, ' '))
238                                 SPRINT(buffer, "'%s'\n", trace.element[i].value);
239                         else
240                                 SPRINT(buffer, "%s\n", trace.element[i].value);
241                         SCAT(trace_string, buffer);
242                         i++;
243                 }
244                 break;
245         }
246
247         /* end */
248         if (detail >= 3)
249                 SCAT(trace_string, "\n");
250         return(trace_string);
251 }
252
253
254 /*
255  * trace ends
256  * this function will put the trace to sockets and logfile, if requested
257  */
258 void _end_trace(const char *__file, int __line)
259 {
260         char *string;
261         FILE *fp;
262         struct admin_list       *admin;
263         struct admin_queue      *response, **responsep; /* response pointer */
264
265         if (!trace.name[0])
266                 PERROR("trace not started in file %s line %d\n", __file, __line);
267         
268         if (options.deb || options.log[0]) {
269                 string = print_trace(1, -1, NULL, NULL, NULL, 0);
270                 if (string) {
271                         /* process debug */
272                         if (options.deb)
273                                 debug(NULL, 0, "trace", string);
274                         /* process log */
275                         if (options.log[0]) {
276                                 fp = fopen(options.log, "a");
277                                 if (fp) {
278                                         fwrite(string, strlen(string), 1, fp);
279                                         fclose(fp);
280                                 }
281                         }
282                 }
283         }
284
285         /* process admin */
286         admin = admin_first;
287         while(admin) {
288                 if (admin->trace.detail) {
289                         string = print_trace(admin->trace.detail, admin->trace.port, admin->trace.interface, admin->trace.caller, admin->trace.dialing, admin->trace.category);
290                         if (string) {
291                                 /* seek to end of response list */
292                                 response = admin->response;
293                                 responsep = &admin->response;
294                                 while(response) {
295                                         responsep = &response->next;
296                                         response = response->next;
297                                 }
298
299                                 /* create state response */
300                                 response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
301                                 memuse++;
302                                 response->num = 1;
303                                 /* message */
304                                 response->am[0].message = ADMIN_TRACE_RESPONSE;
305                                 SCPY(response->am[0].u.trace_rsp.text, string);
306
307                                 /* attach to response chain */
308                                 *responsep = response;
309                                 responsep = &response->next;
310                         }
311                 }
312                 admin = admin->next;
313         }
314 //      fwrite(string, strlen(string), 1, fp);
315
316         memset(&trace, 0, sizeof(struct trace));
317 }
318
319
320
321