home *** CD-ROM | disk | FTP | other *** search
/ Mastering Computers 3 / Mastering Computers Vol 3.iso / Win95 / Fun&Utils / MFCMSG.EXE / TRACEMSG.H < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-01  |  5.1 KB  |  162 lines

  1. ////////////////////////////////////////////////////////////////
  2. // TRACEMSG Copyright 1995 Microsoft Systems Journal. 
  3. // If this program works, it was written by Paul DiLascia.
  4. // If not, I don't know who wrote it.
  5. //
  6. // This file defines the tracing class and "phantom" template classes that
  7. // shadow MFC classes, overriding WindowProc, OnCommand and OnCmdMsg.
  8.  
  9. #ifndef TRACEMSG_H
  10. #define TRACEMSG_H
  11.  
  12. #if (_MFC_VER < 0x300)
  13. #error Sorry, this program requires MFC Version 3.0 or later.
  14. #endif
  15.  
  16. #ifdef _DEBUG
  17.  
  18. //////////////////
  19. // Trace class for CCmdTarget. Overrides OnCmdMsg to log the call.
  20. //
  21. template<class BASE>
  22. class TrCmdTarget : public BASE {
  23. public:
  24.    virtual BOOL OnCmdMsg(UINT nID, int nCode, void* pExtra,
  25.       AFX_CMDHANDLERINFO* pHandlerInfo);
  26. };
  27.  
  28. //////////////////
  29. // Trace class for CWnd. Overrides WindowProc and OnCommand.
  30. // 
  31. template<class BASE>
  32. class TrWnd : public TrCmdTarget<BASE> {
  33. public:
  34.    virtual LRESULT WindowProc(UINT msg, WPARAM wp, LPARAM lp);
  35.    virtual BOOL OnCommand(WPARAM wp, LPARAM lp);
  36. };
  37.  
  38. //////////////////
  39. // This class does the actual tracing, to keep template functions are small.
  40. // There's only one of these. It also implements Trace On/Off commands.
  41. //
  42. class CMsgTracer : public TrCmdTarget<CCmdTarget> {
  43.    DECLARE_DYNAMIC(CMsgTracer)
  44. private:
  45.    CMapWordToPtr m_mapMsg;    // Table of WM_XXX message names
  46.    CMapWordToPtr m_mapCmd;    // Table of MFC ID_XXX command ID names
  47.    CFile m_file;              // TRACE file (TRACE.OUT)
  48.    char  m_szIndent[80];      // indent string filled with '|' 
  49.    int   m_nCallDepth;        // number of nested calls
  50.  
  51.    // Push/pop call level for pretty printing
  52.    void PushCallLevel() {  
  53.       m_szIndent[m_nCallDepth++] = '|';
  54.       m_szIndent[m_nCallDepth] = 0;
  55.    }
  56.    void PopCallLevel() {
  57.       m_szIndent[--m_nCallDepth] = 0;
  58.    }
  59.  
  60.    // Helpers
  61.    LPCSTR GetMessageName(UINT msg);    // e.g. "WM_SIZE"
  62.    LPCSTR GetCommandName(UINT nID);    // e.g. "ID_FILE_OPEN"
  63.    LPCSTR GetCmdCodeName(int nCode);   // e.g. "CN_UPDATE_COMMAND_UI"
  64.  
  65. public:
  66.    BOOL  m_bTraceMsg;      // message tracing on
  67.    BOOL  m_bTraceCmd;      // command tracing on
  68.  
  69.    CMsgTracer();
  70.    ~CMsgTracer();
  71.  
  72.    void Write(LPCSTR str, int len=-1); // write a string to trace file
  73.  
  74.    BOOL TRACE_OnCmdMsg(CCmdTarget* pTarg, UINT nID, int nCode, void* pExtra, 
  75.       AFX_CMDHANDLERINFO* pHandlerInfo);
  76.    BOOL TRACE_WindowProc(CWnd* pWnd, UINT msg, WPARAM wp, LPARAM lp);
  77.    BOOL TRACE_OnCommand(CWnd* pWnd, WPARAM wp, LPARAM lp);
  78.  
  79.    void TRACE_ReturnWindowProc(LRESULT lRet);
  80.    void TRACE_ReturnOnCommand(BOOL bRet);
  81.    void TRACE_ReturnOnCmdMsg(BOOL bRet);
  82.  
  83.    // This object "Trace" commands
  84.    afx_msg void OnTraceMsg();
  85.    afx_msg void OnTraceCmd();
  86.    afx_msg void OnUpdateTraceCmd(CCmdUI* pCmdUI);
  87.    afx_msg void OnUpdateTraceMsg(CCmdUI* pCmdUI);
  88.    DECLARE_MESSAGE_MAP();
  89. };
  90.  
  91. extern CMsgTracer theTracer;     // the one-and-only tracing object
  92.  
  93. ////////////////////////////////////////////////////////////////
  94. // Template class "implementations." These template functions tell the
  95. // compiler how to generate the real functions for the template classes.
  96. // Override MFC functions OnCmdMsg, WindowProc, OnCommand.
  97. // TRACE the call, call the base class, then TRACE the return value.
  98. //
  99. // The implementation is similar for all: call CMsgTracer first
  100. // to trace the call, then the return value.
  101.  
  102. template<class BASE> 
  103. BOOL TrCmdTarget<BASE>::OnCmdMsg(UINT nID, int nCode, void* pExtra,
  104.    AFX_CMDHANDLERINFO* pHI)
  105. {
  106.    BOOL bTraced = theTracer.TRACE_OnCmdMsg(this, nID, nCode, pExtra, pHI);
  107.    BOOL bRet = BASE::OnCmdMsg(nID, nCode, pExtra, pHI);
  108.    if (bTraced)
  109.       theTracer.TRACE_ReturnOnCmdMsg(bRet);
  110.    return bRet;
  111. }
  112.  
  113. template<class BASE> 
  114. LRESULT TrWnd<BASE>::WindowProc(UINT msg, WPARAM wp, LPARAM lp)
  115. {
  116.    BOOL bTraced = theTracer.TRACE_WindowProc(this, msg, wp, lp);
  117.    LRESULT lRet = BASE::WindowProc(msg, wp, lp);
  118.    if (bTraced)
  119.       theTracer.TRACE_ReturnWindowProc(lRet);
  120.    return lRet;
  121. }
  122.  
  123. template<class BASE> 
  124. BOOL TrWnd<BASE>::OnCommand(WPARAM wp, LPARAM lp)
  125. {
  126.    BOOL bTraced = theTracer.TRACE_OnCommand(this, wp, lp);
  127.    BOOL bRet = BASE::OnCommand(wp, lp);
  128.    if (bTraced)
  129.       theTracer.TRACE_ReturnOnCommand(bRet);
  130.    return bRet;
  131. }
  132.  
  133. // Derive your app classes from these
  134. //
  135. #define TCmdTarget   TrCmdTarget<CCmdTarget>
  136. #define TWinApp      TrCmdTarget<CWinApp>
  137. #define TDocument    TrCmdTarget<CDocument>
  138.  
  139. #define TWnd         TrWnd<CWnd>
  140. #define TMDIFrameWnd TrWnd<CMDIFrameWnd>
  141. #define TMDIChildWnd TrWnd<CMDIChildWnd>
  142. #define TView        TrWnd<CView>
  143. #define TDialog      TrWnd<CDialog>
  144. #define TStatusBar   TrWnd<CStatusBar>
  145. #define TToolBar     TrWnd<CToolBar>
  146.  
  147. #else // not _DEBUG
  148.  
  149. #define TWinApp      CWinApp
  150. #define TDocument    CDocument
  151. #define TMDIFrameWnd CMDIFrameWnd
  152. #define TMDIChildWnd CMDIChildWnd
  153. #define TView        CView
  154. #define TDialog      CDialog
  155. #define TWnd         CWnd
  156. #define TStatusBar   CStatusBar
  157. #define TToolBar     CToolBar
  158.  
  159. #endif // _DEBUG
  160.  
  161. #endif // TRACEMSG_H
  162.