fixed so, PRI/BRI NT/TE works
[lcr.git] / trace.c
diff --git a/trace.c b/trace.c
index e5e9a2e..42c7146 100644 (file)
--- a/trace.c
+++ b/trace.c
@@ -32,10 +32,10 @@ static char *spaces[11] = {
  * initializes a new trace
  * all values will be reset
  */
-void start_trace(int port, struct interface *interface, char *caller, char *dialing, int direction, int category, int serial, char *name)
+void _start_trace(const char *__file, int __line, int port, struct interface *interface, char *caller, char *dialing, int direction, int category, int serial, char *name)
 {
        if (trace.name[0])
-               PERROR("trace already started (name=%s)\n", trace.name);
+               PERROR("trace already started (name=%s) in file %s line %d\n", trace.name, __file, __line);
        memset(&trace, 0, sizeof(struct trace));
        trace.port = port;
        if (interface)
@@ -49,6 +49,8 @@ void start_trace(int port, struct interface *interface, char *caller, char *dial
        trace.serial = serial;
        if (name) if (name[0])
                SCPY(trace.name, name);
+       if (!trace.name[0])
+               SCPY(trace.name, "<unknown>");
        trace.sec = now_tv.tv_sec;
        trace.usec = now_tv.tv_usec;
 }
@@ -59,12 +61,12 @@ void start_trace(int port, struct interface *interface, char *caller, char *dial
  * if subelement is given, element will also contain a subelement
  * if multiple subelements belong to same element, name must be equal for all subelements
  */
-void add_trace(char *name, char *sub, const char *fmt, ...)
+void _add_trace(const char *__file, int __line, char *name, char *sub, const char *fmt, ...)
 {
        va_list args;
 
        if (!trace.name[0])
-               PERROR("trace not started\n");
+               PERROR("trace not started in file %s line %d\n", __file, __line);
        
        /* check for required name value */
        if (!name)
@@ -115,17 +117,18 @@ static char *print_trace(int detail, int port, char *interface, char *caller, ch
        if (interface) if (interface[0] && trace.interface[0])
                if (!!strcasecmp(interface, trace.interface)) return(NULL);
        if (caller) if (caller[0] && trace.caller[0])
-               if (!!strcasecmp(caller, trace.caller)) return(NULL);
+               if (!!strncasecmp(caller, trace.caller, strlen(trace.caller))) return(NULL);
        if (dialing) if (dialing[0] && trace.dialing[0])
-               if (!!strcasecmp(dialing, trace.dialing)) return(NULL);
+               if (!!strncasecmp(dialing, trace.dialing, strlen(trace.dialing))) return(NULL);
        if (category && trace.category)
-               if (category != trace.category) return(NULL);
+               if (!(category & trace.category)) return(NULL);
 
        /* head */
        if (detail >= 3)
        {
+               SCAT(trace_string, "------------------------------------------------------------------------------\n");
                /* "Port: 1 (BRI PTMP TE)" */
-               if (port)
+               if (trace.port)
                {
                        mISDNport = mISDNport_first;
                        while(mISDNport)
@@ -135,9 +138,13 @@ static char *print_trace(int detail, int port, char *interface, char *caller, ch
                                mISDNport = mISDNport->next;
                        }
                        if (mISDNport)
-                               SPRINT(buffer, "Port: %d (%s %s %s)", port, (mISDNport->pri)?"PRI":"BRI", (mISDNport->ptp)?"PTP":"PTMP", (mISDNport->ntmode)?"NT":"TE");
-                       else
-                               SPRINT(buffer, "Port: %d (does not exist}\n", port);
+                       {
+                               SPRINT(buffer, "Port: %d (%s %s %s)", trace.port, (mISDNport->pri)?"PRI":"BRI", (mISDNport->ptp)?"PTP":"PTMP", (mISDNport->ntmode)?"NT":"TE");
+                               /* copy interface, if we have a port */
+                               if (mISDNport->ifport) if (mISDNport->ifport->interface)
+                               SCPY(trace.interface, mISDNport->ifport->interface->name);
+                       } else
+                               SPRINT(buffer, "Port: %d (does not exist)\n", trace.port);
                        SCAT(trace_string, buffer);
                } else
                        SCAT(trace_string, "Port: ---");
@@ -182,6 +189,13 @@ static char *print_trace(int detail, int port, char *interface, char *caller, ch
                SCAT(trace_string, "------------------------------------------------------------------------------\n");
        }
 
+       if (detail < 3)
+       {
+               tm = localtime(&ti);
+               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);
+               SCAT(trace_string, buffer);
+       }
+
        /* "CH(45): CC_SETUP (net->user)" */
        switch (trace.category)
        {       case CATEGORY_CH:
@@ -196,7 +210,7 @@ static char *print_trace(int detail, int port, char *interface, char *caller, ch
                SCAT(trace_string, "--");
        }
        if (trace.serial)
-               SPRINT(buffer, "(%d): %s", trace.serial, trace.name[0]?trace.name:"<unknown>");
+               SPRINT(buffer, "(%lu): %s", trace.serial, trace.name[0]?trace.name:"<unknown>");
        else
                SPRINT(buffer, ": %s", trace.name[0]?trace.name:"<unknown>");
        SCAT(trace_string, buffer);
@@ -205,6 +219,11 @@ static char *print_trace(int detail, int port, char *interface, char *caller, ch
        switch(detail)
        {
                case 1: /* brief */
+               if (trace.port)
+               {
+                       SPRINT(buffer, "  port %d", trace.port);
+                       SCAT(trace_string, buffer);
+               }
                i = 0;
                while(i < trace.elements)
                {
@@ -263,24 +282,70 @@ static char *print_trace(int detail, int port, char *interface, char *caller, ch
  * trace ends
  * this function will put the trace to sockets and logfile, if requested
  */
-void end_trace(void)
+void _end_trace(const char *__file, int __line)
 {
        char *string;
+       FILE *fp;
+       struct admin_list       *admin;
+       struct admin_queue      *response, **responsep; /* response pointer */
 
        if (!trace.name[0])
-               PERROR("trace not started\n");
+               PERROR("trace not started in file %s line %d\n", __file, __line);
        
-       /* process log file */
-       if (options.deb)
+       if (options.deb || options.log[0])
        {
                string = print_trace(1, 0, NULL, NULL, NULL, 0);
                if (string)
                {
-                       debug(NULL, 0, "trace", string);
+                       /* process debug */
+                       if (options.deb)
+                               debug(NULL, 0, "trace", string);
+                       /* process log */
+                       if (options.log[0])
+                       {
+                               fp = fopen(options.log, "a");
+                               if (fp)
+                               {
+                                       fwrite(string, strlen(string), 1, fp);
+                                       fclose(fp);
+                               }
+                       }
                }
        }
-printf("%s", print_trace(3, 0, NULL, NULL, NULL, 0));
-#warning trace auch zum socket
+
+       /* process admin */
+       admin = admin_first;
+       while(admin)
+       {
+               if (admin->trace.detail)
+               {
+                       string = print_trace(admin->trace.detail, admin->trace.port, admin->trace.interface, admin->trace.caller, admin->trace.dialing, admin->trace.category);
+                       if (string)
+                       {
+                               /* seek to end of response list */
+                               response = admin->response;
+                               responsep = &admin->response;
+                               while(response)
+                               {
+                                       responsep = &response->next;
+                                       response = response->next;
+                               }
+
+                               /* create state response */
+                               response = (struct admin_queue *)MALLOC(sizeof(struct admin_queue)+sizeof(admin_message));
+                               memuse++;
+                               response->num = 1;
+                               /* message */
+                               response->am[0].message = ADMIN_TRACE_RESPONSE;
+                               SCPY(response->am[0].u.trace_rsp.text, string);
+
+                               /* attach to response chain */
+                               *responsep = response;
+                               responsep = &response->next;
+                       }
+               }
+               admin = admin->next;
+       }
 //     fwrite(string, strlen(string), 1, fp);
 
        memset(&trace, 0, sizeof(struct trace));
@@ -289,4 +354,3 @@ printf("%s", print_trace(3, 0, NULL, NULL, NULL, 0));
 
 
 
-