Leaked source code of windows server 2003
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.

176 lines
4.0 KiB

  1. // DP8Log.cpp : Defines the entry point for the console application.
  2. //
  3. #include <windows.h>
  4. #include <stdio.h>
  5. #include "memlog.h"
  6. int _cdecl main(int argc, char* argv[])
  7. {
  8. HANDLE hFile = 0;
  9. HANDLE hMutex = 0;
  10. UINT timebase = 0;
  11. UINT i = 0;
  12. UINT nTotalEntries = 1;
  13. UINT time = 0;
  14. DWORD lcid;
  15. #ifdef TEST_MAX_SIZE
  16. UINT nMaxSize = 0;
  17. UINT nCurrentSize = 0;
  18. #endif
  19. PSHARED_LOG_FILE pLogFile = NULL;
  20. PMEMLOG_ENTRY pLog = NULL;
  21. PMEMLOG_ENTRY pReadEntry = NULL;
  22. OSVERSIONINFO osvi;
  23. osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
  24. GetVersionEx (&osvi);
  25. if (osvi.dwPlatformId == VER_PLATFORM_WIN32_NT)
  26. {
  27. hFile = CreateFileMappingA (INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, sizeof(SHARED_LOG_FILE), "Global\\" BASE_LOG_MEMFILENAME);
  28. }
  29. else
  30. {
  31. hFile = CreateFileMappingA (INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, sizeof(SHARED_LOG_FILE), BASE_LOG_MEMFILENAME);
  32. }
  33. if (!hFile)
  34. {
  35. goto exit;
  36. }
  37. if (osvi.dwPlatformId == VER_PLATFORM_WIN32_NT)
  38. {
  39. hMutex = CreateMutexA (NULL, FALSE, "Global\\" BASE_LOG_MUTEXNAME);
  40. }
  41. else
  42. {
  43. hMutex = CreateMutexA (NULL, FALSE, BASE_LOG_MUTEXNAME);
  44. }
  45. if (!hMutex)
  46. {
  47. goto exit;
  48. }
  49. pLogFile = (PSHARED_LOG_FILE) MapViewOfFile(hFile, FILE_MAP_READ|FILE_MAP_WRITE, 0, 0, 0);
  50. if(!pLogFile)
  51. {
  52. goto exit;
  53. }
  54. pLog = (PMEMLOG_ENTRY)(pLogFile + 1);
  55. // The user has asked us to clear the buffer, do that and return
  56. lcid = MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT);
  57. if (argc != 1 && CSTR_EQUAL == CompareString(lcid, NORM_IGNORECASE, argv[1], -1, TEXT("clearbuffer"), -1))
  58. {
  59. WaitForSingleObject(hMutex,INFINITE);
  60. ZeroMemory(pLog, pLogFile->nEntries * (sizeof(MEMLOG_ENTRY) + pLogFile->cbLine));
  61. pLogFile->iWrite = 0;
  62. ReleaseMutex(hMutex);
  63. goto exit;
  64. }
  65. if (argc == 1 || CSTR_EQUAL != CompareString(lcid, NORM_IGNORECASE, argv[1], -1, TEXT("nomutex"), -1))
  66. {
  67. WaitForSingleObject(hMutex,INFINITE);
  68. }
  69. // dump last half of buffer
  70. for(i = pLogFile->iWrite; i < pLogFile->nEntries; i++)
  71. {
  72. pReadEntry = (PMEMLOG_ENTRY)((PBYTE)&pLog[i] + pLogFile->cbLine * i);
  73. if (pReadEntry->str[0] != 0)
  74. {
  75. if (nTotalEntries == 1)
  76. timebase = pReadEntry->tLogged;
  77. if (pReadEntry->tLogged > timebase)
  78. time = pReadEntry->tLogged - timebase;
  79. else
  80. time = 0;
  81. #ifdef TEST_MAX_SIZE
  82. nCurrentSize = strlen(pReadEntry->str);
  83. if (nCurrentSize > nMaxSize)
  84. nMaxSize = nCurrentSize;
  85. #endif
  86. if (pReadEntry->str[1] == 0)
  87. {
  88. // str is Unicode
  89. wprintf(L"%5d: %8d %6d %s", nTotalEntries, pReadEntry->tLogged, time, (WCHAR*)pReadEntry->str);
  90. }
  91. else
  92. {
  93. // str is Ansi
  94. printf("%5d: %8d %6d %s", nTotalEntries, pReadEntry->tLogged, time, pReadEntry->str);
  95. }
  96. nTotalEntries++;
  97. timebase = pReadEntry->tLogged;
  98. }
  99. else
  100. {
  101. break;
  102. }
  103. }
  104. // dump first part of buffer
  105. for(i = 0; i<pLogFile->iWrite; i++)
  106. {
  107. pReadEntry = (PMEMLOG_ENTRY)((PBYTE)&pLog[i] + pLogFile->cbLine * i);
  108. if (nTotalEntries == 1)
  109. timebase = pReadEntry->tLogged;
  110. time = pReadEntry->tLogged - timebase;
  111. #ifdef TEST_MAX_SIZE
  112. nCurrentSize = strlen(pReadEntry->str);
  113. if (nCurrentSize > nMaxSize)
  114. nMaxSize = nCurrentSize;
  115. #endif
  116. if (pReadEntry->str[1] == 0)
  117. {
  118. // str is Unicode
  119. wprintf(L"%5d: %8d %6i %s", nTotalEntries, pReadEntry->tLogged, time, (WCHAR*)pReadEntry->str);
  120. }
  121. else
  122. {
  123. // str is Ansi
  124. printf("%5d: %8d %6i %s", nTotalEntries, pReadEntry->tLogged, time, pReadEntry->str);
  125. }
  126. nTotalEntries++;
  127. timebase = pReadEntry->tLogged;
  128. }
  129. #ifdef TEST_MAX_SIZE
  130. printf("Max Text Size was: %d\n", nMaxSize);
  131. #endif
  132. if (argc == 1 || CSTR_EQUAL != CompareString(lcid, NORM_IGNORECASE, argv[1], -1, TEXT("nomutex"), -1))
  133. {
  134. ReleaseMutex(hMutex);
  135. }
  136. exit:
  137. if (pLogFile)
  138. {
  139. UnmapViewOfFile(pLogFile);
  140. }
  141. if(hFile)
  142. {
  143. CloseHandle(hFile);
  144. }
  145. if(hMutex)
  146. {
  147. CloseHandle(hMutex);
  148. }
  149. return 0;
  150. }