1 |
/* debugmodule.h |
---|
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 |
#ifndef DEBUGMODULE_H |
---|
22 |
#define DEBUGMODULE_H |
---|
23 |
|
---|
24 |
#include "../fbtypes.h" |
---|
25 |
|
---|
26 |
|
---|
27 |
#include <vector> |
---|
28 |
#include <iostream> |
---|
29 |
|
---|
30 |
typedef short debug_level_t; |
---|
31 |
|
---|
32 |
/* MB_NEXT() relies on the fact that MB_BUFFERS is a power of two */ |
---|
33 |
#define MB_BUFFERS 4096 |
---|
34 |
#define MB_NEXT(index) ((index+1) & (MB_BUFFERS-1)) |
---|
35 |
#define MB_BUFFERSIZE 256 /* message length limit */ |
---|
36 |
|
---|
37 |
#define debugFatal( format, args... ) \ |
---|
38 |
m_debugModule.print( DebugModule::eDL_Fatal, \ |
---|
39 |
__FILE__, \ |
---|
40 |
__FUNCTION__, \ |
---|
41 |
__LINE__, \ |
---|
42 |
format, \ |
---|
43 |
##args ) |
---|
44 |
#define debugError( format, args... ) \ |
---|
45 |
m_debugModule.print( DebugModule::eDL_Error, \ |
---|
46 |
__FILE__, \ |
---|
47 |
__FUNCTION__, \ |
---|
48 |
__LINE__, \ |
---|
49 |
format, \ |
---|
50 |
##args ) |
---|
51 |
#define debugWarning( format, args... ) \ |
---|
52 |
m_debugModule.print( DebugModule::eDL_Warning, \ |
---|
53 |
__FILE__, \ |
---|
54 |
__FUNCTION__, \ |
---|
55 |
__LINE__, \ |
---|
56 |
format, \ |
---|
57 |
##args ) |
---|
58 |
|
---|
59 |
#define debugFatalShort( format, args... ) \ |
---|
60 |
m_debugModule.printShort( DebugModule::eDL_Fatal, \ |
---|
61 |
format, \ |
---|
62 |
##args ) |
---|
63 |
#define debugErrorShort( format, args... ) \ |
---|
64 |
m_debugModule.printShort( DebugModule::eDL_Error, \ |
---|
65 |
format, \ |
---|
66 |
##args ) |
---|
67 |
#define debugWarningShort( format, args... ) \ |
---|
68 |
m_debugModule.printShort( DebugModule::eDL_Warning, \ |
---|
69 |
format, \ |
---|
70 |
##args ) |
---|
71 |
|
---|
72 |
#define DECLARE_DEBUG_MODULE static DebugModule m_debugModule |
---|
73 |
#define IMPL_DEBUG_MODULE( ClassName, RegisterName, Level ) \ |
---|
74 |
DebugModule ClassName::m_debugModule = \ |
---|
75 |
DebugModule( #RegisterName, Level ) |
---|
76 |
|
---|
77 |
#define DECLARE_GLOBAL_DEBUG_MODULE extern DebugModule m_debugModule |
---|
78 |
#define IMPL_GLOBAL_DEBUG_MODULE( RegisterName, Level ) \ |
---|
79 |
DebugModule m_debugModule = \ |
---|
80 |
DebugModule( #RegisterName, Level ) |
---|
81 |
|
---|
82 |
#define setDebugLevel( Level ) { \ |
---|
83 |
m_debugModule.setLevel( Level ); \ |
---|
84 |
} |
---|
85 |
|
---|
86 |
/* m_debugModule.print( eDL_Normal, \ |
---|
87 |
__FILE__, \ |
---|
88 |
__FUNCTION__, \ |
---|
89 |
__LINE__, \ |
---|
90 |
"Setting debug level to %d\n", \ |
---|
91 |
Level ); \ |
---|
92 |
}*/ |
---|
93 |
|
---|
94 |
#define getDebugLevel( ) \ |
---|
95 |
m_debugModule.getLevel( ) |
---|
96 |
|
---|
97 |
|
---|
98 |
#ifdef DEBUG |
---|
99 |
|
---|
100 |
#define debugOutput( level, format, args... ) \ |
---|
101 |
m_debugModule.print( level, \ |
---|
102 |
__FILE__, \ |
---|
103 |
__FUNCTION__, \ |
---|
104 |
__LINE__, \ |
---|
105 |
format, \ |
---|
106 |
##args ) |
---|
107 |
|
---|
108 |
#define debugOutputShort( level, format, args... ) \ |
---|
109 |
m_debugModule.printShort( level, \ |
---|
110 |
format, \ |
---|
111 |
##args ) |
---|
112 |
|
---|
113 |
#else |
---|
114 |
|
---|
115 |
#define debugOutput( level, format, args... ) |
---|
116 |
#define debugOutputShort( level, format, args... ) |
---|
117 |
|
---|
118 |
#endif |
---|
119 |
|
---|
120 |
/* Enable preemption checking for Linux Realtime Preemption kernels. |
---|
121 |
* |
---|
122 |
* This checks if any RT-safe code section does anything to cause CPU |
---|
123 |
* preemption. Examples are sleep() or other system calls that block. |
---|
124 |
* If a problem is detected, the kernel writes a syslog entry, and |
---|
125 |
* sends SIGUSR2 to the client. |
---|
126 |
*/ |
---|
127 |
|
---|
128 |
// #define DO_PREEMPTION_CHECKING |
---|
129 |
|
---|
130 |
#include <sys/time.h> |
---|
131 |
|
---|
132 |
#ifdef DO_PREEMPTION_CHECKING |
---|
133 |
#define CHECK_PREEMPTION(onoff) \ |
---|
134 |
gettimeofday((struct timeval *)1, (struct timezone *)onoff) |
---|
135 |
#else |
---|
136 |
#define CHECK_PREEMPTION(onoff) |
---|
137 |
#endif |
---|
138 |
|
---|
139 |
unsigned char toAscii( unsigned char c ); |
---|
140 |
void quadlet2char( fb_quadlet_t quadlet, unsigned char* buff ); |
---|
141 |
void hexDump( unsigned char *data_start, unsigned int length ); |
---|
142 |
void hexDumpQuadlets( quadlet_t *data_start, unsigned int length ); |
---|
143 |
|
---|
144 |
class DebugModule { |
---|
145 |
public: |
---|
146 |
enum { |
---|
147 |
eDL_Fatal = 0, |
---|
148 |
eDL_Error = 1, |
---|
149 |
eDL_Warning = 2, |
---|
150 |
eDL_Normal = 3, |
---|
151 |
eDL_Info = 4, |
---|
152 |
eDL_Verbose = 5, |
---|
153 |
eDL_VeryVerbose = 6, |
---|
154 |
} EDebugLevel; |
---|
155 |
|
---|
156 |
DebugModule( std::string name, debug_level_t level ); |
---|
157 |
virtual ~DebugModule(); |
---|
158 |
|
---|
159 |
void printShort( debug_level_t level, |
---|
160 |
const char* format, |
---|
161 |
... ) const; |
---|
162 |
|
---|
163 |
void print( debug_level_t level, |
---|
164 |
const char* file, |
---|
165 |
const char* function, |
---|
166 |
unsigned int line, |
---|
167 |
const char* format, |
---|
168 |
... ) const; |
---|
169 |
|
---|
170 |
bool setLevel( debug_level_t level ) |
---|
171 |
{ m_level = level; return true; } |
---|
172 |
debug_level_t getLevel() |
---|
173 |
{ return m_level; } |
---|
174 |
std::string getName() |
---|
175 |
{ return m_name; } |
---|
176 |
|
---|
177 |
protected: |
---|
178 |
const char* getPreSequence( debug_level_t level ) const; |
---|
179 |
const char* getPostSequence( debug_level_t level ) const; |
---|
180 |
|
---|
181 |
private: |
---|
182 |
std::string m_name; |
---|
183 |
debug_level_t m_level; |
---|
184 |
}; |
---|
185 |
|
---|
186 |
#define DEBUG_LEVEL_NORMAL DebugModule::eDL_Normal |
---|
187 |
#define DEBUG_LEVEL_INFO DebugModule::eDL_Info |
---|
188 |
#define DEBUG_LEVEL_VERBOSE DebugModule::eDL_Verbose |
---|
189 |
#define DEBUG_LEVEL_VERY_VERBOSE DebugModule::eDL_VeryVerbose |
---|
190 |
|
---|
191 |
|
---|
192 |
class DebugModuleManager { |
---|
193 |
public: |
---|
194 |
friend class DebugModule; |
---|
195 |
|
---|
196 |
static DebugModuleManager* instance(); |
---|
197 |
~DebugModuleManager(); |
---|
198 |
|
---|
199 |
bool setMgrDebugLevel( std::string name, debug_level_t level ); |
---|
200 |
|
---|
201 |
protected: |
---|
202 |
bool registerModule( DebugModule& debugModule ); |
---|
203 |
bool unregisterModule( DebugModule& debugModule ); |
---|
204 |
|
---|
205 |
bool init(); |
---|
206 |
|
---|
207 |
void print(const char *fmt, ...); |
---|
208 |
void va_print(const char *fmt, va_list); |
---|
209 |
|
---|
210 |
private: |
---|
211 |
DebugModuleManager(); |
---|
212 |
|
---|
213 |
typedef std::vector< DebugModule* > DebugModuleVector; |
---|
214 |
typedef std::vector< DebugModule* >::iterator DebugModuleVectorIterator; |
---|
215 |
|
---|
216 |
char mb_buffers[MB_BUFFERS][MB_BUFFERSIZE]; |
---|
217 |
unsigned int mb_initialized; |
---|
218 |
unsigned int mb_inbuffer; |
---|
219 |
unsigned int mb_outbuffer; |
---|
220 |
unsigned int mb_overruns; |
---|
221 |
pthread_t mb_writer_thread; |
---|
222 |
pthread_mutex_t mb_write_lock; |
---|
223 |
pthread_cond_t mb_ready_cond; |
---|
224 |
|
---|
225 |
static void *mb_thread_func(void *arg); |
---|
226 |
void mb_flush(); |
---|
227 |
|
---|
228 |
static DebugModuleManager* m_instance; |
---|
229 |
DebugModuleVector m_debugModules; |
---|
230 |
}; |
---|
231 |
|
---|
232 |
#endif |
---|