كيفية تمكين تتبع الكلية في الإفراج عن الوضع ؟

StackOverflow https://stackoverflow.com/questions/27622

  •  09-06-2019
  •  | 
  •  

سؤال

على تتبع الكلية يمكن استخدامها لانتاج التشخيص رسائل إلى مصحح الأخطاء عندما يتم ترجمة التعليمات البرمجية في التصحيح الوضعية.أنا في حاجة إلى نفس الرسائل بينما في الإصدار الوضعية.هل هناك طريقة لتحقيق ذلك ؟

(يرجى لا إضاعة الوقت في مناقشة لماذا لا ينبغي أن تستخدم أثر في وضع الإصدار :-)

هل كانت مفيدة؟

المحلول

في الواقع ، فإن أثر الكلي هو الكثير أكثر مرونة من OutputDebugString.فإنه يأخذ printf() نمط شكل سلسلة المعلمة القائمة بينما OutputDebugString فقط يأخذ سلسلة واحدة.من أجل تنفيذ كامل أثر وظائف في وضع الإصدار تحتاج أن تفعل شيئا مثل هذا:

void trace(const char* format, ...)
{
   char buffer[1000];

   va_list argptr;
   va_start(argptr, format);
   wvsprintf(buffer, format, argptr);
   va_end(argptr);

   OutputDebugString(buffer);
}

نصائح أخرى

قبل بضع سنوات كنت بحاجة وظائف مماثلة لذلك أنا مرقع معا التعليمات البرمجية التالية.مجرد حفظه في ملف مثلاrtrace.ح ، إدراجه في نهاية stdafx.ح ، إضافة _RTRACE إلى الإفراج عن وضع المعالج يحدد.

ربما شخص ما سوف تجد الأمر :-)

جون

#pragma once

//------------------------------------------------------------------------------------------------
//
// Author:   John Cullen
// Date:     2006/04/12
// Based On: MSDN examples for variable argument lists and ATL implementation of TRACE.
//
// Description: Allows the use of TRACE statements in RELEASE builds, by overriding the
// TRACE macro definition and redefining in terms of the RTRACE class and overloaded
// operator (). Trace output is generated by calling OutputDebugString() directly.
//
//
// Usage:    Add to the end of stdafx.h and add _RTRACE to the preprocessor defines (typically
//           for RELEASE builds, although the flag will be ignored for DEBUG builds.
//
//------------------------------------------------------------------------------------------------

#ifdef _DEBUG

// NL defined as a shortcut for writing FTRACE(_T("\n")); for example, instead write FTRACE(NL);
#define NL _T("\n") 
#define LTRACE TRACE(_T("%s(%d): "), __FILE__, __LINE__); TRACE
#define FTRACE TRACE(_T("%s(%d): %s: "), __FILE__, __LINE__, __FUNCTION__); TRACE

#else   // _DEBUG

#ifdef _RTRACE
#undef TRACE
#define TRACE  RTRACE()
#define LTRACE RTRACE(__FILE__, __LINE__)
#define FTRACE RTRACE(__FILE__, __LINE__, __FUNCTION__)
#define NL _T("\n") 

class RTRACE
{
public:
    // default constructor, no params
    RTRACE(void) : m_pszFileName( NULL ), m_nLineNo( 0 ), m_pszFuncName( NULL ) {};

    // overloaded constructor, filename and lineno
    RTRACE(PCTSTR const pszFileName, int nLineNo) :
        m_pszFileName(pszFileName), m_nLineNo(nLineNo), m_pszFuncName(NULL) {};

    // overloaded constructor, filename, lineno, and function name
    RTRACE(PCTSTR const pszFileName, int nLineNo, PCTSTR const pszFuncName) :
        m_pszFileName(pszFileName), m_nLineNo(nLineNo), m_pszFuncName(pszFuncName) {};

    virtual ~RTRACE(void) {};

    // no arguments passed, e.g. RTRACE()()
    void operator()() const
    {
        // no arguments passed, just dump the file, line and function if requested
        OutputFileAndLine();
        OutputFunction();
    }

    // format string and parameters passed, e.g. RTRACE()(_T("%s\n"), someStringVar)
    void operator()(const PTCHAR pszFmt, ...) const
    {
        // dump the file, line and function if requested, followed by the TRACE arguments
        OutputFileAndLine();
        OutputFunction();

        // perform the standard TRACE output processing
        va_list ptr; va_start( ptr, pszFmt );
        INT len = _vsctprintf( pszFmt, ptr ) + 1;
        TCHAR* buffer = (PTCHAR) malloc( len * sizeof(TCHAR) );
        _vstprintf( buffer, pszFmt, ptr );
        OutputDebugString(buffer);
        free( buffer );
    }

private:
    // output the current file and line
    inline void OutputFileAndLine() const
    {
        if (m_pszFileName && _tcslen(m_pszFileName) > 0)
        {
            INT len = _sctprintf( _T("%s(%d): "), m_pszFileName, m_nLineNo ) + 1;
            PTCHAR buffer = (PTCHAR) malloc( len * sizeof(TCHAR) );
            _stprintf( buffer, _T("%s(%d): "), m_pszFileName, m_nLineNo );
            OutputDebugString( buffer );
            free( buffer );
        }
    }

    // output the current function name
    inline void OutputFunction() const
    {
        if (m_pszFuncName && _tcslen(m_pszFuncName) > 0)
        {
            INT len = _sctprintf( _T("%s: "), m_pszFuncName ) + 1;
            PTCHAR buffer = (PTCHAR) malloc( len * sizeof(TCHAR) );
            _stprintf( buffer, _T("%s: "), m_pszFuncName );
            OutputDebugString( buffer );
            free( buffer );
        }
    }

private:
    PCTSTR const m_pszFuncName;
    PCTSTR const m_pszFileName;
    const int m_nLineNo;
};

#endif // _RTRACE

#endif // NDEBUG

أثر هو مجرد الكلي على OutputDebugString.بحيث يمكنك بسهولة فقط لجعل الخاصة بك تتبع الكلية (أو يطلق عليه شيء آخر) التي سوف نداء OutputDebugString.

إنها ببساطة رمز أنني قد أرى

#undef ATLTRACE
#undef ATLTRACE2

#define ATLTRACE2 CAtlTrace(__FILE__, __LINE__, __FUNCTION__)
#define ATLTRACE ATLTRACE2

انظر http://alax.info/blog/1351

في MFC ، وتعقب بأنه ATLTRACE.و في وضع الإصدار التي يتم تعريفها بأنها:

#define ATLTRACE            __noop

وذلك باستخدام خارج منطقة الجزاء أثر من MFC ، لن يكون في الواقع قادرا على قراءة أي أثر النص, لأنه لن تكون مكتوبة.يمكن أن تكتب التتبع الخاص بك وظيفة بدلا من ذلك ، ثم إعادة تحديد أثر الماكرو.هل يمكن أن تفعل شيئا مثل هذا:

void MyTrace(const CString& text)
{
  ::OutputDebugString(text); // Outputs to console, same as regular TRACE
  // TODO: Do whatever output you need here. Write to event log / write to text file / write to pipe etc.
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top