comparison common/logging.c @ 616:0172740f5c6e

Include user information in windows event log messages
author Andre Heinecke <andre.heinecke@intevation.de>
date Thu, 19 Jun 2014 12:06:47 +0200
parents 2a4f7364ab81
children bc02ee484067
comparison
equal deleted inserted replaced
615:2a4f7364ab81 616:0172740f5c6e
23 23
24 #ifdef WIN32 24 #ifdef WIN32
25 static void 25 static void
26 win_log(const char *format, va_list ap, bool error) 26 win_log(const char *format, va_list ap, bool error)
27 { 27 {
28 HANDLE log_src = NULL; 28 HANDLE log_src = NULL,
29 process_token = NULL;
29 wchar_t *wmsg = NULL; 30 wchar_t *wmsg = NULL;
30 BOOL failure = TRUE; 31 BOOL failure = TRUE;
31 WORD type = 0, 32 WORD type = 0,
32 category = 0; 33 category = 0;
33 char buffer[MAX_LOG+1]; 34 char buffer[MAX_LOG+1];
35 PTOKEN_USER user_struct = NULL;
36 PSID user_sid = NULL;
34 37
35 vsnprintf (buffer, MAX_LOG, format, ap); 38 vsnprintf (buffer, MAX_LOG, format, ap);
36 buffer[MAX_LOG] = '\0'; 39 buffer[MAX_LOG] = '\0';
37 40
38 log_src = RegisterEventSourceA (NULL, LOG_NAME); 41 log_src = RegisterEventSourceA (NULL, LOG_NAME);
57 { 60 {
58 ERRORPRINTF ("Failed to convert log message to utf-16"); 61 ERRORPRINTF ("Failed to convert log message to utf-16");
59 goto done; 62 goto done;
60 } 63 }
61 64
65 /* Get the current user sid for logging */
66
67 OpenProcessToken (GetCurrentProcess(), TOKEN_READ, &process_token);
68 if (process_token)
69 {
70 DWORD size = 0;
71
72 // check how much space is needed
73 GetTokenInformation (process_token, TokenUser, NULL, 0, &size);
74 if (ERROR_INSUFFICIENT_BUFFER == GetLastError())
75 {
76 user_struct = xmalloc (size);
77 GetTokenInformation (process_token, TokenUser, user_struct, size, &size);
78 user_sid = user_struct->User.Sid;
79 }
80 }
81
82
62 failure = ReportEventW (log_src, 83 failure = ReportEventW (log_src,
63 type, 84 type,
64 category, 85 category,
65 0, 86 0,
66 NULL, 87 user_sid,
67 1, 88 1,
68 0, 89 0,
69 (const WCHAR **) &wmsg, 90 (const WCHAR **) &wmsg,
70 NULL); 91 NULL);
71 if (failure) 92 if (failure)
72 { 93 {
73 PRINTLASTERROR ("Failed to report event."); 94 PRINTLASTERROR ("Failed to report event.");
74 } 95 }
75 96
76 done: 97 done:
98 if (process_token)
99 {
100 CloseHandle(process_token);
101 }
102 xfree (user_struct);
77 xfree (wmsg); 103 xfree (wmsg);
78 104
79 if (!DeregisterEventSource (log_src)) 105 if (!DeregisterEventSource (log_src))
80 { 106 {
81 PRINTLASTERROR ("Failed to close log source."); 107 PRINTLASTERROR ("Failed to close log source.");

http://wald.intevation.org/projects/trustbridge/