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.

120 lines
2.9 KiB

  1. #include "wsdueng.h"
  2. #define REGKEY_WUV3TEST "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\WindowsUpdate\\wuv3test"
  3. FILE* CLogger::c_pfile = stdout;
  4. int CLogger::c_cnIndent = 0;
  5. int CLogger::c_cnLevels = -1;
  6. CLogger::CLogger(
  7. const char* szBlockName /*= 0*/,
  8. int nLoggingLevel/*= 0*/,
  9. const char* szFileName/*= 0*/,
  10. int nLine/*= 0*/
  11. )
  12. {
  13. if (-1 == c_cnLevels)
  14. {
  15. /*c_cnLevels = 0;
  16. HKEY hkey;
  17. if (NO_ERROR == RegOpenKeyEx(HKEY_LOCAL_MACHINE, REGKEY_WUV3TEST, 0, KEY_READ, &hkey))
  18. {
  19. DWORD dwSize = sizeof(c_cnLevels);
  20. RegQueryValueEx(hkey, "LogLevel", 0, 0, (LPBYTE)&c_cnLevels, &dwSize);
  21. char szLogFile[MAX_PATH] = {0};
  22. dwSize = sizeof(szLogFile);
  23. RegQueryValueEx(hkey, "LogFile", 0, 0, (LPBYTE)&szLogFile, &dwSize);
  24. FILE* pfile = fopen(szLogFile, "at");
  25. if (pfile)
  26. {
  27. c_pfile = pfile;
  28. }
  29. RegCloseKey(hkey);
  30. }*/
  31. char sz_LogFileName[MAX_PATH];
  32. c_cnLevels = 4; // default to log level 4
  33. ExpandEnvironmentStrings(cszLoggingFile, sz_LogFileName, MAX_PATH);
  34. FILE* pfile = fopen(sz_LogFileName, "at");
  35. if (pfile)
  36. {
  37. c_pfile = pfile;
  38. }
  39. }
  40. m_szBlockName[0] = 0;
  41. //m_fOut = nLoggingLevel < c_cnLevels;
  42. m_fOut = TRUE;
  43. if (m_fOut && NULL != szBlockName)
  44. {
  45. lstrcpyn(m_szBlockName, szBlockName, sizeOfArray(m_szBlockName));
  46. //out("%s %s(%d)", szBlockName, szFileName, nLine);
  47. out("%s", szBlockName);
  48. m_dwStartTick = GetTickCount();
  49. c_cnIndent ++;
  50. }
  51. }
  52. CLogger::~CLogger()
  53. {
  54. if (c_pfile && m_fOut && NULL != m_szBlockName[0])
  55. {
  56. c_cnIndent --;
  57. out("~%s (%d msecs)", m_szBlockName, GetTickCount() - m_dwStartTick);
  58. }
  59. }
  60. void __cdecl CLogger::out(const char *szFormat, ...)
  61. {
  62. if (m_fOut)
  63. {
  64. va_list va;
  65. va_start (va, szFormat);
  66. v_out(szFormat, va);
  67. va_end (va);
  68. }
  69. }
  70. void __cdecl CLogger::error(const char *szFormat, ...)
  71. {
  72. if (m_fOut)
  73. {
  74. va_list va;
  75. va_start (va, szFormat);
  76. char szOut[4 * 1024];
  77. wvsprintf(szOut, szFormat, va);
  78. va_end (va);
  79. out("ERROR - %s", szOut);
  80. }
  81. }
  82. void __cdecl CLogger::out1(const char *szFormat, ...)
  83. {
  84. CLogger logger;
  85. va_list va;
  86. va_start (va, szFormat);
  87. logger.v_out(szFormat, va);
  88. va_end (va);
  89. }
  90. void CLogger::v_out( const char* szFormat, va_list va)
  91. {
  92. char szOut[5 * 1024];
  93. char* pszOut = szOut;
  94. // Indent first
  95. for(int i = 0; i < c_cnIndent; i ++)
  96. *(pszOut ++) = '\t';
  97. wvsprintf(pszOut, szFormat, va);
  98. // move the file pointer to the end
  99. if (0 == fseek(c_pfile, 0, SEEK_END))
  100. {
  101. fprintf(c_pfile, "%s\n", szOut);
  102. fflush(c_pfile);
  103. }
  104. }
  105. void CLogger::close(void)
  106. {
  107. if (c_pfile) fclose(c_pfile);
  108. c_pfile = NULL;
  109. c_cnLevels = -1;
  110. }