TRC Library for FRC (2011) 2011
TRC Library Code Project

C:/Users/Michael/Frc/2011/code/trclib/DbgTrace.h

Go to the documentation of this file.
00001 #if 0
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 #endif
00014 
00015 #ifndef _DBGTRACE_H
00016 #define _DBGTRACE_H
00017 
00018 //
00019 // Module ID.
00020 //
00021 #define MOD_LIB                 0xffffff00
00022 #define MOD_TRCROBOT            0x00000100
00023 #define MOD_JOYSTICK            0x00000200
00024 #define MOD_DIGITALIN           0x00000400
00025 #define MOD_ANALOGIN            0x00000800
00026 #define MOD_GYRO                0x00001000
00027 #define MOD_ACCEL               0x00002000
00028 #define MOD_PIDCTRL             0x00004000
00029 #define MOD_PIDDRIVE            0x00008000
00030 #define MOD_SM                  0x00010000
00031 #define MOD_TIMER               0x00020000
00032 #define MOD_EVENT               0x00040000
00033 
00034 #define MOD_MAIN                0x00000001
00035 #define TGenModId(n)            ((MOD_MAIN << (n)) & 0xff)
00036 
00037 #define INIT                    0
00038 #define API                     1
00039 #define CALLBK                  2
00040 #define EVENT                   3
00041 #define FUNC                    4
00042 #define TASK                    5
00043 #define UTIL                    6
00044 #define HIFREQ                  7
00045 
00046 #define FATAL                   0
00047 #define ERR                     1
00048 #define WARN                    2
00049 #define INFO                    3
00050 #define VERBOSE                 4
00051 
00052 #ifndef TRACE_PERIOD
00053   #define TRACE_PERIOD          500     //in msec
00054 #endif
00055 
00056 #ifndef SAMPLING_PERIOD
00057   #define SAMPLING_PERIOD       500     //in msec
00058 #endif
00059 
00060 #define TPrintf                 printf
00061 
00062 //
00063 // Trace macros.
00064 //
00065 #ifdef _DBGTRACE_ENABLED
00066     #define TModEnterMsg(m,p)   if (g_Trace.m_fTraceEnabled && \
00067                                     ((g_Trace.m_traceModules & (m)) != 0) && \
00068                                     (_traceLevel <= g_Trace.m_traceLevel)) \
00069                                 { \
00070                                     g_Trace.FuncPrefix(MOD_NAME, \
00071                                                        __FUNCTION__, \
00072                                                        true, \
00073                                                        false); \
00074                                     TPrintf p; \
00075                                     TPrintf(")\n"); \
00076                                 }
00077     #define TModEnter(m)        if (g_Trace.m_fTraceEnabled && \
00078                                     ((g_Trace.m_traceModules & (m)) != 0) && \
00079                                     (_traceLevel <= g_Trace.m_traceLevel)) \
00080                                 { \
00081                                     g_Trace.FuncPrefix(MOD_NAME, \
00082                                                        __FUNCTION__, \
00083                                                        true, \
00084                                                        true); \
00085                                 }
00086     #define TModExitMsg(m,p)    if (g_Trace.m_fTraceEnabled && \
00087                                     ((g_Trace.m_traceModules & (m)) != 0) && \
00088                                     (_traceLevel <= g_Trace.m_traceLevel)) \
00089                                 { \
00090                                     g_Trace.FuncPrefix(MOD_NAME, \
00091                                                        __FUNCTION__, \
00092                                                        false, \
00093                                                        false); \
00094                                     TPrintf p; \
00095                                     TPrintf("\n"); \
00096                                 }
00097     #define TModExit(m)         if (g_Trace.m_fTraceEnabled && \
00098                                     ((g_Trace.m_traceModules & (m)) != 0) && \
00099                                     (_traceLevel <= g_Trace.m_traceLevel)) \
00100                                 { \
00101                                     g_Trace.FuncPrefix(MOD_NAME, \
00102                                                        __FUNCTION__, \
00103                                                        false, \
00104                                                        true); \
00105                                 }
00106     #define TModMsg(m,e,p)      if (g_Trace.m_fTraceEnabled && \
00107                                     ((g_Trace.m_traceModules & (m)) != 0) && \
00108                                     ((e) <= g_Trace.m_msgLevel)) \
00109                                 { \
00110                                     g_Trace.MsgPrefix(MOD_NAME, \
00111                                                       __FUNCTION__, \
00112                                                       e); \
00113                                     TPrintf p; \
00114                                     TPrintf("\n"); \
00115                                 }
00116     #define TEnable(b)          g_Trace.m_fTraceEnabled = (b)
00117     #define TraceInit(m,l,e)    g_Trace.Initialize(m, l, e)
00118     #define TLevel(l)           UINT32 _traceLevel = l
00119     #define TEnterMsg(p)        TModEnterMsg(MOD_ID, p)
00120     #define TEnter()            TModEnter(MOD_ID)
00121     #define TExitMsg(p)         TModExitMsg(MOD_ID, p)
00122     #define TExit()             TModExit(MOD_ID)
00123     #define TMsg(e,p)           TModMsg(MOD_ID, e, p)
00124     #define TFatal(p)           TModMsg(MOD_ID, FATAL, p)
00125     #define TErr(p)             TModMsg(MOD_ID, ERR, p)
00126     #define TWarn(p)            TModMsg(MOD_ID, WARN, p)
00127     #define TInfo(p)            TModMsg(MOD_ID, INFO, p)
00128     #define TVerbose(p)         TModMsg(MOD_ID, VERBOSE, p)
00129     #define TMsgPeriod(t,p)     { \
00130                                     static UINT32 _usecNextTime = 0; \
00131                                     if (GetFPGATime() >= _usecNextTime) \
00132                                     { \
00133                                         _usecNextTime = GetFPGATime() + \
00134                                                         (UINT32)(t)*1000; \
00135                                         TModMsg(MOD_ID, INFO, p); \
00136                                     } \
00137                                 }
00138     #define TSampling(p)        TMsgPeriod(SAMPLING_PERIOD, p)
00139     #define TAssertPeriod(t,p,r) { \
00140                                     UINT32 _time = GetMsecTime(); \
00141                                     float _period = (float)(_time - (t))/ \
00142                                                     1000.0; \
00143                                     t = _time; \
00144                                     if (fabs(_period - (p)) > (r)) \
00145                                     { \
00146                                         TWarn(("Period variance exceeding " \
00147                                                "tolerance (period=%f)", \
00148                                                _period)); \
00149                                     } \
00150                                 }
00151     #define TPeriodStart()      if (GetFPGATime() >= g_Trace.m_traceTime) \
00152                                 { \
00153                                     g_Trace.m_traceTime = GetFPGATime + \
00154                                                           TRACE_PERIOD; \
00155                                     TEnable(true); \
00156                                 }
00157     #define TPeriodEnd()        TEnable(false)
00158 #else
00159     #define TraceInit(m,l,e)
00160     #define TLevel(l)
00161     #define TEnterMsg(p)
00162     #define TEnter()
00163     #define TExitMsg(p)
00164     #define TExit()
00165     #define TMsg(e,p)
00166     #define TFatal(p)
00167     #define TErr(p)
00168     #define TWarn(p)
00169     #define TInfo(p)
00170     #define TVerbose(p)
00171     #define TMsgPeriod(t,p)
00172     #define TSampling(p)
00173     #define TAssertPeriod(t,p,r)
00174     #define TPeriodStart()
00175     #define TPeriodEnd()
00176 #endif  //ifdef _DBGTRACE_ENABLED
00177 
00186 class DbgTrace
00187 {
00188 public:
00189     bool   m_fTraceEnabled;
00190     UINT32 m_traceModules;
00191     UINT32 m_traceLevel;
00192     UINT32 m_msgLevel;
00193     UINT32 m_traceTime;
00194 
00195 private:
00196     INT32  m_indentLevel;
00197 
00198 public:
00202     DbgTrace(
00203         void
00204         )
00205     {
00206         m_fTraceEnabled = false;
00207         m_traceModules = 0;
00208         m_tracceLevel = 0;
00209         m_msgLevel = 0;
00210         m_traceTime = 0;
00211         m_indentLevel = 0;
00212     }   //DbgTrace
00213 
00217     virtual
00218     ~DbgTrace(
00219         void
00220         )
00221     {
00222     }   //~DbgTrace
00223 
00235     void
00236     Initialize(
00237         __in UINT32 traceModules,
00238         __in UINT32 traceLevel,
00239         __in UINT32 msgLevel
00240         )
00241     {
00242         m_traceModules = traceModules;
00243         m_traceLevel = traceLevel;
00244         m_msgLevel = msgLevel;
00245         m_indentLevel = 0;
00246     }   //Initialize
00247 
00258     void
00259     FuncPrefix(
00260         __in const char *pszMod,
00261         __in const char *pszFunc,
00262         __in bool        fEnter,
00263         __in bool        fNewLine
00264         )
00265     {
00266         if (fEnter)
00267         {
00268             m_levelIndent++;
00269         }
00270 
00271         for (INT32 i = 0; i < m_levelIndent; i++)
00272         {
00273             TPrintf("| ");
00274         }
00275 
00276         TPrintf("%s.%s", pszMod, pszFunc);
00277 
00278         if (fEnter)
00279         {
00280             TPrintf("%s", fNewLine? "()\n": "(");
00281         }
00282         else
00283         {
00284             TPrintf("%s", fNewLine? "!\n": "!");
00285             m_levelIndent--;
00286         }
00287     }   //FuncPrefix
00288 
00298     void
00299     MsgPrefix(
00300         __in const char *pszMod,
00301         __in const char *pszFunc,
00302         __in UINT32      msgLevel
00303         )
00304     {
00305         char *pszPrefix = "_Unk: ";
00306 
00307         switch (msgLevel)
00308         {
00309         case FATAL:
00310             pszPrefix = "_Fatal: ";
00311             break;
00312 
00313         case ERR:
00314             pszPrefix = "_Err: ";
00315             break;
00316 
00317         case WARN:
00318             pszPrefix = "_Warn: ";
00319             break;
00320 
00321         case INFO:
00322             pszPrefix = "_Info: ";
00323             break;
00324 
00325         case VERBOSE:
00326             pszPrefix = "_Verbose: ";
00327             break;
00328         }
00329 
00330         TPrintf("%s.%s%s", pszMod, pszFunc, pszPrefix);
00331     }   //MsgPrefix
00332 };      //class DbgTrace
00333 
00334 #ifdef _DBGTRACE_ENABLED
00335     DbgTrace g_Trace;
00336 #endif
00337 
00338 #endif  //ifndef _DBGTRACE_H
 All Classes Files Functions Variables Defines