PyXR

c:\python24\lib\site-packages\win32\lib \ win32evtlogutil.py



0001 """Event Log Utilities - helper for win32evtlog.pyd
0002 """
0003 
0004 import win32api, win32con, winerror, win32evtlog, string
0005 
0006 error = win32api.error # The error the evtlog module raises.
0007 
0008 langid = win32api.MAKELANGID(win32con.LANG_NEUTRAL, win32con.SUBLANG_NEUTRAL)
0009 
0010 def AddSourceToRegistry(appName, msgDLL = None, eventLogType = "Application", eventLogFlags = None):
0011     """Add a source of messages to the event log.
0012 
0013     Allows Python program to register a custom source of messages in the
0014     registry.  You must also provide the DLL name that has the message table, so the
0015     full message text appears in the event log.
0016 
0017     Note that the win32evtlog.pyd file has a number of string entries with just "%1"
0018     built in, so many Python programs can simply use this DLL.  Disadvantages are that
0019     you do not get language translation, and the full text is stored in the event log,
0020     blowing the size of the log up.
0021     """
0022 
0023     # When an application uses the RegisterEventSource or OpenEventLog
0024     # function to get a handle of an event log, the event loggging service
0025     # searches for the specified source name in the registry. You can add a
0026     # new source name to the registry by opening a new registry subkey
0027     # under the Application key and adding registry values to the new
0028     # subkey.
0029 
0030     if msgDLL is None:
0031         msgDLL = win32evtlog.__file__
0032 
0033     # Create a new key for our application
0034     hkey = win32api.RegCreateKey(win32con.HKEY_LOCAL_MACHINE, \
0035         "SYSTEM\\CurrentControlSet\\Services\\EventLog\\%s\\%s" % (eventLogType, appName))
0036 
0037     # Add the Event-ID message-file name to the subkey.
0038     win32api.RegSetValueEx(hkey,
0039         "EventMessageFile",    # value name \
0040         0,                     # reserved \
0041         win32con.REG_EXPAND_SZ,# value type \
0042         msgDLL)
0043 
0044     # Set the supported types flags and add it to the subkey.
0045     if eventLogFlags is None:
0046         eventLogFlags = win32evtlog.EVENTLOG_ERROR_TYPE | win32evtlog.EVENTLOG_WARNING_TYPE | win32evtlog.EVENTLOG_INFORMATION_TYPE
0047     win32api.RegSetValueEx(hkey, # subkey handle \
0048         "TypesSupported",        # value name \
0049         0,                       # reserved \
0050         win32con.REG_DWORD,      # value type \
0051         eventLogFlags)
0052     win32api.RegCloseKey(hkey)
0053 
0054 def RemoveSourceFromRegistry(appName, eventLogType = "Application"):
0055     """Removes a source of messages from the event log.
0056     """
0057 
0058     # Delete our key
0059     try:
0060         win32api.RegDeleteKey(win32con.HKEY_LOCAL_MACHINE, \
0061                      "SYSTEM\\CurrentControlSet\\Services\\EventLog\\%s\\%s" % (eventLogType, appName))
0062     except win32api.error, (hr, fn, desc):
0063         if hr != winerror.ERROR_FILE_NOT_FOUND:
0064             raise
0065 
0066 
0067 def ReportEvent(appName, eventID, eventCategory = 0, eventType=win32evtlog.EVENTLOG_ERROR_TYPE, strings = None, data = None, sid=None):
0068     """Report an event for a previously added event source.
0069     """
0070     # Get a handle to the Application event log
0071     hAppLog = win32evtlog.RegisterEventSource(None, appName)
0072 
0073     # Now report the event, which will add this event to the event log */
0074     win32evtlog.ReportEvent(hAppLog, # event-log handle \
0075         eventType,
0076         eventCategory,
0077         eventID,
0078         sid,
0079         strings,
0080         data)
0081 
0082     win32evtlog.DeregisterEventSource(hAppLog);
0083 
0084 def FormatMessage( eventLogRecord, logType="Application" ):
0085     """Given a tuple from ReadEventLog, and optionally where the event
0086     record came from, load the message, and process message inserts.
0087 
0088     Note that this function may raise win32api.error.  See also the
0089     function SafeFormatMessage which will return None if the message can
0090     not be processed.
0091     """
0092 
0093     # From the event log source name, we know the name of the registry
0094     # key to look under for the name of the message DLL that contains
0095     # the messages we need to extract with FormatMessage. So first get
0096     # the event log source name...
0097     keyName = "SYSTEM\\CurrentControlSet\\Services\\EventLog\\%s\\%s" % (logType, eventLogRecord.SourceName)
0098 
0099     # Now open this key and get the EventMessageFile value, which is
0100     # the name of the message DLL.
0101     handle = win32api.RegOpenKey(win32con.HKEY_LOCAL_MACHINE, keyName)
0102     try:
0103         dllNames = win32api.RegQueryValueEx(handle, "EventMessageFile")[0].split(";")
0104         # Win2k etc appear to allow multiple DLL names
0105         data = None
0106         for dllName in dllNames:
0107             try:
0108                 # Expand environment variable strings in the message DLL path name,
0109                 # in case any are there.
0110                 dllName = win32api.ExpandEnvironmentStrings(dllName)
0111 
0112                 dllHandle = win32api.LoadLibraryEx(dllName, 0, win32con.DONT_RESOLVE_DLL_REFERENCES)
0113                 try:
0114                     data = win32api.FormatMessageW(win32con.FORMAT_MESSAGE_FROM_HMODULE,
0115                                     dllHandle, eventLogRecord.EventID, langid, eventLogRecord.StringInserts)
0116                 finally:
0117                     win32api.FreeLibrary(dllHandle)
0118             except win32api.error:
0119                 pass # Not in this DLL - try the next
0120             if data is not None:
0121                 break
0122     finally:
0123         win32api.RegCloseKey(handle)
0124     return data or u'' # Don't want "None" ever being returned.
0125 
0126 def SafeFormatMessage( eventLogRecord, logType=None ):
0127     """As for FormatMessage, except returns an error message if
0128     the message can not be processed.
0129     """
0130     if logType is None: logType = "Application"
0131     try:
0132         return FormatMessage(eventLogRecord, logType)
0133     except win32api.error:
0134         if eventLogRecord.StringInserts is None:
0135             desc = ""
0136         else:
0137             desc = u", ".join(eventLogRecord.StringInserts)
0138         return u"<The description for Event ID ( %d ) in Source ( %r ) could not be found. It contains the following insertion string(s):%r.>" % (winerror.HRESULT_CODE(eventLogRecord.EventID), eventLogRecord.SourceName, desc)
0139 
0140 def FeedEventLogRecords(feeder, machineName = None, logName = "Application", readFlags = None):
0141     if readFlags is None:
0142         readFlags = win32evtlog.EVENTLOG_BACKWARDS_READ|win32evtlog.EVENTLOG_SEQUENTIAL_READ
0143 
0144     h=win32evtlog.OpenEventLog(machineName, logName)
0145     try:
0146         while 1:
0147             objects = win32evtlog.ReadEventLog(h, readFlags, 0)
0148             if not objects:
0149                 break
0150             map(lambda item, feeder = feeder: apply(feeder, (item,)), objects)
0151     finally:
0152         win32evtlog.CloseEventLog(h)
0153 

Generated by PyXR 0.9.4
SourceForge.net Logo