Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

133 lines
2.8 KiB

  1. //=======================================================================
  2. //
  3. // Copyright (c) 1998-1999 Microsoft Corporation. All Rights Reserved.
  4. //
  5. // File: log.cpp
  6. //
  7. // Original Author: Yan Leshinsky
  8. //
  9. // Description:
  10. //
  11. // Logging support implementation
  12. //
  13. //=======================================================================
  14. #if 1
  15. #include <windows.h>
  16. #include <ole2.h>
  17. #include <tchar.h>
  18. #include <atlconv.h>
  19. #include <wustl.h>
  20. #include <log.h>
  21. #include "cdmlibp.h"
  22. const TCHAR* LIVE_LOG_FILENAME = _T("c:\\wuv3live.log");
  23. FILE* CLogger::c_pfile = stdout;
  24. int CLogger::c_cnIndent = 0;
  25. int CLogger::c_cnLevels = -1;
  26. CLogger::CLogger(
  27. const char* szBlockName /*= 0*/,
  28. int nLoggingLevel/*= 0*/,
  29. const char* szFileName/*= 0*/,
  30. int nLine/*= 0*/
  31. ) {
  32. USES_CONVERSION;
  33. if (-1 == c_cnLevels)
  34. {
  35. c_cnLevels = 0;
  36. auto_hkey hkey;
  37. if (NO_ERROR == RegOpenKeyEx(HKEY_LOCAL_MACHINE, REGKEY_WUV3TEST, 0, KEY_READ, &hkey)) {
  38. DWORD dwSize = sizeof(c_cnLevels);
  39. RegQueryValueEx(hkey, _T("LogLevel"), 0, 0, (LPBYTE)&c_cnLevels, &dwSize);
  40. TCHAR szLogFile[MAX_PATH] = {0};
  41. #ifdef _WUV3TEST
  42. // RAID# 156253
  43. dwSize = sizeof(szLogFile);
  44. RegQueryValueEx(hkey, _T("LogFile"), 0, 0, (LPBYTE)&szLogFile, &dwSize);
  45. #else
  46. // Hardwire log to root of c: for "Live" controls
  47. _tcscpy(szLogFile, LIVE_LOG_FILENAME);
  48. #endif
  49. FILE* pfile = _tfopen(szLogFile, _T("at"));
  50. if (pfile)
  51. c_pfile = pfile;
  52. }
  53. }
  54. m_szBlockName[0] = 0;
  55. m_fOut = nLoggingLevel < c_cnLevels;
  56. if (m_fOut && NULL != szBlockName)
  57. {
  58. strncpy(m_szBlockName, szBlockName, sizeOfArray(m_szBlockName));
  59. #ifdef _WUV3TEST
  60. // RAID# 146771
  61. out("%s %s(%d)", A2T(const_cast<char*>(szBlockName)), A2T(const_cast<char*>(szFileName)), nLine);
  62. #else
  63. out("%s", A2T(const_cast<char*>(szBlockName)));
  64. #endif
  65. m_dwStartTick = GetTickCount();
  66. c_cnIndent ++;
  67. }
  68. }
  69. CLogger::~CLogger()
  70. {
  71. USES_CONVERSION;
  72. if (m_fOut && NULL != m_szBlockName[0])
  73. {
  74. c_cnIndent --;
  75. out("~%s (%d msecs)", A2T(const_cast<char*>(m_szBlockName)), GetTickCount() - m_dwStartTick);
  76. }
  77. }
  78. void __cdecl CLogger::out(const char *szFormat, ...)
  79. {
  80. if (m_fOut)
  81. {
  82. va_list va;
  83. va_start (va, szFormat);
  84. tab_out();
  85. v_out(szFormat, va);
  86. va_end (va);
  87. }
  88. }
  89. void __cdecl CLogger::error(const char *szFormat, ...)
  90. {
  91. USES_CONVERSION;
  92. if (m_fOut)
  93. {
  94. va_list va;
  95. va_start (va, szFormat);
  96. tab_out();
  97. _fputts(_T("ERROR - "), c_pfile);
  98. v_out(szFormat, va);
  99. va_end (va);
  100. }
  101. }
  102. void __cdecl CLogger::out1(const char *szFormat, ...)
  103. {
  104. CLogger logger;
  105. va_list va;
  106. va_start (va, szFormat);
  107. logger.v_out(szFormat, va);
  108. va_end (va);
  109. }
  110. void CLogger::v_out( const char* szFormat, va_list va)
  111. {
  112. USES_CONVERSION;
  113. _vftprintf(c_pfile, A2T(const_cast<char*>(szFormat)), va);
  114. _fputtc(_T('\n'), c_pfile);
  115. fflush(c_pfile);
  116. }
  117. #endif