ftclib/dbgtrace.h
author Trc492/3543
Sun Dec 16 22:58:56 2012 -0800 (2012-12-16)
changeset 107 fb1661a7bcdf
parent 34 f8380a2750e9
permissions -rw-r--r--
Final code from East District Championship
     1 #if 0
     2 /// Copyright (c) Titan Robotics Club. All rights reserved.
     3 ///
     4 /// <module name="dbgtrace.h" />
     5 ///
     6 /// <summary>
     7 ///     This module contains the tracing functions and definitions.
     8 /// </summary>
     9 ///
    10 /// <remarks>
    11 ///     Environment: RobotC for Lego Mindstorms NXT.
    12 /// </remarks>
    13 #endif
    14 
    15 #ifndef _DBGTRACE_H
    16 #define _DBGTRACE_H
    17 
    18 #pragma systemFile
    19 
    20 //
    21 // Constants.
    22 //
    23 #ifndef TRACE_MODULES
    24   #define TRACE_MODULES         0
    25 #endif
    26 #ifndef TRACE_LEVEL
    27   #define TRACE_LEVEL           TASK
    28 #endif
    29 #ifndef MSG_LEVEL
    30   #define MSG_LEVEL             INFO
    31 #endif
    32 
    33 //
    34 // Module ID.
    35 //
    36 #define MOD_LIB                 0xfffffff0
    37 #define MOD_GEN_MASK            0x0000000f
    38 #define MOD_BATT                0x00000010
    39 #define MOD_MENU                0x00000020
    40 #define MOD_NXTBTN              0x00000040
    41 #define MOD_JOYBTN              0x00000080
    42 #define MOD_TIMER               0x00000100
    43 #define MOD_SM                  0x00000200
    44 #define MOD_KALMAN              0x00000400
    45 #define MOD_ANALOG              0x00000800
    46 #define MOD_SENSOR              0x00001000
    47 #define MOD_ENCODER             0x00002000
    48 #define MOD_ACCEL               0x00004000
    49 #define MOD_GYRO                0x00008000
    50 #define MOD_COMPASS             0x00010000
    51 #define MOD_TOUCH               0x00020000
    52 #define MOD_IRSEEKER            0x00040000
    53 #define MOD_RADAR               0x00080000
    54 #define MOD_SERVO               0x00100000
    55 #define MOD_PIDCTRL             0x00200000
    56 #define MOD_DRIVE               0x00400000
    57 #define MOD_PIDDRIVE            0x00800000
    58 #define MOD_PIDMOTOR            0x01000000
    59 #define MOD_LNFOLLOW            0x02000000
    60 #define MOD_WALLFOLLOW          0x04000000
    61 
    62 #define MOD_MAIN                0x00000001
    63 #define TGenModId(n)            ((MOD_MAIN << (n)) && MOD_GEN_MASK)
    64 
    65 #define INIT                    0
    66 #define API                     1
    67 #define CALLBK                  2
    68 #define EVENT                   3
    69 #define FUNC                    4
    70 #define TASK                    5
    71 #define UTIL                    6
    72 #define HIFREQ                  7
    73 
    74 #define FATAL                   0
    75 #define ERR                     1
    76 #define WARN                    2
    77 #define INFO                    3
    78 #define VERBOSE                 4
    79 
    80 #ifndef TRACE_PERIOD
    81     #define TRACE_PERIOD        500     //in msec
    82 #endif
    83 
    84 #ifndef SAMPLING_PERIOD
    85     #define SAMPLING_PERIOD     500     //in msec
    86 #endif
    87 
    88 #define TPrintf                 writeDebugStream
    89 #define TPrintfLine             writeDebugStreamLine
    90 
    91 //
    92 // Trace macros.
    93 //
    94 #ifdef _DEBUG_TRACE
    95     #define TModEnterMsg(m,p)   if (g_TraceEnabled && \
    96                                     ((g_TraceModules & (m)) != 0) && \
    97                                     (_levelTrace <= g_TraceLevel)) \
    98                                 { \
    99                                     TracePrefix(_pszFuncName, true, false); \
   100                                     TPrintf p; \
   101                                     TPrintf(")\n"); \
   102                                 }
   103     #define TModEnter(m)        if (g_TraceEnabled && \
   104                                     ((g_TraceModules & (m)) != 0) && \
   105                                     (_levelTrace <= g_TraceLevel)) \
   106                                 { \
   107                                     TracePrefix(_pszFuncName, true, true); \
   108                                 }
   109     #define TModExitMsg(m,p)    if (g_TraceEnabled && \
   110                                     ((g_TraceModules & (m)) != 0) && \
   111                                     (_levelTrace <= g_TraceLevel)) \
   112                                 { \
   113                                     TracePrefix(_pszFuncName, false, false); \
   114                                     TPrintfLine p; \
   115                                 }
   116     #define TModExit(m)         if (g_TraceEnabled && \
   117                                     ((g_TraceModules & (m)) != 0) && \
   118                                     (_levelTrace <= g_TraceLevel)) \
   119                                 { \
   120                                     TracePrefix(_pszFuncName, false, true); \
   121                                 }
   122     #define TModMsg(m,e,p)      if (g_TraceEnabled && \
   123                                     ((g_TraceModules & (m)) != 0) && \
   124                                     ((e) <= g_MsgLevel)) \
   125                                 { \
   126                                     MsgPrefix(_pszFuncName, e); \
   127                                     TPrintfLine p; \
   128                                 }
   129     #define TraceInit(m,l,e)    { \
   130                                     g_TraceModules = (m); \
   131                                     g_TraceLevel = (l); \
   132                                     g_MsgLevel = (e); \
   133                                     g_TraceEnabled = false; \
   134                                     g_TraceTime = nPgmTime; \
   135                                 }
   136     #define TEnable(b)          g_TraceEnabled = b
   137     #define TFuncName(s)        char *_pszFuncName = s
   138     #define TLevel(l)           int _levelTrace = l
   139     #define TEnterMsg(p)        TModEnterMsg(MOD_ID, p)
   140     #define TEnter()            TModEnter(MOD_ID)
   141     #define TExitMsg(p)         TModExitMsg(MOD_ID, p)
   142     #define TExit()             TModExit(MOD_ID)
   143     #define TMsg(e,p)           TModMsg(MOD_ID, e, p)
   144     #define TFatal(p)           TModMsg(MOD_ID, FATAL, p)
   145     #define TErr(p)             TModMsg(MOD_ID, ERR, p)
   146     #define TWarn(p)            TModMsg(MOD_ID, WARN, p)
   147     #define TInfo(p)            TModMsg(MOD_ID, INFO, p)
   148     #define TVerbose(p)         TModMsg(MOD_ID, VERBOSE, p)
   149     #define TMsgPeriod(t,p)     { \
   150                                     static long _nextTime = nPgmTime; \
   151                                     if (nPgmTime >= _nextTime) \
   152                                     { \
   153                                         _nextTime = nPgmTime + (t); \
   154                                         TModMsg(MOD_ID, INFO, p); \
   155                                     } \
   156                                 }
   157     #define TSampling(p)        TMsgPeriod(SAMPLING_PERIOD, p)
   158     #define TAssertPeriod(t,p,r) { \
   159                                     long _currTime = nPgmTime; \
   160                                     long _period = _currTime - (t); \
   161                                     t = _currTime; \
   162                                     if (abs(_period - (p)) > (r)) \
   163                                     { \
   164                                         TWarn(("AssertPeriod=%f", _period)); \
   165                                     } \
   166                                 }
   167     #define TPeriodStart()      if (nPgmTime >= g_TraceTime) \
   168                                 { \
   169                                     g_TraceTime = nPgmTime + TRACE_PERIOD; \
   170                                     TEnable(true); \
   171                                 }
   172     #define TPeriodEnd()        TEnable(false)
   173 #else
   174     #define TraceInit(m,l,e)
   175     #define TEnable(b)
   176     #define TFuncName(s)
   177     #define TLevel(l)
   178     #define TEnterMsg(p)
   179     #define TEnter()
   180     #define TExitMsg(p)
   181     #define TExit()
   182     #define TMsg(e,p)
   183     #define TFatal(p)
   184     #define TErr(p)
   185     #define TWarn(p)
   186     #define TInfo(p)
   187     #define TVerbose(p)
   188     #define TMsgPeriod(t,p)
   189     #define TSampling(p)
   190     #define TAssertPeriod(t,p,r)
   191     #define TPeriodStart()
   192     #define TPeriodEnd()
   193 #endif  //ifdef _DEBUG_TRACE
   194 
   195 #ifdef _DEBUG_TRACE
   196 
   197 long g_TraceModules = 0;
   198 int  g_TraceLevel = 0;
   199 int  g_MsgLevel = 0;
   200 int  g_IndentLevel = 0;
   201 bool g_TraceEnabled = false;
   202 long g_TraceTime = 0;
   203 
   204 /**
   205  *  This function prints the trace prefix string to the debug stream.
   206  *
   207  *  @param pszFuncName Specifies the function name.
   208  *  @param fEnter Specifies whether this is a TEnter or TExit.
   209  *  @param fNewLine Specifies whether it should print a newline.
   210  */
   211 void
   212 TracePrefix(
   213     char *pszFuncName,
   214     bool fEnter,
   215     bool fNewLine
   216     )
   217 {
   218     if (fEnter)
   219     {
   220         g_IndentLevel++;
   221     }
   222 
   223     for (int i = 0; i < g_IndentLevel; ++i)
   224     {
   225         TPrintf("| ");
   226     }
   227 
   228     TPrintf(pszFuncName);
   229 
   230     if (fEnter)
   231     {
   232         TPrintf(fNewLine? "()\n": "(");
   233     }
   234     else
   235     {
   236         TPrintf(fNewLine? "!\n": "!");
   237         g_IndentLevel--;
   238     }
   239 
   240     return;
   241 }   //TracePrefix
   242 
   243 /**
   244  *  This function prints the message prefix string to the debug stream.
   245  *
   246  *  @param pszFuncName Specifies the function name.
   247  *  @param msgLevel Specifies the message level.
   248  */
   249 void
   250 MsgPrefix(
   251     char *pszFuncName,
   252     int msgLevel
   253     )
   254 {
   255     char *pszPrefix;
   256 
   257     switch (msgLevel)
   258     {
   259         case FATAL:
   260             pszPrefix = "_Fatal:";
   261             break;
   262 
   263         case ERR:
   264             pszPrefix = "_Err:";
   265             break;
   266 
   267         case WARN:
   268             pszPrefix = "_Warn:";
   269             break;
   270 
   271         case INFO:
   272             pszPrefix = "_Info:";
   273             break;
   274 
   275         case VERBOSE:
   276             pszPrefix = "_Verbose:";
   277             break;
   278 
   279         default:
   280             pszPrefix = "_Unk:";
   281             break;
   282     }
   283     TPrintf("%s%s", pszFuncName, pszPrefix);
   284 
   285     return;
   286 }   //MsgPrefix
   287 
   288 #endif  //ifdef _DEBUG_TRACE
   289 #endif  //ifndef _DBGTRACE_H