1 ////////////////////////////////////////////////////////////////////
4 // E V E N T L O G G I N G F U N C T I O N S
7 ////////////////////////////////////////////////////////////////////
13 #include <WINNT/afsreg.h>
14 #include "afsd_eventlog.h"
16 static CHAR szKeyName[] = AFSREG_APPLOG_SUBKEY "\\" AFSREG_CLT_SVC_NAME;
18 static BOOL GetServicePath(LPTSTR lpPathBuf, PDWORD pdwPathBufSize);
19 static BOOL AddEventSource(void);
22 GetServicePath(LPTSTR lpPathBuf, PDWORD pdwPathBufSize)
31 if ( RegOpenKeyEx( HKEY_LOCAL_MACHINE, AFSREG_SVR_SVC_SUBKEY, 0, KEY_QUERY_VALUE, &hKey ) )
37 // prepare user's buffer and read into it
38 dwData = *pdwPathBufSize;
39 memset(lpPathBuf, '\0', dwData);
41 hKey, // handle to key
42 "ImagePath", // value name
45 (LPBYTE) lpPathBuf, // data buffer
46 &dwData)) // size of data buffer
52 *pdwPathBufSize = dwData;
63 // Ensure name for message file is in proper location in Registry.
69 UCHAR szBuf[MAX_PATH];
70 DWORD dwData, dwDisposition;
74 // Let's see if key already exists as a subkey under the
75 // Application key in the EventLog registry key. If not,
77 if ( RegOpenKeyEx( HKEY_LOCAL_MACHINE, szKeyName, 0,
78 KEY_QUERY_VALUE, &hKey ) )
81 if ( RegCreateKeyEx(HKEY_LOCAL_MACHINE, szKeyName, 0,
82 NULL, REG_OPTION_NON_VOLATILE,
83 KEY_ALL_ACCESS, NULL, &hKey,
90 // Set the name of the message file
91 // Get "ImagePath" from TransarcAFSDaemon service
92 memset(szBuf, '\0', MAX_PATH);
94 GetServicePath(szBuf, &dwData);
96 // Add the name to the EventMessageFile subkey.
98 hKey, // subkey handle
99 AFSREG_SVR_APPLOG_MSGFILE_VALUE, // value name
101 REG_EXPAND_SZ, // value type
102 (LPBYTE) szBuf, // pointer to value data
103 strlen(szBuf) + 1)) // length of value data
109 // Set the supported event types in the TypesSupported subkey.
110 dwData = EVENTLOG_ERROR_TYPE | EVENTLOG_WARNING_TYPE |
111 EVENTLOG_INFORMATION_TYPE;
114 hKey, // subkey handle
115 AFSREG_SVR_APPLOG_MSGTYPE_VALUE, // value name
117 REG_DWORD, // value type
118 (LPBYTE) &dwData, // pointer to value data
119 sizeof(DWORD))) // length of value data
128 // key was opened - read it
129 memset(szBuf, '\0', MAX_PATH);
131 if ( RegQueryValueEx(
132 hKey, // handle to key
133 AFSREG_SVR_APPLOG_MSGFILE_VALUE, // value name
136 (LPBYTE) szBuf, // data buffer
137 &dwData)) // size of data buffer
152 // Log an event with a formatted system message as the (only) substitution
153 // string, from the given message ID.
155 LogEventMessage(WORD wEventType, DWORD dwEventID, DWORD dwMessageID)
159 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM
160 | FORMAT_MESSAGE_ALLOCATE_BUFFER,
161 NULL, dwMessageID, 0, (LPTSTR)&msgBuf, 0, NULL);
162 LogEvent(wEventType, dwEventID, msgBuf, NULL);
167 // Use the ReportEvent API to write an entry to the system event log.
169 #define MAXSTRINGARGS 100
171 LogEvent(WORD wEventType, DWORD dwEventID, LPTSTR lpString, ...)
175 LPTSTR lpStrings[MAXSTRINGARGS];
178 // Ensure that our event source is properly initialized.
179 if (!AddEventSource())
182 // Get a handle to the event log.
183 hEventSource = RegisterEventSource(NULL, AFSREG_CLT_SVC_PARAM_KEY);
184 if (hEventSource == NULL)
187 // Construct the array of substitution strings.
188 va_start(listStrings, lpString);
189 for (wNumStrings = 0;
190 lpString != NULL && wNumStrings < MAXSTRINGARGS;
193 lpStrings[wNumStrings] = lpString;
194 // Advance to the next argument.
195 lpString = va_arg(listStrings, LPTSTR);
199 // Make sure we were not given too many args.
200 if (wNumStrings >= MAXSTRINGARGS)
204 ReportEvent(hEventSource, // handle of event source
205 wEventType, // event type
207 dwEventID, // event ID
208 NULL, // current user's SID
209 wNumStrings, // strings in lpszStrings
210 0, // no bytes of raw data
211 lpStrings, // array of error strings
212 NULL); // no raw data
214 DeregisterEventSource(hEventSource);