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.

352 lines
9.7 KiB

  1. /*****************************************************************************************************************
  2. FILENAME: Logging.cpp
  3. COPYRIGHT 2001 Microsoft Corporation and Executive Software International, Inc.
  4. How a program logs events:
  5. 1) Init logging is called to set up logging for this process and get logging info from the registry.
  6. 2) LogEvent is called each time you want to log something.
  7. 3) CleanupLogging is called on exit.
  8. The following events are logged if the user has them selected to log in log options:
  9. See the LOG_OPTIONS structure in header file.
  10. Defragment Engine starts or stops = bLogDefragStartStop - logged by the Controller
  11. File was defragmented = bLogFilesDefragged - logged by the Engine.
  12. File was moved = bLogFilesMoved - logged by the Engine.
  13. Summary is logged after defragmentation pass = bLogDefragSummary - logged by the Controller
  14. The corresponding registry entries are Values within the standard Diskeeper directory tree \UserSettings\
  15. Type REG_SZ - a 1 means yes, log it, 0 means no don't log it.
  16. bLogDefragStartStop - LogDefragStartStop
  17. bLogFilesDefragged - LogFilesDefragged
  18. bLogFilesMoved - LogFilesMoved
  19. bLogDefragSummary - LogDefragSummary
  20. REVISION HISTORY:
  21. 0.0E00 - ? 1996 - Zack Gainsforth - created module.
  22. 1.0E00 - 22 April 1997 - Zack Gainsforth - extracted module and nuked half so as not not have complexity
  23. of the communication methods used in the old diskeeper.
  24. /****************************************************************************************************************/
  25. #include "stdafx.h"
  26. #ifndef NOEVTLOG
  27. #include <windows.h>
  28. #include "ErrMacro.h"
  29. #include "Event.h"
  30. #include "GetReg.h"
  31. #include "Logging.h"
  32. //This is the structure that contains the logging data - if any of these fields are true then that indicates
  33. //that the appropriate message should be logged.
  34. typedef struct{
  35. BOOL bLogDefragStartStop;
  36. BOOL bLogFilesDefragged;
  37. BOOL bLogFilesMoved;
  38. BOOL bLogDefragSummary;
  39. BOOL bLogServiceStartStop;
  40. BOOL bLogFileInformation;
  41. BOOL bLogDiskInformation;
  42. BOOL bLogPageFileInformation;
  43. BOOL bLogDirectoryInformation;
  44. BOOL bLogMFTInformation;
  45. } LOG_OPTIONS;
  46. static LOG_OPTIONS LogOptions; //This is a global to this module only.
  47. static HANDLE hEventSource; //The Handle to the event log.
  48. /*****************************************************************************************************************
  49. COPYRIGHT 2001 Microsoft Corporation and Executive Software International, Inc.
  50. ROUTINE DESCRIPTION:
  51. This routine initializes logging for the current process.
  52. INPUT + OUTPUT:
  53. cEventSource - a char string giving the event source to be passed into RegisterEventSource
  54. GLOBALS:
  55. None.
  56. RETURN:
  57. TRUE - Success
  58. FALSE - Fatal Error
  59. */
  60. BOOL InitLogging(
  61. PTCHAR cEventSource
  62. )
  63. {
  64. //0.0E00 Fill LogOptions structure (GetLogOptionsFromRegistry)
  65. EF(GetLogOptionsFromRegistry());
  66. //0.0E00 Open the Event Log (RegisterEventSource)
  67. if ((hEventSource = RegisterEventSource(NULL, cEventSource)) == NULL) {
  68. //0.0E00 RPC_S_UNKNOWN_IF is an error that happens on shut down, this is a kludge.
  69. EF(GetLastError() != RPC_S_UNKNOWN_IF);
  70. return FALSE;
  71. }
  72. return TRUE;
  73. }
  74. /*****************************************************************************************************************
  75. COPYRIGHT 2001 Microsoft Corporation and Executive Software International, Inc.
  76. ROUTINE DESCRIPTION:
  77. This routine retrieves logging options from the registry.
  78. INPUT + OUTPUT:
  79. None.
  80. GLOBALS:
  81. LogOptions - Contains the options for what sort of messages to log.
  82. RETURN:
  83. TRUE - Success
  84. FALSE - Fatal Error
  85. */
  86. BOOL GetLogOptionsFromRegistry(
  87. )
  88. {
  89. HKEY hKey = NULL;
  90. TCHAR cRegValue[10];
  91. DWORD dwRegValueSize;
  92. TCHAR *cLoggingRoot = L"SOFTWARE\\Microsoft\\Dfrg";
  93. //0.0E00 Fill out LogOptions structure with the various registry values.
  94. __try {
  95. //The first call gets handle to registry.
  96. dwRegValueSize = sizeof(cRegValue);
  97. #ifdef DKMS
  98. EF(GetRegValue(&hKey,
  99. cLoggingRoot,
  100. TEXT("LogDefragStartStop"),
  101. cRegValue,
  102. &dwRegValueSize) == ERROR_SUCCESS);
  103. #else
  104. EF(GetRegValue(&hKey,
  105. TEXT("SOFTWARE\\Executive Software\\Diskeeper\\UserSettings"),
  106. TEXT("LogServiceStartStop"),
  107. cRegValue,
  108. &dwRegValueSize) == ERROR_SUCCESS);
  109. LogOptions.bLogServiceStartStop = (cRegValue[0] == TEXT('1')) ? TRUE : FALSE;
  110. //Get all the other values now that the key is opened.
  111. dwRegValueSize = sizeof(cRegValue);
  112. EF(GetRegValue(&hKey, NULL, TEXT("LogDefragStartStop"), cRegValue, &dwRegValueSize) == ERROR_SUCCESS);
  113. #endif
  114. LogOptions.bLogDefragStartStop = (cRegValue[0] == TEXT('1')) ? TRUE : FALSE;
  115. //0.0E00 Get all the other values now that the key is opened.
  116. dwRegValueSize = sizeof(cRegValue);
  117. EF(GetRegValue(&hKey, NULL, TEXT("LogFilesDefragged"), cRegValue, &dwRegValueSize) == ERROR_SUCCESS);
  118. LogOptions.bLogFilesDefragged = (cRegValue[0] == TEXT('1')) ? TRUE : FALSE;
  119. dwRegValueSize = sizeof(cRegValue);
  120. EF(GetRegValue(&hKey, NULL, TEXT("LogFilesMoved"), cRegValue, &dwRegValueSize) == ERROR_SUCCESS);
  121. LogOptions.bLogFilesMoved = (cRegValue[0] == TEXT('1')) ? TRUE : FALSE;
  122. #ifdef DKMS
  123. dwRegValueSize = sizeof(cRegValue);
  124. EF(GetRegValue(&hKey, NULL, TEXT("LogDefragSummary"), cRegValue, &dwRegValueSize) == ERROR_SUCCESS);
  125. LogOptions.bLogDefragSummary = (cRegValue[0] == TEXT('1')) ? TRUE : FALSE;
  126. #endif
  127. #ifndef DKMS
  128. dwRegValueSize = sizeof(cRegValue);
  129. EF(GetRegValue(&hKey, NULL, TEXT("LogFileInformation"), cRegValue, &dwRegValueSize) == ERROR_SUCCESS);
  130. LogOptions.bLogFileInformation = (cRegValue[0] == TEXT('1')) ? TRUE : FALSE;
  131. dwRegValueSize = sizeof(cRegValue);
  132. EF(GetRegValue(&hKey, NULL, TEXT("LogDiskInformation"), cRegValue, &dwRegValueSize) == ERROR_SUCCESS);
  133. LogOptions.bLogDiskInformation = (cRegValue[0] == TEXT('1')) ? TRUE : FALSE;
  134. dwRegValueSize = sizeof(cRegValue);
  135. EF(GetRegValue(&hKey, NULL, TEXT("LogPageFileInformation"), cRegValue, &dwRegValueSize) == ERROR_SUCCESS);
  136. LogOptions.bLogPageFileInformation = (cRegValue[0] == TEXT('1')) ? TRUE : FALSE;
  137. dwRegValueSize = sizeof(cRegValue);
  138. EF(GetRegValue(&hKey, NULL, TEXT("LogDirectoryInformation"), cRegValue, &dwRegValueSize) == ERROR_SUCCESS);
  139. LogOptions.bLogDirectoryInformation = (cRegValue[0] == TEXT('1')) ? TRUE : FALSE;
  140. dwRegValueSize = sizeof(cRegValue);
  141. EF(GetRegValue(&hKey, NULL, TEXT("LogMFTInformation"), cRegValue, &dwRegValueSize) == ERROR_SUCCESS);
  142. LogOptions.bLogMFTInformation = (cRegValue[0] == TEXT('1')) ? TRUE : FALSE;
  143. #endif
  144. }
  145. __finally {
  146. //0.0E00 Close the registry.
  147. if(hKey){
  148. EF(RegCloseKey(hKey)==ERROR_SUCCESS);
  149. }
  150. }
  151. return TRUE;
  152. }
  153. /*****************************************************************************************************************
  154. COPYRIGHT 2001 Microsoft Corporation and Executive Software International, Inc.
  155. ROUTINE DESCRIPTION:
  156. This routine shuts down the logging and closes all handles.
  157. INPUT + OUTPUT:
  158. None.
  159. GLOBALS:
  160. hEventSource - The handle to the EventLog.
  161. RETURN:
  162. TRUE - Success
  163. FALSE - Fatal Error
  164. */
  165. BOOL CleanupLogging(
  166. )
  167. {
  168. //0.0E00 Close the Event Log (DeregisterEventSource)
  169. EF(DeregisterEventSource(hEventSource));
  170. return TRUE;
  171. }
  172. /*****************************************************************************************************************
  173. COPYRIGHT 2001 Microsoft Corporation and Executive Software International, Inc.
  174. ROUTINE DESCRIPTION:
  175. This module logs an event to the event log.
  176. INPUT + OUTPUT:
  177. EventID - DWORD ID for which message to log.
  178. cMsg - A string to append to the end of the standard message text.
  179. GLOBALS:
  180. LogOptions - Contains the options for what sort of messages to log.
  181. hEventSource - The handle to the EventLog.
  182. RETURN:
  183. TRUE - Success
  184. FALSE - Fatal Error
  185. */
  186. BOOL LogEvent(
  187. DWORD dwEventID,
  188. PTCHAR cMsg
  189. )
  190. {
  191. BOOL bLogThisEvent = FALSE;
  192. LPCTSTR cStrings[1];
  193. //0.0E00 Check whether or not to log this dwEventID from the LogOptions structure.
  194. switch(dwEventID){
  195. #ifndef DKMS
  196. case MSG_CONTROL_START:
  197. case MSG_CONTROL_CLOSE:
  198. if(LogOptions.bLogServiceStartStop){
  199. bLogThisEvent = TRUE;
  200. }
  201. break;
  202. #endif
  203. case MSG_ENGINE_START:
  204. case MSG_ENGINE_CLOSE:
  205. if(LogOptions.bLogDefragStartStop){
  206. bLogThisEvent = TRUE;
  207. }
  208. break;
  209. case MSG_ENGINE_DEFRAGMENT:
  210. if(LogOptions.bLogFilesDefragged){
  211. bLogThisEvent = TRUE;
  212. }
  213. break;
  214. case MSG_ENGINE_FREE_SPACE:
  215. if(LogOptions.bLogFilesMoved){
  216. bLogThisEvent = TRUE;
  217. }
  218. break;
  219. #ifdef DKMS
  220. case MSG_DEFRAG_SUMMARY:
  221. if(LogOptions.bLogDefragSummary){
  222. bLogThisEvent = TRUE;
  223. }
  224. break;
  225. #endif
  226. #ifndef DKMS
  227. case MSG_DISK_INFO:
  228. if(LogOptions.bLogDiskInformation){
  229. bLogThisEvent = TRUE;
  230. }
  231. break;
  232. case MSG_FILE_INFO:
  233. if(LogOptions.bLogFileInformation){
  234. bLogThisEvent = TRUE;
  235. }
  236. break;
  237. case MSG_PAGE_FILE_INFO:
  238. if(LogOptions.bLogPageFileInformation){
  239. bLogThisEvent = TRUE;
  240. }
  241. break;
  242. case MSG_DIRECTORIS_INFO:
  243. if(LogOptions.bLogDirectoryInformation){
  244. bLogThisEvent = TRUE;
  245. }
  246. break;
  247. case MSG__MFT_INFO:
  248. if(LogOptions.bLogMFTInformation){
  249. bLogThisEvent = TRUE;
  250. }
  251. break;
  252. #endif
  253. //Log all the following events.
  254. case MSG_CONTROL_EXCLUDE:
  255. case MSG_CONTROL_SCHEDULE:
  256. case MSG_CONTROL_ERROR:
  257. case MSG_ENGINE_ERROR:
  258. bLogThisEvent = TRUE;
  259. break;
  260. }
  261. //0.0E00 If this event should not be logged, don't log it.
  262. if(!bLogThisEvent){
  263. return TRUE;
  264. }
  265. //0.0E00 If we log this dwEventID - log it. (ReportEvent)
  266. cStrings[0] = cMsg;
  267. //0.0E00 Place the event in the event log
  268. ReportEvent(hEventSource, // handle of event source
  269. EVENTLOG_INFORMATION_TYPE, // event type
  270. 0, // event category
  271. dwEventID, // event ID
  272. NULL, // current user's SID
  273. 1, // strings in cStrings
  274. 0, // no bytes of raw data
  275. cStrings, // array of error strings
  276. NULL); // no raw data
  277. return TRUE;
  278. }
  279. #endif //NOEVTLOG