Main Page | Class Hierarchy | Class List | File List | Class Members | File Members

EventLog.h

Go to the documentation of this file.
00001 /*  EventLog.h - NT Event log API wrapper
00002     Copyright (C) 2001-2004 Mark Weaver
00003     Written by Mark Weaver <mark@npsl.co.uk>
00004 
00005     Part of the Open-Win32 library.
00006     This library is free software; you can redistribute it and/or
00007     modify it under the terms of the GNU Library General Public
00008     License as published by the Free Software Foundation; either
00009     version 2 of the License, or (at your option) any later version.
00010 
00011     This library is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014     Library General Public License for more details.
00015 
00016     You should have received a copy of the GNU Library General Public
00017     License along with this library; if not, write to the
00018     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00019     Boston, MA  02111-1307, USA.
00020 */
00021 
00026 #ifndef OW32_EventLog_h
00027 #define OW32_EventLog_h
00028 
00029 #include <OW32/XHKEY.h>
00030 #include <tchar.h>
00031 #include <cstdio>
00032 
00033 namespace OW32
00034 {
00035 
00037 class OW32_LIB_EXPORT CEventLog
00038 {
00039 private:
00040     HANDLE m_hEventLog;
00041 
00042 public:
00044     CEventLog() : m_hEventLog(NULL) {}
00045 
00047     ~CEventLog() { Close(); }
00048 
00054     BOOL Open(LPCTSTR SourceName, LPCTSTR Computer = NULL)
00055     {
00056         m_hEventLog = RegisterEventSource(Computer, SourceName); // open the event log
00057         return (m_hEventLog != NULL); // oh dear, and no way to report the error!
00058     }
00059 
00062     void Close()
00063     {
00064         if (m_hEventLog != NULL) {
00065             DeregisterEventSource(m_hEventLog);
00066             m_hEventLog = NULL;
00067         }
00068     }
00069 
00081     BOOL ReportEvent(WORD wType,                // type of event (success, error, etc).
00082                      DWORD dwEventID,           // message table ID of event
00083                      WORD wCategory = 0,        // category, defaults to `None'
00084                      WORD wNumStrings = 0,      // number of strings to merge
00085                      LPCTSTR* lpStrings = NULL, // the strings
00086                      DWORD dwDataSize = 0,      // raw data size
00087                      LPVOID lpRawData = NULL,   // raw data buffer
00088                      PSID lpUserSID = NULL)     // current user's SID for logging user name
00089     {
00090         if (!m_hEventLog) // uhoh!
00091             return FALSE;
00092 
00093         return ::ReportEvent(m_hEventLog, wType, wCategory, dwEventID, lpUserSID, 
00094                            wNumStrings, dwDataSize, lpStrings, lpRawData);
00095     }
00096 
00105     BOOL ReportInformation(DWORD dwEventID, LPCTSTR str1=0, LPCTSTR str2=0, LPCTSTR str3=0, LPCTSTR str4=0)
00106     {
00107         LPCTSTR lpStrings[5] = {str1, str2, str3, str4, 0};
00108         int NumStrings=0;
00109         for ( ; lpStrings[NumStrings] != 0; NumStrings++)
00110             ;
00111         return ReportEvent((WORD)EVENTLOG_INFORMATION_TYPE, dwEventID, (WORD)0, (WORD)NumStrings, lpStrings);
00112     }
00113 
00122     BOOL ReportWarning(DWORD dwEventID, LPCTSTR str1=0, LPCTSTR str2=0, LPCTSTR str3=0, LPCTSTR str4=0)
00123     {
00124         LPCTSTR lpStrings[5] = {str1, str2, str3, str4, 0};
00125         int NumStrings=0;
00126         for ( ; lpStrings[NumStrings] != 0; NumStrings++)
00127             ;
00128         return ReportEvent((WORD)EVENTLOG_WARNING_TYPE, dwEventID, (WORD)0, (WORD)NumStrings, lpStrings);
00129     }
00130 
00139     BOOL ReportError(DWORD dwEventID, LPCTSTR str1=0, LPCTSTR str2=0, LPCTSTR str3=0, LPCTSTR str4=0)
00140     {
00141         LPCTSTR lpStrings[5] = {str1, str2, str3, str4, 0};
00142         int NumStrings=0;
00143         for ( ; lpStrings[NumStrings] != 0; NumStrings++)
00144             ;
00145         return ReportEvent((WORD)EVENTLOG_ERROR_TYPE, dwEventID, (WORD)0, (WORD)NumStrings, lpStrings);
00146     }
00147 
00153     BOOL ReportAPIError(DWORD dwEventID, DWORD dwErr, LPCTSTR str1=0, LPCTSTR str2=0, LPCTSTR str3=0)
00154     {
00155         LPTSTR lpMessage=NULL;
00156         if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, // flags
00157                       0, // source (if hmodule/string)
00158                       dwErr, // message id
00159                       0, // lang id
00160                       (LPTSTR)&lpMessage, // output buffer
00161                       0, // max char count
00162                       NULL) == 0) { // args
00163             TCHAR num[32];
00164             _sntprintf(num, sizeof(num)/sizeof(num[0]), _T("%d"), dwErr);
00165             return ReportError(dwEventID, num, str1, str2, str3);
00166         }
00167         BOOL bRet = ReportError(dwEventID, lpMessage, str1, str2, str3);
00168         LocalFree(lpMessage);
00169         return bRet;
00170     }
00171 
00183     static LONG RegisterMessageDLL(LPCTSTR SourceName, LPCTSTR MessageDLLPath=NULL, HMODULE hModule=NULL,
00184         DWORD TypesSupported = EVENTLOG_ERROR_TYPE | EVENTLOG_WARNING_TYPE | EVENTLOG_INFORMATION_TYPE)
00185     {
00186         TCHAR EventLogKeyName[MAX_PATH];
00187         _sntprintf(EventLogKeyName, MAX_PATH, _T("SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\%s"), 
00188             SourceName);
00189 
00190         // register the app as an event source
00191         XHKEY hk;
00192         LONG lErr;
00193         if ((lErr=RegCreateKey(HKEY_LOCAL_MACHINE,
00194                 EventLogKeyName,
00195                 &hk)) != ERROR_SUCCESS) {
00196             return lErr;
00197         }
00198 
00199         // set the name of the message file
00200         TCHAR path[MAX_PATH];
00201         if (!MessageDLLPath) {
00202             if (!GetModuleFileName(hModule, path, MAX_PATH))
00203                 return ::GetLastError();
00204             MessageDLLPath = path;
00205         }
00206         if ((lErr=RegSetValueEx(hk, _T("EventMessageFile"), 0, REG_EXPAND_SZ, (LPBYTE)MessageDLLPath, (DWORD)(_tcslen(MessageDLLPath)+1)*sizeof(TCHAR))) !=
00207             ERROR_SUCCESS) {
00208             return lErr;
00209         }
00210 
00211         // set the supported event types in the TypesSupported value
00212         if ((lErr=RegSetValueEx(hk, _T("TypesSupported"), 0, REG_DWORD, (LPBYTE)&TypesSupported, sizeof(TypesSupported))) !=
00213             ERROR_SUCCESS) {
00214             return lErr;
00215         }
00216         return ERROR_SUCCESS;
00217     }
00218 
00223     static LONG UnregisterMessageDLL(LPCTSTR SourceName)
00224     {
00225         // work out the name of the event log key
00226         TCHAR EventLogKeyName[MAX_PATH];
00227         _sntprintf(EventLogKeyName, MAX_PATH, _T("SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\%s"), SourceName);
00228 
00229         // delete registry key for event source
00230         LONG lErr;
00231         if ((lErr=RegDeleteKey(HKEY_LOCAL_MACHINE, EventLogKeyName)) != ERROR_SUCCESS) {
00232             return lErr;
00233         }
00234         return ERROR_SUCCESS;
00235     }
00236 };
00237 
00238 } // namespace OW32
00239 
00240 #endif // OW32_EventLog_h

Generated on Sun Jun 5 01:29:17 2005 for OW32 by  doxygen 1.3.9.1