logging.h
Upload User: geng8029
Upload Date: 2021-01-30
Package Size: 187k
Code Size: 3k
Category:

Audio program

Development Platform:

Visual C++

  1. #pragma once
  2. #include <strsafe.h>
  3. #ifdef _DEBUG
  4. #include <crtdbg.h>
  5. #include <assert.h>
  6. #endif
  7. namespace MediaFoundationSamples
  8. {
  9.     //--------------------------------------------------------------------------------------
  10.     // Debug logging functions
  11.     // Description: Contains debug logging functions.
  12.     //
  13.     //     Initialize: Opens a logging file with the specified file name.
  14.     //     Trace: Writes a sprintf-formatted string to the logging file.
  15.     //     Close: Closes the logging file and reports any memory leaks.
  16.     //
  17.     // To enable logging in debug builds, #define USE_LOGGING.
  18.     // The TRACE_INIT, TRACE, and TRACE_CLOSE macros are mapped to the logging functions.
  19.     // 
  20.     // In retail builds, these macros are mapped to nothing.
  21.     //--------------------------------------------------------------------------------------
  22. #ifdef _DEBUG
  23.     class DebugLog
  24.     {
  25.     public:
  26.         static void Initialize()
  27.         {
  28.             _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_DEBUG);
  29.         }
  30.         static void  Trace(const WCHAR *sFormatString, ...)
  31.         {
  32.             HRESULT hr = S_OK;
  33.             va_list va;
  34.             const DWORD TRACE_STRING_LEN = 512;
  35.             WCHAR message[TRACE_STRING_LEN];
  36.             va_start(va, sFormatString);
  37.             hr = StringCchVPrintf(message, TRACE_STRING_LEN, sFormatString, va);
  38.             va_end(va);
  39.             if (SUCCEEDED(hr))
  40.             {
  41.                 _CrtDbgReport(_CRT_WARN, NULL, NULL, NULL, "%S", message);
  42.             }
  43.         }
  44.         static void Close()
  45.         {
  46.             int bLeak = _CrtDumpMemoryLeaks();
  47.             assert( bLeak == FALSE );
  48.         }
  49.     };
  50. #else
  51. // For retail builds, turn off USE_LOGGING
  52. #undef USE_LOGGING
  53. #endif
  54. #ifdef USE_LOGGING
  55.     #define TRACE_INIT() DebugLog::Initialize()
  56.     #define TRACE(x) DebugLog::Trace x
  57.     #define TRACE_CLOSE() DebugLog::Close()
  58.     // Log HRESULTs on failure.
  59.     inline HRESULT _LOG_HRESULT(HRESULT hr, const char* sFileName, long lLineNo)
  60.     {
  61.         if (FAILED(hr))
  62.         {
  63.             TRACE((L"%Sn", sFileName)); 
  64.             TRACE((L"Line: %d hr=0x%Xn", lLineNo, hr));
  65.         }
  66.         return hr;
  67.     }
  68.     #define LOG_HRESULT(hr) _LOG_HRESULT(hr, __FILE__, __LINE__)
  69.     #define LOG_MSG_IF_FAILED(msg, hr) if (FAILED(hr)) { TRACE((msg)); }
  70. #else
  71.     #define TRACE_INIT() 
  72.     #define TRACE(x) 
  73.     #define TRACE_CLOSE()
  74.     #define LOG_MSG_IF_FAILED(x, hr)
  75.     #define LOG_HRESULT(hr)
  76.     #define LOG_MSG_IF_FAILED(msg, hr)
  77. #endif
  78. } // namespace MediaFoundationSamples