Raw File
1 #include <stdlib.h> // for exit...
2 #include <time.h>
3 #include <pthread.h>
4 #include <stdio.h>
5 #define DEBUG_VARS 1 // need this so we don't get extern defs
6 #include "debug.h"
7
8 int log_level = 0;
9 FILE *log_file;
10 int trace_flags = 0;
11 char *log_desc[LOG_TRACE + 1];
12 pthread_mutex_t log_mutex;
13
14 char *get_trace_type(int type) {
15 switch(type) {
16 case TRACE_MODEM_IN:
17 return "RS<-";
18 case TRACE_MODEM_OUT:
19 return "RS->";
20 case TRACE_SERIAL_IN:
21 return "SR<-";
22 case TRACE_SERIAL_OUT:
23 return "SR->";
24 case TRACE_IP_IN:
25 return "IP<-";
26 case TRACE_IP_OUT:
27 return "IP->";
28 }
29 return "NONE";
30 }
31
32 int log_init() {
33 log_file = stdout;
34 log_level = 0;
35 trace_flags = 0;
36 log_desc[LOG_FATAL] = "FATAL";
37 log_desc[LOG_ERROR] = "ERROR";
38 log_desc[LOG_WARN] = "WARN";
39 log_desc[LOG_INFO] = "INFO";
40 log_desc[LOG_DEBUG] = "DEBUG";
41 log_desc[LOG_ENTER_EXIT] = "ENTER_EXIT";
42 log_desc[LOG_ALL] = "DEBUG_X";
43 log_desc[LOG_TRACE]= "TRACE";
44 if( -1 == pthread_mutex_init(&log_mutex, NULL)) {
45 perror("Could not create Log Mutex");
46 exit(-1);
47 }
48 return 0;
49 }
50
51 void log_set_file(FILE *a) {
52 log_file = a;
53 }
54
55 void log_set_level(int a) {
56 log_level = a;
57 }
58
59 void log_set_trace_flags(int a) {
60 trace_flags = a;
61 }
62
63 int log_get_trace_flags() {
64 return trace_flags;
65 }
66
67 void log_trace(int type, unsigned char *line, int len) {
68 int i = 0;
69 int ch;
70 char data[64] = "\0";
71 char *dptr = NULL;
72 char text[17];
73
74 if(len == 0)
75 return;
76
77 if((type & trace_flags) != 0) {
78 text[16] = 0;
79 for(i = 0; i < len; i++) {
80 if((i % 16) == 0) {
81 // beginning of line
82 dptr = data;
83 sprintf(dptr, "%4.4x|", i);
84 }
85 ch = line[i];
86 sprintf(dptr + 5 + ((i % 16) * 3), "%2.2x", ch);
87 if(ch > 31 && ch < 127) {
88 text[i % 16] = ch;
89 } else {
90 text[i % 16] = '.';
91 }
92 if((i % 16) == 15) {
93 log_start(LOG_TRACE);
94 fprintf(log_file, "%s|%s|%s|", get_trace_type(type), data, text);
95 log_end();
96 } else {
97 sprintf(dptr + 7 + ((i % 16) * 3), " ");
98 }
99 }
100 i = i % 16;
101 if(i > 0) {
102 for(; i < 16; i++) {
103 sprintf(dptr + 5 + ((i % 16) * 3), " ");
104 if((i % 16) != 15) {
105 sprintf(dptr + 7 + ((i % 16) * 3), " ");
106 }
107 text[i % 16] = ' ';
108 }
109 log_start(LOG_TRACE);
110 fprintf(log_file, "%s|%s|%s|", get_trace_type(type), data, text);
111 }
112 log_end();
113 }
114 }
115
116 void log_start(int level) {
117 char t[23];
118 time_t now;
119
120 if(-1 == pthread_mutex_lock(&log_mutex)) {
121 perror("Could not lock the log mutex");
122 } else {
123 // we have the lock.
124 now = time(NULL);
125 strftime(t, 22, "%Y-%m-%d %H:%M:%S", localtime(&now));
126 fprintf(log_file, "%s:%5.5ld:%s:", t, (long)pthread_self(), log_desc[level]);
127 //free(t);
128 }
129 }
130
131 void log_end() {
132 fprintf(log_file, "\n");
133 fflush(log_file);
134 if(-1 == pthread_mutex_unlock(&log_mutex)) {
135 perror("Could not lock the log mutex");
136 }
137 }
138
Generated by GNU Enscript 1.6.6, and GophHub 1.3.