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