e5e9a2ed8882d53b88d816c5a1b620bf9d65d006
[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 char *spaces[11] = {
18         "          ",
19         "         ",
20         "        ",
21         "       ",
22         "      ",
23         "     ",
24         "    ",
25         "   ",
26         "  ",
27         " ",
28         "",
29 };
30
31 /*
32  * initializes a new trace
33  * all values will be reset
34  */
35 void start_trace(int port, struct interface *interface, char *caller, char *dialing, int direction, int category, int serial, char *name)
36 {
37         if (trace.name[0])
38                 PERROR("trace already started (name=%s)\n", trace.name);
39         memset(&trace, 0, sizeof(struct trace));
40         trace.port = port;
41         if (interface)
42                 SCPY(trace.interface, interface->name);
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;
54 }
55
56
57 /*
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
61  */
62 void add_trace(char *name, char *sub, const char *fmt, ...)
63 {
64         va_list args;
65
66         if (!trace.name[0])
67                 PERROR("trace not started\n");
68         
69         /* check for required name value */
70         if (!name)
71                 goto nostring;
72         if (!name[0])
73         {
74                 nostring:
75                 PERROR("trace with name=%s gets element with no string\n", trace.name);
76                 return;
77         }
78         
79         /* write name, sub and value */
80         SCPY(trace.element[trace.elements].name, name);
81         if (sub) if (sub[0])
82                 SCPY(trace.element[trace.elements].sub, sub);
83         if (fmt) if (fmt[0])
84         {
85                 va_start(args, fmt);
86                 VUNPRINT(trace.element[trace.elements].value, sizeof(trace.element[trace.elements].value)-1, fmt, args);
87                 va_end(args);
88         }
89
90         /* increment elements */
91         trace.elements++;
92 }
93
94
95 /*
96  * prints trace to socket or log
97  * detail: 1 = brief, 2=short, 3=long
98  */
99 static char *print_trace(int detail, int port, char *interface, char *caller, char *dialing, int category)
100 {
101         char buffer[256];
102         time_t ti = trace.sec;
103         struct tm *tm;
104         struct mISDNport *mISDNport;
105         int i;
106
107         trace_string[0] = '\0'; // always clear string
108
109         if (detail < 1)
110                 return(NULL);
111
112         /* filter trace */
113         if (port && trace.port)
114                 if (port != trace.port) return(NULL);
115         if (interface) if (interface[0] && trace.interface[0])
116                 if (!!strcasecmp(interface, trace.interface)) return(NULL);
117         if (caller) if (caller[0] && trace.caller[0])
118                 if (!!strcasecmp(caller, trace.caller)) return(NULL);
119         if (dialing) if (dialing[0] && trace.dialing[0])
120                 if (!!strcasecmp(dialing, trace.dialing)) return(NULL);
121         if (category && trace.category)
122                 if (category != trace.category) return(NULL);
123
124         /* head */
125         if (detail >= 3)
126         {
127                 /* "Port: 1 (BRI PTMP TE)" */
128                 if (port)
129                 {
130                         mISDNport = mISDNport_first;
131                         while(mISDNport)
132                         {
133                                 if (mISDNport->portnum == trace.port)
134                                         break;
135                                 mISDNport = mISDNport->next;
136                         }
137                         if (mISDNport)
138                                 SPRINT(buffer, "Port: %d (%s %s %s)", port, (mISDNport->pri)?"PRI":"BRI", (mISDNport->ptp)?"PTP":"PTMP", (mISDNport->ntmode)?"NT":"TE");
139                         else
140                                 SPRINT(buffer, "Port: %d (does not exist}\n", port);
141                         SCAT(trace_string, buffer);
142                 } else
143                         SCAT(trace_string, "Port: ---");
144
145                 if (trace.interface[0])
146                 {
147                         /* "  Interface: 'Ext'" */
148                         SPRINT(buffer, "  Interface: '%s'", trace.interface);
149                         SCAT(trace_string, buffer);
150                 } else
151                         SCAT(trace_string, "  Interface: ---");
152                         
153                 if (trace.caller[0])
154                 {
155                         /* "  Caller: '021256493'" */
156                         SPRINT(buffer, "  Caller: '%s'\n", trace.caller);
157                         SCAT(trace_string, buffer);
158                 } else
159                         SCAT(trace_string, "  Caller: ---\n");
160
161                 /* "Time: 25.08.73 05:14:39.282" */
162                 tm = localtime(&ti);
163                 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);
164                 SCAT(trace_string, buffer);
165
166                 if (trace.direction)
167                 {
168                         /* "  Direction: out" */
169                         SPRINT(buffer, "  Direction: %s", (trace.direction==DIRECTION_OUT)?"OUT":"IN");
170                         SCAT(trace_string, buffer);
171                 } else
172                         SCAT(trace_string, "  Direction: ---");
173
174                 if (trace.dialing[0])
175                 {
176                         /* "  Dialing: '57077'" */
177                         SPRINT(buffer, "  Dialing: '%s'\n", trace.dialing);
178                         SCAT(trace_string, buffer);
179                 } else
180                         SCAT(trace_string, "  Dialing: ---\n");
181
182                 SCAT(trace_string, "------------------------------------------------------------------------------\n");
183         }
184
185         /* "CH(45): CC_SETUP (net->user)" */
186         switch (trace.category)
187         {       case CATEGORY_CH:
188                 SCAT(trace_string, "CH");
189                 break;
190
191                 case CATEGORY_EP:
192                 SCAT(trace_string, "EP");
193                 break;
194
195                 default:
196                 SCAT(trace_string, "--");
197         }
198         if (trace.serial)
199                 SPRINT(buffer, "(%d): %s", trace.serial, trace.name[0]?trace.name:"<unknown>");
200         else
201                 SPRINT(buffer, ": %s", trace.name[0]?trace.name:"<unknown>");
202         SCAT(trace_string, buffer);
203
204         /* elements */
205         switch(detail)
206         {
207                 case 1: /* brief */
208                 i = 0;
209                 while(i < trace.elements)
210                 {
211                         SPRINT(buffer, "  %s", trace.element[i].name);
212                         if (i) if (!strcmp(trace.element[i].name, trace.element[i-1].name))
213                                 buffer[0] = '\0';
214                         SCAT(trace_string, buffer);
215                         if (trace.element[i].sub[0])
216                                 SPRINT(buffer, " %s=", trace.element[i].sub);
217                         else
218                                 SPRINT(buffer, " ");
219                         SCAT(trace_string, buffer);
220                         if (strchr(trace.element[i].value, ' '))
221                                 SPRINT(buffer, "'%s'", trace.element[i].value);
222                         else
223                                 SPRINT(buffer, "%s", trace.element[i].value);
224                         SCAT(trace_string, buffer);
225                         i++;
226                 }
227                 SCAT(trace_string, "\n");
228                 break;
229
230                 case 2: /* short */
231                 case 3: /* long */
232                 SCAT(trace_string, "\n");
233                 i = 0;
234                 while(i < trace.elements)
235                 {
236                         SPRINT(buffer, " %s%s", trace.element[i].name, spaces[strlen(trace.element[i].name)]);
237                         if (i) if (!strcmp(trace.element[i].name, trace.element[i-1].name))
238                                 SPRINT(buffer, "           ");
239                         SCAT(trace_string, buffer);
240                         if (trace.element[i].sub[0])
241                                 SPRINT(buffer, " : %s%s = ", trace.element[i].sub, spaces[strlen(trace.element[i].sub)]);
242                         else
243                                 SPRINT(buffer, " :              ");
244                         SCAT(trace_string, buffer);
245                         if (strchr(trace.element[i].value, ' '))
246                                 SPRINT(buffer, "'%s'\n", trace.element[i].value);
247                         else
248                                 SPRINT(buffer, "%s\n", trace.element[i].value);
249                         SCAT(trace_string, buffer);
250                         i++;
251                 }
252                 break;
253         }
254
255         /* end */
256         if (detail >= 3)
257                 SCAT(trace_string, "\n");
258         return(trace_string);
259 }
260
261
262 /*
263  * trace ends
264  * this function will put the trace to sockets and logfile, if requested
265  */
266 void end_trace(void)
267 {
268         char *string;
269
270         if (!trace.name[0])
271                 PERROR("trace not started\n");
272         
273         /* process log file */
274         if (options.deb)
275         {
276                 string = print_trace(1, 0, NULL, NULL, NULL, 0);
277                 if (string)
278                 {
279                         debug(NULL, 0, "trace", string);
280                 }
281         }
282 printf("%s", print_trace(3, 0, NULL, NULL, NULL, 0));
283 #warning trace auch zum socket
284 //      fwrite(string, strlen(string), 1, fp);
285
286         memset(&trace, 0, sizeof(struct trace));
287 }
288
289
290
291
292