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.

104 lines
3.1 KiB

  1. //#pragma title( "TEvent.cpp - Log events" )
  2. /*
  3. Copyright (c) 1995-1998, Mission Critical Software, Inc. All rights reserved.
  4. ===============================================================================
  5. Module - TAudit.cpp
  6. System - EnterpriseAdministrator
  7. Author - Rich Denham
  8. Created - 1995-11-10
  9. Description - TErrorEventLog class
  10. Updates -
  11. ===============================================================================
  12. */
  13. #include <stdio.h>
  14. #include <windows.h>
  15. #include "Common.hpp"
  16. #include "Err.hpp"
  17. #include "UString.hpp"
  18. #include "TEvent.hpp"
  19. BOOL
  20. TErrorEventLog::LogOpen(
  21. WCHAR const * svcName ,// in -service name
  22. int mode ,// in -0=overwrite, 1=append
  23. int level // in -minimum level to log
  24. )
  25. {
  26. hEventSource = RegisterEventSourceW( NULL, svcName );
  27. if ( hEventSource == NULL )
  28. lastError = GetLastError();
  29. return hEventSource != NULL;
  30. }
  31. void
  32. TErrorEventLog::LogWrite(
  33. WCHAR const * msg
  34. )
  35. {
  36. BOOL rcBool;
  37. DWORD rcErr;
  38. static const WORD levelTranslate[] = {EVENTLOG_INFORMATION_TYPE,
  39. EVENTLOG_WARNING_TYPE,
  40. EVENTLOG_ERROR_TYPE,
  41. EVENTLOG_ERROR_TYPE,
  42. EVENTLOG_ERROR_TYPE,
  43. EVENTLOG_ERROR_TYPE,
  44. EVENTLOG_ERROR_TYPE,
  45. EVENTLOG_ERROR_TYPE};
  46. SID * pSid = NULL;
  47. HANDLE hToken = NULL;
  48. TOKEN_USER tUser[10];
  49. ULONG len;
  50. if ( OpenProcessToken(GetCurrentProcess(),TOKEN_READ,&hToken) )
  51. {
  52. if ( GetTokenInformation(hToken,TokenUser,tUser,10*(sizeof TOKEN_USER),&len) )
  53. {
  54. pSid = (SID*)tUser[0].User.Sid;
  55. }
  56. else
  57. {
  58. rcErr = GetLastError();
  59. }
  60. CloseHandle(hToken);
  61. }
  62. else
  63. {
  64. rcErr = GetLastError();
  65. }
  66. // TODO: setup event category
  67. // TODO: log events in Unicode
  68. rcBool = ReportEventW( hEventSource, // handle of event source
  69. levelTranslate[level], // event type
  70. 0, // event category
  71. // CAT_AGENT, // event category
  72. DCT_MSG_GENERIC_S, // event ID
  73. pSid, // current user's SID
  74. 1, // strings in lpszStrings
  75. 0, // no bytes of raw data
  76. &msg, // array of error strings
  77. NULL ); // no raw data
  78. if ( !rcBool )
  79. {
  80. rcErr = GetLastError();
  81. }
  82. }
  83. void
  84. TErrorEventLog::LogClose()
  85. {
  86. if ( hEventSource != NULL )
  87. {
  88. DeregisterEventSource( hEventSource );
  89. hEventSource = NULL;
  90. }
  91. };
  92. // TEvent.cpp - end of file