root/branches/libfreebob-2.0/src/debugmodule/debugmodule.cpp

Revision 228, 7.2 kB (checked in by pieterpalmers, 17 years ago)

- generic rate control added for ringbuffer based ports.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 /* debugmodule.cpp
2  * Copyright (C) 2005 by Daniel Wagner
3  *
4  * This file is part of FreeBob.
5  *
6  * FreeBob is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * FreeBob is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with FreeBob; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
18  * MA 02111-1307 USA.
19  */
20
21 #include "debugmodule.h"
22
23 #include <stdarg.h>
24 #include <netinet/in.h>
25
26 #include <iostream>
27
28 using namespace std;
29
30 struct ColorEntry  {
31     const char* preSequence;
32     const char* postSequence;
33 };
34
35 ColorEntry colorTable[] = {
36     { "\033[31mFatal",   "\033[0m" },
37     { "\033[31mError",   "\033[0m" },
38     { "\033[31mWarning", "\033[0m" },
39     { "Debug",           ""        },
40 };
41
42
43 DebugModule::DebugModule( std::string name,  debug_level_t level )
44     : m_name( name )
45     , m_level( level )
46 {
47     if ( !DebugModuleManager::instance()->registerModule( *this ) ) {
48         cerr << "Could not register DebugModule (" << name
49              << ") at DebugModuleManager"
50              << endl;
51     }
52 }
53
54 DebugModule::~DebugModule()
55 {
56     if ( !DebugModuleManager::instance()->unregisterModule( *this ) ) {
57         cerr << "Could not unregister DebugModule at DebugModuleManager"
58              << endl;
59     }
60 }
61
62 void
63 DebugModule::printShort( debug_level_t level,
64                          const char* format,
65                          ... ) const
66 {
67     if ( level > m_level ) {
68         return;
69     }
70
71     va_list arg;
72
73     va_start( arg, format );
74
75     if ( vprintf( format, arg ) < 0 ) {
76         cerr << "Could not create debug string with printf" << endl;
77         return;
78     }
79    
80     va_end( arg );
81 }
82
83 void
84 DebugModule::print( debug_level_t level,
85                     const char*   file,
86                     const char*   function,
87                     unsigned int  line,
88                     const char*   format,
89                     ... ) const
90 {
91     if ( level > m_level ) {
92         return;
93     }
94
95     va_list arg;
96     va_start( arg, format );
97
98     if ( printf( "%s (%s)[%d] %s: ", getPreSequence( level ),
99                  file,  line,  function ) < 0 ) {
100         cerr << "Could not create debug string with printf" << endl;
101         return;
102     }
103     if ( vprintf( format, arg ) < 0 ) {
104         cerr << "Could not create debug string with printf" << endl;
105         return;
106     }
107     if ( printf( "%s", getPostSequence( level ) ) < 0 ) {
108         cerr << "Could not create debug string with printf" << endl;
109         return;
110     }
111     va_end( arg );}
112
113 const char*
114 DebugModule::getPreSequence( debug_level_t level ) const
115 {
116     if ( ( level <= eDL_Normal ) && ( level >= eDL_Fatal ) ) {
117         return colorTable[level].preSequence;
118     }
119     return colorTable[eDL_Normal].preSequence;
120 }
121
122 const char*
123 DebugModule::getPostSequence( debug_level_t level ) const
124 {
125     if ( ( level <= eDL_Normal ) && ( level >= eDL_Fatal ) ) {
126         return colorTable[level].postSequence;
127     }
128     return colorTable[eDL_Normal].postSequence;
129 }
130
131 //--------------------------------------
132
133 DebugModuleManager* DebugModuleManager::m_instance = 0;
134
135 DebugModuleManager::DebugModuleManager()
136 {
137 }
138
139 DebugModuleManager::~DebugModuleManager()
140 {
141 }
142
143 DebugModuleManager*
144 DebugModuleManager::instance()
145 {
146     if ( !m_instance ) {
147         m_instance = new DebugModuleManager;
148         if ( !m_instance ) {
149             cerr << "DebugModuleManager::instance Failed to create "
150                  << "DebugModuleManager" << endl;
151         }
152     }
153     return m_instance;
154 }
155
156 bool
157 DebugModuleManager::registerModule( DebugModule& debugModule )
158 {
159     m_debugModules.push_back( &debugModule );
160     return true;
161 }
162
163 bool
164 DebugModuleManager::unregisterModule( DebugModule& debugModule )
165 {
166     for ( DebugModuleVectorIterator it = m_debugModules.begin();
167           it != m_debugModules.end();
168           ++it )
169     {
170         if ( *it == &debugModule ) {
171             m_debugModules.erase( it );
172             return true;
173         }
174     }
175
176     cerr << "DebugModuleManager::unregisterModule: Could not unregister "
177          << "DebugModule (" << debugModule.getName() << ")" << endl;
178     return false;
179 }
180
181 bool
182 DebugModuleManager::setMgrDebugLevel( std::string name, debug_level_t level )
183 {
184     for ( DebugModuleVectorIterator it = m_debugModules.begin();
185           it != m_debugModules.end();
186           ++it )
187     {
188         if ( (*it)->getName() == name ) {
189             return (*it)->setLevel( level );
190         }
191     }
192
193     cerr << "setDebugLevel: Did not find DebugModule ("
194          << name << ")" << endl;
195     return false;
196 }
197
198 //----------------------------------------
199
200 unsigned char
201 toAscii( unsigned char c )
202 {
203     if ( ( c > 31 ) && ( c < 126) ) {
204         return c;
205     } else {
206         return '.';
207     }
208 }
209
210 /* converts a quadlet to a uchar * buffer
211  * not implemented optimally, but clear
212  */
213 void
214 quadlet2char( quadlet_t quadlet, unsigned char* buff )
215 {
216     *(buff)   = (quadlet>>24)&0xFF;
217     *(buff+1) = (quadlet>>16)&0xFF;
218     *(buff+2) = (quadlet>> 8)&0xFF;
219     *(buff+3) = (quadlet)    &0xFF;
220 }
221
222 void
223 hexDump( unsigned char *data_start, unsigned int length )
224 {
225     unsigned int i=0;
226     unsigned int byte_pos;
227     unsigned int bytes_left;
228
229     if ( length <= 0 ) {
230         return;
231     }
232     if ( length >= 7 ) {
233         for ( i = 0; i < (length-7); i += 8 ) {
234             printf( "%04X: %02X %02X %02X %02X %02X %02X %02X %02X "
235                     "- [%c%c%c%c%c%c%c%c]\n",
236
237                     i,
238
239                     *(data_start+i+0),
240                     *(data_start+i+1),
241                     *(data_start+i+2),
242                     *(data_start+i+3),
243                     *(data_start+i+4),
244                     *(data_start+i+5),
245                     *(data_start+i+6),
246                     *(data_start+i+7),
247
248                     toAscii( *(data_start+i+0) ),
249                     toAscii( *(data_start+i+1) ),
250                     toAscii( *(data_start+i+2) ),
251                     toAscii( *(data_start+i+3) ),
252                     toAscii( *(data_start+i+4) ),
253                     toAscii( *(data_start+i+5) ),
254                     toAscii( *(data_start+i+6) ),
255                     toAscii( *(data_start+i+7) )
256                 );
257         }
258     }
259     byte_pos = i;
260     bytes_left = length - byte_pos;
261
262     printf( "%04X:" ,i );
263     for ( i = byte_pos; i < length; i += 1 ) {
264         printf( " %02X", *(data_start+i) );
265     }
266     for ( i=0; i < 8-bytes_left; i+=1 ) {
267         printf( "   " );
268     }
269
270     printf( " - [" );
271     for ( i = byte_pos; i < length; i += 1) {
272         printf( "%c", toAscii(*(data_start+i)));
273     }
274     for ( i = 0; i < 8-bytes_left; i += 1) {
275         printf( " " );
276     }
277
278     printf( "]" );
279     printf( "\n" );
280 }
281
282 void
283 hexDumpQuadlets( quadlet_t *data, unsigned int length )
284 {
285     unsigned int i=0;
286
287     if ( length <= 0 ) {
288         return;
289     }
290     for (i = 0; i< length; i += 1) {
291         printf( "%02d %04X: %08X (%08X)"
292                 "\n", i, i*4, data[i],ntohl(data[i]));
293     }
294 }
295
296
Note: See TracBrowser for help on using the browser.