backup
[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[MX_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, char *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) 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;
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.element].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  * trace ends
97  * this function will put the trace to sockets and logfile, if requested
98  */
99 void end_trace(void);
100 {
101         if (!trace.name[0])
102                 PERROR("trace not started\n");
103         
104         /* process log file */
105         if (options.log[0])
106         {
107                 string = print_trace(1, 0, NULL, NULL, NULL, -1, "AP", CATEGORY_EP);
108                 fwrite(string, strlen(string), 1, fp);
109         }
110
111         memset(trace, 0, sizeof(struct trace));
112 }
113
114
115 /*
116  * prints trace to socket or log
117  * detail: 1 = brief, 2=short, 3=long
118  */
119 static char *print_trace(int detail, int port, char *interface, char *caller, char *dialing, int category);
120 {
121         trace_string[0] = '\0';
122         char buffer[256];
123         struct tm *tm;
124
125         if (detail < 1)
126                 return;
127
128         /* filter trace */
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;
139
140         /* head */
141         if (detail >= 3)
142         {
143                 /* "Port: 1 (BRI PTMP TE)" */
144                 if (port)
145                 {
146                         mISDNport = mISDNport_first;
147                         while(mISDNport)
148                         {
149                                 if (mISDNport->number == trace.port)
150                                         break;
151                                 mISDNport = mISDNport->next;
152                         }
153                         if (mISDNport)
154                                 SPRINT(buffer, "Port: %d (%s %s %s)", port, (mISDNport->pri)?"PRI":"BRI", (mISDNport->ptp)?"PTP":"PTMP", (mISDNport->nt)?"NT":"TE");
155                         else
156                                 SPRINT(buffer, "Port: %d (does not exist}\n", port);
157                         SCAT(trace_string, buffer);
158                 } else
159                         SCAT(trace_string, "Port: ---");
160
161                 if (trace.interface[0])
162                 {
163                         /* "  Interface: 'Ext'" */
164                         SPRINT(buffer, "  Interface: '%s'", trace.interface);
165                         SCAT(trace_string, buffer);
166                 } else
167                         SCAT(trace_string, "  Interface: ---");
168                         
169                 if (trace.caller[0])
170                 {
171                         /* "  Caller: '021256493'" */
172                         SPRINT(buffer, "  Caller: '%s'\n", trace.caller);
173                         SCAT(trace_string, buffer);
174                 } else
175                         SCAT(trace_string, "  Caller: ---\n");
176
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);
181
182                 if (trace.direction)
183                 {
184                         /* "  Direction: out" */
185                         SPRINT(buffer, "  Direction: %s", (trace.direction==DIRECTION_OUT)?"OUT":"IN");
186                         SCAT(trace_string, buffer);
187                 } else
188                         SCAT(trace_string, "  Direction: ---");
189
190                 if (trace.dialing[0])
191                 {
192                         /* "  Dialing: '57077'" */
193                         SPRINT(buffer, "  Dialing: '%s'\n", trace.dialing);
194                         SCAT(trace_string, buffer);
195                 } else
196                         SCAT(trace_string, "  Dialing: ---\n");
197
198                 SCAT(trace_string, "------------------------------------------------------------------------------\n");
199         }
200
201         /* "CH(45): CC_SETUP (net->user)" */
202         switch (trace.category)
203         {       case CATEGORY_CH:
204                 SCAT(trace_string, "CH");
205                 break;
206
207                 case CATEGORY_EP:
208                 SCAT(trace_string, "EP");
209                 break;
210
211                 default:
212                 SCAT(trace_string, "--");
213         }
214         SPRINT(buffer, "(%d): %s", trace.serial, trace.name[0]?trace.name:"<unknown>");
215         SCAT(trace_string, buffer);
216
217         /* elements */
218         switch(detail)
219         {
220                 case 1: /* brief */
221                 i = 0;
222                 while(i < trace.elements)
223                 {
224                         SPRINT(buffer, "  %s", trace.element[i].name);
225                         if (i) if (!strcmp(trace.element[i].name, trace.element[i-1].name))
226                                 buffer[0] = '\0';
227                         SCAT(trace_string, buffer);
228                         if (trace.element[i].sub[0])
229                                 SPRINT(buffer, " %s=", trace.element[i].sub, value);
230                         else
231                                 SPRINT(buffer, " ", value);
232                         SCAT(trace_string, buffer);
233                         if (strchr(value, ' '))
234                                 SPRINT(buffer, "'%s'", value);
235                         else
236                                 SPRINT(buffer, "%s", value);
237                         SCAT(trace_string, buffer);
238                         i++;
239                 }
240                 SCAT(trace_string, "\n");
241                 break;
242
243                 case 2: /* short */
244                 case 3: /* long */
245                 SCAT(trace_string, "\n");
246                 i = 0;
247                 while(i < trace.elements)
248                 {
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))
251                                 SPRINT(buffer, "           ");
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);
255                         else
256                                 SPRINT(buffer, " :              ", value);
257                         SCAT(trace_string, buffer);
258                         if (strchr(value, ' '))
259                                 SPRINT(buffer, "'%s'\n", value);
260                         else
261                                 SPRINT(buffer, "%s\n", value);
262                         SCAT(trace_string, buffer);
263                         i++;
264                 }
265                 break;
266         }
267
268         /* end */
269         if (detail >= 3)
270                 SCAT(trace_string, "\n");
271 }
272
273
274
275 ^todo:
276 socket
277 file open
278
279