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.

106 lines
3.3 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. if (hEventSource != NULL)
  27. DeregisterEventSource(hEventSource);
  28. hEventSource = RegisterEventSourceW( NULL, svcName );
  29. if ( hEventSource == NULL )
  30. lastError = GetLastError();
  31. return hEventSource != NULL;
  32. }
  33. void
  34. TErrorEventLog::LogWrite(
  35. WCHAR const * msg
  36. )
  37. {
  38. BOOL rcBool;
  39. DWORD rcErr;
  40. static const WORD levelTranslate[] = {EVENTLOG_INFORMATION_TYPE,
  41. EVENTLOG_WARNING_TYPE,
  42. EVENTLOG_ERROR_TYPE,
  43. EVENTLOG_ERROR_TYPE,
  44. EVENTLOG_ERROR_TYPE,
  45. EVENTLOG_ERROR_TYPE,
  46. EVENTLOG_ERROR_TYPE,
  47. EVENTLOG_ERROR_TYPE};
  48. SID * pSid = NULL;
  49. HANDLE hToken = NULL;
  50. TOKEN_USER tUser[10];
  51. ULONG len;
  52. if ( OpenProcessToken(GetCurrentProcess(),TOKEN_READ,&hToken) )
  53. {
  54. if ( GetTokenInformation(hToken,TokenUser,tUser,10*(sizeof TOKEN_USER),&len) )
  55. {
  56. pSid = (SID*)tUser[0].User.Sid;
  57. }
  58. else
  59. {
  60. rcErr = GetLastError();
  61. }
  62. CloseHandle(hToken);
  63. }
  64. else
  65. {
  66. rcErr = GetLastError();
  67. }
  68. // TODO: setup event category
  69. // TODO: log events in Unicode
  70. rcBool = ReportEventW( hEventSource, // handle of event source
  71. levelTranslate[level], // event type
  72. 0, // event category
  73. // CAT_AGENT, // event category
  74. DCT_MSG_GENERIC_S, // event ID
  75. pSid, // current user's SID
  76. 1, // strings in lpszStrings
  77. 0, // no bytes of raw data
  78. &msg, // array of error strings
  79. NULL ); // no raw data
  80. if ( !rcBool )
  81. {
  82. rcErr = GetLastError();
  83. }
  84. }
  85. void
  86. TErrorEventLog::LogClose()
  87. {
  88. if ( hEventSource != NULL )
  89. {
  90. DeregisterEventSource( hEventSource );
  91. hEventSource = NULL;
  92. }
  93. };
  94. // TEvent.cpp - end of file