root/src/router/openlldp/src/lldp_debug.c

Revision 8347, 6.2 kB (checked in by BrainSlayer, 2 years ago)

openlldp added, maybe usefull

Line 
1 /** @file lldp_debug.c
2  *
3  * OpenLLDP Debug Core
4  *
5  * Licensed under a dual GPL/Proprietary license. 
6  * See LICENSE file for more info.
7  *
8  * File: lldp_debug.c
9  *
10  * Authors: Terry Simons (terry.simons@gmail.com)
11  *
12  * Some code below was borrowed from the Open1X project.
13  *
14  *******************************************************************/
15
16 #include <stdio.h>
17 #include <stdarg.h>
18 #include <strings.h>
19 #include <string.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <ctype.h>
23 #include <syslog.h>
24 #include <stdint.h>
25
26 #include "lldp_debug.h"
27
28 /* Borrowed from Open1X */
29 unsigned char debug_level = 0;
30 int isdaemon = 0;
31 int syslogging = 0;
32 FILE *logfile = NULL;
33
34 /* Borrowed from Open1X */
35 static inline char to_hex_char(int val)
36 {
37   return("0123456789abcdef"[val & 0xf]);
38 }
39
40 /* Borrowed from Open1X */
41 /**
42  *
43  * Dump hex values, without the ascii versions.
44  *
45  */
46 void debug_hex_printf(unsigned char level, uint8_t *hextodump, int size)
47 {
48   int i;
49   int len = 0;
50   char logstr[(size * 3) + 2];
51
52   if ((!(debug_level & level)) && (level != 0))
53     return;
54
55   if (hextodump == NULL)
56     return;
57
58   for (i = 0; i < size; i++)
59     {
60       logstr[len++] = to_hex_char(hextodump[i] >> 4);
61       logstr[len++] = to_hex_char(hextodump[i]);
62       logstr[len++] = ' ';
63     }
64
65   logstr[len++] = '\n';
66   logstr[len] = 0;
67   ufprintf(logfile, logstr, level);
68 }
69
70 /**
71  *
72  * Depending on the value of fh, we will either print to the screen, or
73  * a log file.
74  *
75  */
76 void ufprintf(FILE *fh, char *instr, int level)
77 {
78
79   // If xsup_debug.c is used outside the main Xsupplicant source, it will
80   // get compiler errors because of the lack of xsup_ipc.*.  So, we should
81   // define EXTERNAL_USE so that we don't attempt to build that part.
82   /*#ifndef EXTERNAL_USE
83   // No matter what, we want to send it to connected gui interfaces.
84   if (level == DEBUG_NORMAL)
85     {
86       xsup_ipc_send_log(level, instr);
87     }
88     #endif*/
89
90   // No decide where else to log to.
91   if (((isdaemon == 2) || (fh == NULL)) && (syslogging != 1))
92     {
93       printf("%s", instr);
94       fflush(stdout);
95     } else if (syslogging ==1) {
96       // XXX Consider ways of using other log levels.
97       syslog(LOG_ALERT, "%s", instr);
98     } else {
99       fprintf(fh, "%s", instr);
100       fflush(fh);
101     }
102 }
103
104 /**
105  *
106  * dump some hex values -- also
107  * show the ascii version of the dump.
108  *
109  */
110 void debug_hex_dump(unsigned char level, uint8_t *hextodump, int size)
111 {
112   int i;
113   char buf[80];
114   int str_idx = 0;
115   int chr_idx = 0;
116   int count;
117   int total;
118   int tmp;
119
120   if ((!(debug_level & level)) && (level != 0))
121     return;
122
123   if (hextodump == NULL)
124     return;
125
126   /* Initialize constant fields */
127   memset(buf, ' ', sizeof(buf));
128   buf[4]  = '|';
129   buf[54] = '|';
130   buf[72] = '\n';
131   buf[73] = 0;
132
133   count = 0;
134   total = 0;
135   for (i = 0; i < size; i++)
136     {
137       if (count == 0)
138         {
139           str_idx = 6;
140           chr_idx = 56;
141
142           buf[0] = to_hex_char(total >> 8);
143           buf[1] = to_hex_char(total >> 4);
144           buf[2] = to_hex_char(total);
145         }
146
147       /* store the number */
148       tmp = hextodump[i];
149       buf[str_idx++] = to_hex_char(tmp >> 4);
150       buf[str_idx++] = to_hex_char(tmp);
151       str_idx++;
152
153       /* store the character */
154       buf[chr_idx++] = isprint(tmp) ? tmp : '.';
155
156       total++;
157       count++;
158       if (count >= 16)
159         {
160           count = 0;
161           ufprintf(logfile, buf, level);
162         }
163     }
164
165   /* Print partial line if any */
166   if (count != 0)
167     {
168       /* Clear out any junk */
169       while (count < 16)
170         {
171           buf[str_idx]   = ' ';   /* MSB hex */
172           buf[str_idx+1] = ' ';   /* LSB hex */
173           str_idx += 3;
174
175           buf[chr_idx++] = ' ';
176
177           count++;
178         }
179       ufprintf(logfile, buf, level);
180     }
181 }
182
183 /**
184  *
185  * Display some information.  But only if we are at a debug level that
186  * should display it.
187  *
188  */
189 void debug_printf(unsigned char level, char *fmt, ...)
190 {
191   char dumpstr[2048], temp[2048];
192
193   if (((level & debug_level) || (level == 0)) && (fmt != NULL))
194     {
195       va_list ap;
196       va_start(ap, fmt);
197
198       memset((char *)&dumpstr, 0x00, 2048);
199       memset((char *)&temp, 0x00, 2048);
200
201       // Print out a tag that identifies the type of debug message being used.
202       switch (level)
203         {
204         case DEBUG_NORMAL:
205           break;
206
207         case DEBUG_CONFIG:
208           strcpy((char *)&dumpstr, "[CONFIG] ");
209           break;
210
211         case DEBUG_STATE:
212           strcpy((char *)&dumpstr, "[STATE] ");
213           break;
214
215         case DEBUG_TLV:
216           strcpy((char *)&dumpstr, "[TLV] ");
217           break;
218
219         case DEBUG_INT:
220           strcpy((char *)&dumpstr, "[INT] ");
221           break;
222
223         case DEBUG_EVERYTHING:
224           strcpy((char *)&dumpstr, "[ALL] ");
225           break;
226
227         case DEBUG_EXCESSIVE:
228           strcpy((char *)&dumpstr, "[EXCESSIVE] ");
229           break;
230         }
231
232       vsnprintf((char *)&temp, 2048, fmt, ap);
233
234       strcat((char *)&dumpstr, (char *)&temp);
235
236       ufprintf(logfile, dumpstr, level);
237
238       va_end(ap);
239     }
240 }
241
242
243 /**
244  *
245  * Set flags based on the numeric value that was passed in.
246  *
247  */
248 void debug_set_flags(int new_flags)
249 {
250   if (new_flags >= 1) debug_level |= DEBUG_CONFIG;
251   if (new_flags >= 2) debug_level |= DEBUG_STATE;
252   if (new_flags >= 3) debug_level |= DEBUG_TLV;
253   if (new_flags >= 4) debug_level |= DEBUG_INT;
254   if (new_flags >= 5) debug_level |= DEBUG_SNMP;
255   if (new_flags >= 6) debug_level |= DEBUG_EVERYTHING;
256   if (new_flags >= 7) debug_level |= DEBUG_EXCESSIVE;
257 }
258
259 /**
260  *
261  * Set flags based on an ASCII string that was passed in.
262  *
263  */
264 void debug_alpha_set_flags(char *new_flags)
265 {
266   int i;
267
268   debug_level = 0;
269
270   for (i=0;i<strlen(new_flags);i++)
271     {
272       switch (new_flags[i])
273         {
274         case 'c':
275           debug_level |= DEBUG_CONFIG;
276           break;
277
278         case 's':
279           debug_level |= DEBUG_STATE;
280           break;
281
282         case 't':
283           debug_level |= DEBUG_TLV;
284           break;
285
286         case 'i':
287           debug_level |= DEBUG_INT;
288           break;
289
290         case 'n':
291           debug_level |= DEBUG_SNMP;
292           break;
293
294         case 'e':
295           debug_level |= DEBUG_EVERYTHING;
296           break;
297
298         case 'x':
299           debug_level |= DEBUG_EXCESSIVE;
300           break;
301
302         case 'A':
303           debug_level |= 0xff;   // Set all flags.
304           break;
305         }
306     }
307 }
Note: See TracBrowser for help on using the browser.