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

Revision 244, 7.6 kB (checked in by pieterpalmers, 18 years ago)

- first try to implement the SYT synchronisation.

Not working yet.

- committing to spend some time to get the Motu people going.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
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                 m_debugModule.print( Level,                        \
85                                      __FILE__,                     \
86                                      __FUNCTION__,                 \
87                                      __LINE__,                     \
88                                      "Setting debug level to %d\n",  \
89                                      Level ); \
90                                 }
91 #define getDebugLevel( Level )                                     \
92                 m_debugModule.getLevel( )
93
94
95 #ifdef DEBUG
96
97     #define debugOutput( level, format, args... )                  \
98                 m_debugModule.print( level,                        \
99                                      __FILE__,                     \
100                                      __FUNCTION__,                 \
101                                      __LINE__,                     \
102                                      format,                       \
103                                      ##args )
104
105     #define debugOutputShort( level, format, args... )             \
106                 m_debugModule.printShort( level,                   \
107                                      format,                       \
108                                      ##args )
109
110 #else
111
112     #define debugOutput( level, format, args... )
113     #define debugOutputShort( level, format, args... )
114
115 #endif
116
117 unsigned char toAscii( unsigned char c );
118 void quadlet2char( fb_quadlet_t quadlet, unsigned char* buff );
119 void hexDump( unsigned char *data_start, unsigned int length );
120 void hexDumpQuadlets( quadlet_t *data_start, unsigned int length );
121
122 class DebugModule {
123 public:
124     enum {
125         eDL_Fatal       = 0,
126         eDL_Error       = 1,
127         eDL_Warning     = 2,
128         eDL_Normal      = 3,
129         eDL_Verbose     = 4,
130         eDL_VeryVerbose = 5,
131     } EDebugLevel;
132
133     DebugModule( std::string name, debug_level_t level );
134     virtual ~DebugModule();
135
136     void printShort( debug_level_t level,
137                      const char* format,
138                      ... ) const;
139
140     void print( debug_level_t level,
141                 const char*   file,
142                 const char*   function,
143                 unsigned int  line,
144                 const char*   format,
145                 ... ) const;
146
147     bool setLevel( debug_level_t level )
148         { m_level = level; return true; }
149     debug_level_t getLevel()
150         { return m_level; }
151     std::string getName()
152         { return m_name; }
153
154 protected:
155     const char* getPreSequence( debug_level_t level ) const;
156     const char* getPostSequence( debug_level_t level ) const;
157
158 private:
159     std::string   m_name;
160     debug_level_t m_level;
161 };
162
163 #define DEBUG_LEVEL_NORMAL          DebugModule::eDL_Normal
164 #define DEBUG_LEVEL_VERBOSE         DebugModule::eDL_Verbose
165 #define DEBUG_LEVEL_VERY_VERBOSE    DebugModule::eDL_VeryVerbose
166
167
168 class DebugModuleManager {
169 public:
170     friend class DebugModule;
171
172     static DebugModuleManager* instance();
173     ~DebugModuleManager();
174    
175     bool setMgrDebugLevel( std::string name, debug_level_t level );
176
177 protected:
178     bool registerModule( DebugModule& debugModule );
179     bool unregisterModule( DebugModule& debugModule );
180
181     bool init();
182    
183     void print(const char *fmt, ...);
184     void va_print(const char *fmt, va_list);
185    
186 private:
187     DebugModuleManager();
188
189     typedef std::vector< DebugModule* > DebugModuleVector;
190     typedef std::vector< DebugModule* >::iterator DebugModuleVectorIterator;
191
192     char mb_buffers[MB_BUFFERS][MB_BUFFERSIZE];
193     unsigned int mb_initialized;
194     unsigned int mb_inbuffer;
195     unsigned int mb_outbuffer;
196     unsigned int mb_overruns;
197     pthread_t mb_writer_thread;
198     pthread_mutex_t mb_write_lock;
199     pthread_cond_t mb_ready_cond;
200
201     static void *mb_thread_func(void *arg);
202     void mb_flush();
203    
204     static DebugModuleManager* m_instance;
205     DebugModuleVector          m_debugModules;
206 };
207
208 #endif
Note: See TracBrowser for help on using the browser.