wxdebug.cpp.svn-base

来自「ffshow源码」· SVN-BASE 代码 · 共 981 行 · 第 1/3 页

SVN-BASE
981
字号
//------------------------------------------------------------------------------// File: WXDebug.cpp//// Desc: DirectShow base classes - implements ActiveX system debugging//       facilities.    //// Copyright (c) 1992-2002 Microsoft Corporation.  All rights reserved.//------------------------------------------------------------------------------#include "stdafx.h"#ifdef DEBUG#ifdef UNICODE#ifndef _UNICODE#define _UNICODE#endif // _UNICODE#endif // UNICODE#endif // DEBUG#include <tchar.h>#ifdef DEBUG// The Win32 wsprintf() function writes a maximum of 1024 characters to it's output buffer.// See the documentation for wsprintf()'s lpOut parameter for more information.const INT iDEBUGINFO = 1024;                 // Used to format strings/* For every module and executable we store a debugging level for each of   the five categories (eg LOG_ERROR and LOG_TIMING). This makes it easy   to isolate and debug individual modules without seeing everybody elses   spurious debug output. The keys are stored in the registry under the   HKEY_LOCAL_MACHINE\SOFTWARE\Debug\<Module Name>\<KeyName> key values   NOTE these must be in the same order as their enumeration definition */TCHAR *pKeyNames[] = {    TEXT("TIMING"),      // Timing and performance measurements    TEXT("TRACE"),       // General step point call tracing    TEXT("MEMORY"),      // Memory and object allocation/destruction    TEXT("LOCKING"),     // Locking/unlocking of critical sections    TEXT("ERROR"),       // Debug error notification    TEXT("CUSTOM1"),    TEXT("CUSTOM2"),    TEXT("CUSTOM3"),    TEXT("CUSTOM4"),    TEXT("CUSTOM5")    };const TCHAR CAutoTrace::_szEntering[] = TEXT("Entering: %s");const TCHAR CAutoTrace::_szLeaving[]  = TEXT("Leaving: %s");const INT iMAXLEVELS = NUMELMS(pKeyNames);  // Maximum debug categoriesHINSTANCE m_hInst;                          // Module instance handleTCHAR m_ModuleName[iDEBUGINFO];             // Cut down module nameDWORD m_Levels[iMAXLEVELS];                 // Debug level per categoryCRITICAL_SECTION m_CSDebug;                 // Controls access to listDWORD m_dwNextCookie;                       // Next active object IDObjectDesc *pListHead = NULL;               // First active objectDWORD m_dwObjectCount;                      // Active object countBOOL m_bInit = FALSE;                       // Have we been initialisedHANDLE m_hOutput = INVALID_HANDLE_VALUE;    // Optional output written hereDWORD dwWaitTimeout = INFINITE;             // Default timeout valueDWORD dwTimeOffset;			    // Time of first DbgLog callbool g_fUseKASSERT = false;                 // don't create messageboxbool g_fDbgInDllEntryPoint = false;bool g_fAutoRefreshLevels = false;const TCHAR *pBaseKey = TEXT("SOFTWARE\\Debug");const TCHAR *pGlobalKey = TEXT("GLOBAL");static CHAR *pUnknownName = "UNKNOWN";TCHAR *TimeoutName = TEXT("TIMEOUT");/* This sets the instance handle that the debug library uses to find   the module's file name from the Win32 GetModuleFileName function */void WINAPI DbgInitialise(HINSTANCE hInst){    InitializeCriticalSection(&m_CSDebug);    m_bInit = TRUE;    m_hInst = hInst;    DbgInitModuleName();    if (GetProfileInt(m_ModuleName, TEXT("BreakOnLoad"), 0))       DebugBreak();    DbgInitModuleSettings(false);    DbgInitGlobalSettings(true);    dwTimeOffset = timeGetTime();}/* This is called to clear up any resources the debug library uses - at the   moment we delete our critical section and the object list. The values we   retrieve from the registry are all done during initialisation but we don't   go looking for update notifications while we are running, if the values   are changed then the application has to be restarted to pick them up */void WINAPI DbgTerminate(){    if (m_hOutput != INVALID_HANDLE_VALUE) {       EXECUTE_ASSERT(CloseHandle(m_hOutput));       m_hOutput = INVALID_HANDLE_VALUE;    }    DeleteCriticalSection(&m_CSDebug);    m_bInit = FALSE;}/* This is called by DbgInitLogLevels to read the debug settings   for each logging category for this module from the registry */void WINAPI DbgInitKeyLevels(HKEY hKey, bool fTakeMax){    LONG lReturn;               // Create key return value    LONG lKeyPos;               // Current key category    DWORD dwKeySize;            // Size of the key value    DWORD dwKeyType;            // Receives it's type    DWORD dwKeyValue;           // This fields value    /* Try and read a value for each key position in turn */    for (lKeyPos = 0;lKeyPos < iMAXLEVELS;lKeyPos++) {        dwKeySize = sizeof(DWORD);        lReturn = RegQueryValueEx(            hKey,                       // Handle to an open key            pKeyNames[lKeyPos],         // Subkey name derivation            NULL,                       // Reserved field            &dwKeyType,                 // Returns the field type            (LPBYTE) &dwKeyValue,       // Returns the field's value            &dwKeySize );               // Number of bytes transferred        /* If either the key was not available or it was not a DWORD value           then we ensure only the high priority debug logging is output           but we try and update the field to a zero filled DWORD value */        if (lReturn != ERROR_SUCCESS || dwKeyType != REG_DWORD)  {            dwKeyValue = 0;            lReturn = RegSetValueEx(                hKey,                   // Handle of an open key                pKeyNames[lKeyPos],     // Address of subkey name                (DWORD) 0,              // Reserved field                REG_DWORD,              // Type of the key field                (PBYTE) &dwKeyValue,    // Value for the field                sizeof(DWORD));         // Size of the field buffer            if (lReturn != ERROR_SUCCESS) {                DbgLog((LOG_ERROR,0,TEXT("Could not create subkey %s"),pKeyNames[lKeyPos]));                dwKeyValue = 0;            }        }        if(fTakeMax)        {            m_Levels[lKeyPos] = std::max(dwKeyValue,m_Levels[lKeyPos]);        }        else        {            if((m_Levels[lKeyPos] & LOG_FORCIBLY_SET) == 0) {                m_Levels[lKeyPos] = dwKeyValue;            }        }    }    /*  Read the timeout value for catching hangs */    dwKeySize = sizeof(DWORD);    lReturn = RegQueryValueEx(        hKey,                       // Handle to an open key        TimeoutName,                // Subkey name derivation        NULL,                       // Reserved field        &dwKeyType,                 // Returns the field type        (LPBYTE) &dwWaitTimeout,    // Returns the field's value        &dwKeySize );               // Number of bytes transferred    /* If either the key was not available or it was not a DWORD value       then we ensure only the high priority debug logging is output       but we try and update the field to a zero filled DWORD value */    if (lReturn != ERROR_SUCCESS || dwKeyType != REG_DWORD)  {        dwWaitTimeout = INFINITE;        lReturn = RegSetValueEx(            hKey,                   // Handle of an open key            TimeoutName,            // Address of subkey name            (DWORD) 0,              // Reserved field            REG_DWORD,              // Type of the key field            (PBYTE) &dwWaitTimeout, // Value for the field            sizeof(DWORD));         // Size of the field buffer        if (lReturn != ERROR_SUCCESS) {            DbgLog((LOG_ERROR,0,TEXT("Could not create subkey %s"),pKeyNames[lKeyPos]));            dwWaitTimeout = INFINITE;        }    }}void WINAPI DbgOutString(LPCTSTR psz){    if (m_hOutput != INVALID_HANDLE_VALUE) {        UINT  cb = lstrlen(psz);        DWORD dw;#ifdef UNICODE        CHAR szDest[2048];        WideCharToMultiByte(CP_ACP, 0, psz, -1, szDest, NUMELMS(szDest), 0, 0);        WriteFile (m_hOutput, szDest, cb, &dw, NULL);#else        WriteFile (m_hOutput, psz, cb, &dw, NULL);#endif    } else {        OutputDebugString (psz);    }}/* Called by DbgInitGlobalSettings to setup alternate logging destinations */void WINAPI DbgInitLogTo (    HKEY hKey){    LONG  lReturn;    DWORD dwKeyType;    DWORD dwKeySize;    TCHAR szFile[MAX_PATH] = {0};    static const TCHAR cszKey[] = TEXT("LogToFile");    dwKeySize = MAX_PATH;    lReturn = RegQueryValueEx(        hKey,                       // Handle to an open key        cszKey,                     // Subkey name derivation        NULL,                       // Reserved field        &dwKeyType,                 // Returns the field type        (LPBYTE) szFile,            // Returns the field's value        &dwKeySize);                // Number of bytes transferred    // create an empty key if it does not already exist    //    if (lReturn != ERROR_SUCCESS || dwKeyType != REG_SZ)       {       dwKeySize = sizeof(TCHAR);       lReturn = RegSetValueEx(            hKey,                   // Handle of an open key            cszKey,                 // Address of subkey name            (DWORD) 0,              // Reserved field            REG_SZ,                 // Type of the key field            (PBYTE)szFile,          // Value for the field            dwKeySize);            // Size of the field buffer       }    // if an output-to was specified.  try to open it.    //    if (m_hOutput != INVALID_HANDLE_VALUE) {       EXECUTE_ASSERT(CloseHandle (m_hOutput));       m_hOutput = INVALID_HANDLE_VALUE;    }    if (szFile[0] != 0)       {       if (!lstrcmpi(szFile, TEXT("Console"))) {          m_hOutput = GetStdHandle (STD_OUTPUT_HANDLE);          if (m_hOutput == INVALID_HANDLE_VALUE) {             AllocConsole ();             m_hOutput = GetStdHandle (STD_OUTPUT_HANDLE);          }          SetConsoleTitle (TEXT("ActiveX Debug Output"));       } else if (szFile[0] &&                lstrcmpi(szFile, TEXT("Debug")) &&                lstrcmpi(szFile, TEXT("Debugger")) &&                lstrcmpi(szFile, TEXT("Deb")))          {            m_hOutput = CreateFile(szFile, GENERIC_WRITE,                                 FILE_SHARE_READ,                                 NULL, OPEN_ALWAYS,                                 FILE_ATTRIBUTE_NORMAL,                                 NULL);          if (INVALID_HANDLE_VALUE != m_hOutput)              {              static const TCHAR cszBar[] = TEXT("\r\n\r\n=====DbgInitialize()=====\r\n\r\n");              SetFilePointer (m_hOutput, 0, NULL, FILE_END);              DbgOutString (cszBar);              }          }       }}/* This is called by DbgInitLogLevels to read the global debug settings for   each logging category for this module from the registry. Normally each   module has it's own values set for it's different debug categories but   setting the global SOFTWARE\Debug\Global applies them to ALL modules */void WINAPI DbgInitGlobalSettings(bool fTakeMax){    LONG lReturn;               // Create key return value    TCHAR szInfo[iDEBUGINFO];   // Constructs key names    HKEY hGlobalKey;            // Global override key    /* Construct the global base key name */    wsprintf(szInfo,TEXT("%s\\%s"),pBaseKey,pGlobalKey);    /* Create or open the key for this module */    lReturn = RegCreateKeyEx(HKEY_LOCAL_MACHINE,   // Handle of an open key                             szInfo,               // Address of subkey name                             (DWORD) 0,            // Reserved value                             NULL,                 // Address of class name                             (DWORD) 0,            // Special options flags                             KEY_ALL_ACCESS,       // Desired security access                             NULL,                 // Key security descriptor                             &hGlobalKey,          // Opened handle buffer                             NULL);                // What really happened    if (lReturn != ERROR_SUCCESS) {        DbgLog((LOG_ERROR,0,TEXT("Could not access GLOBAL module key")));        return;    }    DbgInitKeyLevels(hGlobalKey, fTakeMax);    RegCloseKey(hGlobalKey);}/* This sets the debugging log levels for the different categories. We start   by opening (or creating if not already available) the SOFTWARE\Debug key   that all these settings live under. We then look at the global values   set under SOFTWARE\Debug\Global which apply on top of the individual   module settings. We then load the individual module registry settings */void WINAPI DbgInitModuleSettings(bool fTakeMax)

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?