root/trunk/libfreebob/src/debugmodule/debugmodule.cpp

Revision 168, 7.5 kB (checked in by wagi, 16 years ago)

2006-03-04 Daniel Wagner <wagi@monom.org>

  • src/avdevice.cpp: Added 'verbose' argument to constructor
    and set debug level to DEBUG_LEVEL_VERBOSE if 'verbose'
    is true, e.g enables verbose mode with '-v' argument passed
    to test-freebob.
  • src/avdevice.h: Likewsise.
  • src/avdevicesubunit.cpp: Likewsise.
  • src/avdevicesubunit.h: Likewsise.
  • src/avplug.cpp: Likewsise.
  • src/avplug.h: Likewsise.
  • src/devicemanager.cpp: Likewsise.
  • src/debugmodule/debugmodule.cpp: Likewsise.
  • src/debugmodule/debugmodule.h: Likewsise.
  • src/debugmodule/test_debugmodule.cpp: Likewsise.
  • 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     if ( printf( "%s: ", getPreSequence( level ) ) < 0 ) {
75         cerr << "Could not create debug string with printf" << endl;
76         return;
77     }
78     if ( vprintf( format, arg ) < 0 ) {
79         cerr << "Could not create debug string with printf" << endl;
80         return;
81     }
82     if ( printf( "%s", getPostSequence( level ) ) < 0 ) {
83         cerr << "Could not create debug string with printf" << endl;
84         return;
85     }
86     va_end( arg );
87 }
88
89 void
90 DebugModule::print( debug_level_t level,
91                     const char*   file,
92                     const char*   function,
93                     unsigned int  line,
94                     const char*   format,
95                     ... ) const
96 {
97     if ( level > m_level ) {
98         return;
99     }
100
101     va_list arg;
102     va_start( arg, format );
103
104     if ( printf( "%s (%s)[%d] %s: ", getPreSequence( level ),
105                  file,  line,  function ) < 0 ) {
106         cerr << "Could not create debug string with printf" << endl;
107         return;
108     }
109     if ( vprintf( format, arg ) < 0 ) {
110         cerr << "Could not create debug string with printf" << endl;
111         return;
112     }
113     if ( printf( "%s", getPostSequence( level ) ) < 0 ) {
114         cerr << "Could not create debug string with printf" << endl;
115         return;
116     }
117     va_end( arg );}
118
119 const char*
120 DebugModule::getPreSequence( debug_level_t level ) const
121 {
122     if ( ( level <= eDL_Normal ) && ( level >= eDL_Fatal ) ) {
123         return colorTable[level].preSequence;
124     }
125     return colorTable[eDL_Normal].preSequence;
126 }
127
128 const char*
129 DebugModule::getPostSequence( debug_level_t level ) const
130 {
131     if ( ( level <= eDL_Normal ) && ( level >= eDL_Fatal ) ) {
132         return colorTable[level].postSequence;
133     }
134     return colorTable[eDL_Normal].postSequence;
135 }
136
137 //--------------------------------------
138
139 DebugModuleManager* DebugModuleManager::m_instance = 0;
140
141 DebugModuleManager::DebugModuleManager()
142 {
143 }
144
145 DebugModuleManager::~DebugModuleManager()
146 {
147 }
148
149 DebugModuleManager*
150 DebugModuleManager::instance()
151 {
152     if ( !m_instance ) {
153         m_instance = new DebugModuleManager;
154         if ( !m_instance ) {
155             cerr << "DebugModuleManager::instance Failed to create "
156                  << "DebugModuleManager" << endl;
157         }
158     }
159     return m_instance;
160 }
161
162 bool
163 DebugModuleManager::registerModule( DebugModule& debugModule )
164 {
165     m_debugModules.push_back( &debugModule );
166     return true;
167 }
168
169 bool
170 DebugModuleManager::unregisterModule( DebugModule& debugModule )
171 {
172     for ( DebugModuleVectorIterator it = m_debugModules.begin();
173           it != m_debugModules.end();
174           ++it )
175     {
176         if ( *it == &debugModule ) {
177             m_debugModules.erase( it );
178             return true;
179         }
180     }
181
182     cerr << "DebugModuleManager::unregisterModule: Could not unregister "
183          << "DebugModule (" << debugModule.getName() << ")" << endl;
184     return false;
185 }
186
187 bool
188 DebugModuleManager::setMgrDebugLevel( std::string name, debug_level_t level )
189 {
190     for ( DebugModuleVectorIterator it = m_debugModules.begin();
191           it != m_debugModules.end();
192           ++it )
193     {
194         if ( (*it)->getName() == name ) {
195             return (*it)->setLevel( level );
196         }
197     }
198
199     cerr << "setDebugLevel: Did not find DebugModule ("
200          << name << ")" << endl;
201     return false;
202 }
203
204 //----------------------------------------
205
206 unsigned char
207 toAscii( unsigned char c )
208 {
209     if ( ( c > 31 ) && ( c < 126) ) {
210         return c;
211     } else {
212         return '.';
213     }
214 }
215
216 /* converts a quadlet to a uchar * buffer
217  * not implemented optimally, but clear
218  */
219 void
220 quadlet2char( quadlet_t quadlet, unsigned char* buff )
221 {
222     *(buff)   = (quadlet>>24)&0xFF;
223     *(buff+1) = (quadlet>>16)&0xFF;
224     *(buff+2) = (quadlet>> 8)&0xFF;
225     *(buff+3) = (quadlet)    &0xFF;
226 }
227
228 void
229 hexDump( unsigned char *data_start, unsigned int length )
230 {
231     unsigned int i=0;
232     unsigned int byte_pos;
233     unsigned int bytes_left;
234
235     if ( length <= 0 ) {
236         return;
237     }
238     if ( length >= 7 ) {
239         for ( i = 0; i < (length-7); i += 8 ) {
240             printf( "%04X: %02X %02X %02X %02X %02X %02X %02X %02X "
241                     "- [%c%c%c%c%c%c%c%c]\n",
242
243                     i,
244
245                     *(data_start+i+0),
246                     *(data_start+i+1),
247                     *(data_start+i+2),
248                     *(data_start+i+3),
249                     *(data_start+i+4),
250                     *(data_start+i+5),
251                     *(data_start+i+6),
252                     *(data_start+i+7),
253
254                     toAscii( *(data_start+i+0) ),
255                     toAscii( *(data_start+i+1) ),
256                     toAscii( *(data_start+i+2) ),
257                     toAscii( *(data_start+i+3) ),
258                     toAscii( *(data_start+i+4) ),
259                     toAscii( *(data_start+i+5) ),
260                     toAscii( *(data_start+i+6) ),
261                     toAscii( *(data_start+i+7) )
262                 );
263         }
264     }
265     byte_pos = i;
266     bytes_left = length - byte_pos;
267
268     printf( "%04X:" ,i );
269     for ( i = byte_pos; i < length; i += 1 ) {
270         printf( " %02X", *(data_start+i) );
271     }
272     for ( i=0; i < 8-bytes_left; i+=1 ) {
273         printf( "   " );
274     }
275
276     printf( " - [" );
277     for ( i = byte_pos; i < length; i += 1) {
278         printf( "%c", toAscii(*(data_start+i)));
279     }
280     for ( i = 0; i < 8-bytes_left; i += 1) {
281         printf( " " );
282     }
283
284     printf( "]" );
285     printf( "\n" );
286 }
287
288 void
289 hexDumpQuadlets( quadlet_t *data, unsigned int length )
290 {
291     unsigned int i=0;
292
293     if ( length <= 0 ) {
294         return;
295     }
296     for (i = 0; i< length; i += 1) {
297         printf( "%02d %04X: %08X (%08X)"
298                 "\n", i, i*4, data[i],ntohl(data[i]));
299     }
300 }
301
302
Note: See TracBrowser for help on using the browser.