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

C:/Users/Michael/Ftc/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 #pragma systemFile
00019 
00020 //
00021 // Module ID.
00022 //
00023 #define MOD_LIB                 0xffffff00
00024 #define MOD_BATT                0x00000100
00025 #define MOD_MENU                0x00000200
00026 #define MOD_NXTBTN              0x00000400
00027 #define MOD_JOYBTN              0x00000800
00028 #define MOD_SENSOR              0x00001000
00029 #define MOD_TIMER               0x00002000
00030 #define MOD_SM                  0x00004000
00031 #define MOD_PIDCTRL             0x00008000
00032 #define MOD_DRIVE               0x00010000
00033 #define MOD_PIDDRIVE            0x00020000
00034 #define MOD_LNFOLLOW            0x00040000
00035 #define MOD_SERVO               0x00080000
00036 #define MOD_ACCEL               0x00100000
00037 #define MOD_GYRO                0x00200000
00038 #define MOD_TOUCH               0x00400000
00039 
00040 #define MOD_MAIN                0x00000001
00041 #define TGenModId(n)            ((MOD_MAIN << (n)) && 0xff)
00042 
00043 #define INIT                    0
00044 #define API                     1
00045 #define CALLBK                  2
00046 #define EVENT                   3
00047 #define FUNC                    4
00048 #define TASK                    5
00049 #define UTIL                    6
00050 #define HIFREQ                  7
00051 
00052 #define FATAL                   0
00053 #define ERR                     1
00054 #define WARN                    2
00055 #define INFO                    3
00056 #define VERBOSE                 4
00057 
00058 #ifndef TRACE_PERIOD
00059     #define TRACE_PERIOD        500     //in msec
00060 #endif
00061 
00062 #ifndef SAMPLING_PERIOD
00063     #define SAMPLING_PERIOD     500     //in msec
00064 #endif
00065 
00066 #define TPrintf                 writeDebugStream
00067 #define TPrintfLine             writeDebugStreamLine
00068 
00069 //
00070 // Trace macros.
00071 //
00072 #ifdef _DEBUG_TRACE
00073     #define TModEnterMsg(m,p)   if (g_TraceEnabled && \
00074                                     ((g_TraceModules & (m)) != 0) && \
00075                                     (_levelTrace <= g_TraceLevel)) \
00076                                 { \
00077                                     TracePrefix(_strFuncName, true, false); \
00078                                     TPrintf p; \
00079                                     TPrintf(")\n"); \
00080                                 }
00081     #define TModEnter(m)        if (g_TraceEnabled && \
00082                                     ((g_TraceModules & (m)) != 0) && \
00083                                     (_levelTrace <= g_TraceLevel)) \
00084                                 { \
00085                                     TracePrefix(_strFuncName, true, true); \
00086                                 }
00087     #define TModExitMsg(m,p)    if (g_TraceEnabled && \
00088                                     ((g_TraceModules & (m)) != 0) && \
00089                                     (_levelTrace <= g_TraceLevel)) \
00090                                 { \
00091                                     TracePrefix(_strFuncName, false, false); \
00092                                     TPrintfLine p; \
00093                                 }
00094     #define TModExit(m)         if (g_TraceEnabled && \
00095                                     ((g_TraceModules & (m)) != 0) && \
00096                                     (_levelTrace <= g_TraceLevel)) \
00097                                 { \
00098                                     TracePrefix(_strFuncName, false, true); \
00099                                 }
00100     #define TModMsg(m,e,p)      if (g_TraceEnabled && \
00101                                     ((g_TraceModules & (m)) != 0) && \
00102                                     ((e) <= g_MsgLevel)) \
00103                                 { \
00104                                     MsgPrefix(_strFuncName, e); \
00105                                     TPrintfLine p; \
00106                                 }
00107     #define TraceInit(m,l,e)    { \
00108                                     g_TraceModules = (m); \
00109                                     g_TraceLevel = (l); \
00110                                     g_MsgLevel = (e); \
00111                                     g_TraceEnabled = false; \
00112                                     g_TraceTime = nPgmTime; \
00113                                 }
00114     #define TEnable(b)          g_TraceEnabled = b
00115     #define TFuncName(s)        string _strFuncName = s
00116     #define TLevel(l)           int _levelTrace = l
00117     #define TEnterMsg(p)        TModEnterMsg(MOD_ID, p)
00118     #define TEnter()            TModEnter(MOD_ID)
00119     #define TExitMsg(p)         TModExitMsg(MOD_ID, p)
00120     #define TExit()             TModExit(MOD_ID)
00121     #define TMsg(e,p)           TModMsg(MOD_ID, e, p)
00122     #define TFatal(p)           TModMsg(MOD_ID, FATAL, p)
00123     #define TErr(p)             TModMsg(MOD_ID, ERR, p)
00124     #define TWarn(p)            TModMsg(MOD_ID, WARN, p)
00125     #define TInfo(p)            TModMsg(MOD_ID, INFO, p)
00126     #define TVerbose(p)         TModMsg(MOD_ID, VERBOSE, p)
00127     #define TMsgPeriod(t,p)     { \
00128                                     static long _nextTime = nPgmTime; \
00129                                     if (nPgmTime >= _nextTime) \
00130                                     { \
00131                                         _nextTime = nPgmTime + (t); \
00132                                         TModMsg(MOD_ID, INFO, p); \
00133                                     } \
00134                                 }
00135     #define TSampling(p)        TMsgPeriod(SAMPLING_PERIOD, p)
00136     #define TAssertPeriod(t,p,r) { \
00137                                     long _currTime = nPgmTime; \
00138                                     long _period = _currTime - (t); \
00139                                     t = _currTime; \
00140                                     if (abs(_period - (p)) > (r)) \
00141                                     { \
00142                                         TWarn(("AssertPeriod=%f", _period)); \
00143                                     } \
00144                                 }
00145     #define TPeriodStart()      if (nPgmTime >= g_TraceTime) \
00146                                 { \
00147                                     g_TraceTime = nPgmTime + TRACE_PERIOD; \
00148                                     TEnable(true); \
00149                                 }
00150     #define TPeriodEnd()        TEnable(false)
00151 #else
00152     #define TraceInit(m,l,e)
00153     #define TEnable(b)
00154     #define TFuncName(s)
00155     #define TLevel(l)
00156     #define TEnterMsg(p)
00157     #define TEnter()
00158     #define TExitMsg(p)
00159     #define TExit()
00160     #define TMsg(e,p)
00161     #define TFatal(p)
00162     #define TErr(p)
00163     #define TWarn(p)
00164     #define TInfo(p)
00165     #define TVerbose(p)
00166     #define TMsgPeriod(t,p)
00167     #define TSampling(p)
00168     #define TAssertPeriod(t,p,r)
00169     #define TPeriodStart()
00170     #define TPeriodEnd()
00171 #endif  //ifdef _DEBUG_TRACE
00172 
00173 #ifdef _DEBUG_TRACE
00174 
00175 long g_TraceModules = 0;
00176 int  g_TraceLevel = 0;
00177 int  g_MsgLevel = 0;
00178 int  g_IndentLevel = 0;
00179 bool g_TraceEnabled = false;
00180 long g_TraceTime = 0;
00181 
00189 void
00190 TracePrefix(
00191     __in string strFunc,
00192     __in bool fEnter,
00193     __in bool fNewLine
00194     )
00195 {
00196     if (fEnter)
00197     {
00198         g_IndentLevel++;
00199     }
00200 
00201     for (int i = 0; i < g_IndentLevel; ++i)
00202     {
00203         TPrintf("| ");
00204     }
00205 
00206     TPrintf(strFunc);
00207 
00208     if (fEnter)
00209     {
00210         TPrintf(fNewLine? "()\n": "(");
00211     }
00212     else
00213     {
00214         TPrintf(fNewLine? "!\n": "!");
00215         g_IndentLevel--;
00216     }
00217 
00218     return;
00219 }   //TracePrefix
00220 
00227 void
00228 MsgPrefix(
00229     __in string strFunc,
00230     __in int msgLevel
00231     )
00232 {
00233     string strPrefix;
00234 
00235     switch (msgLevel)
00236     {
00237         case FATAL:
00238             strPrefix = "_Fatal:";
00239             break;
00240 
00241         case ERR:
00242             strPrefix = "_Err:";
00243             break;
00244 
00245         case WARN:
00246             strPrefix = "_Warn:";
00247             break;
00248 
00249         case INFO:
00250             strPrefix = "_Info:";
00251             break;
00252 
00253         case VERBOSE:
00254             strPrefix = "_Verbose:";
00255             break;
00256 
00257         default:
00258             strPrefix = "_Unk:";
00259             break;
00260     }
00261     TPrintf("%s%s", strFunc, strPrefix);
00262 
00263     return;
00264 }   //MsgPrefix
00265 
00266 #endif  //ifdef _DEBUG_TRACE
00267 #endif  //ifndef _DBGTRACE_H
 All Data Structures Files Functions Variables Defines