home *** CD-ROM | disk | FTP | other *** search
/ Mundo do CD-ROM 118 / cdrom118.iso / internet / webaroo / WebarooSetup.exe / Webaroo.msi / _A0DEB44B94924E89917E71AA90C5F226 / win32evtlogutil.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2005-12-23  |  5.0 KB  |  136 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.4)
  3.  
  4. '''Event Log Utilities - helper for win32evtlog.pyd
  5. '''
  6. import win32api
  7. import win32con
  8. import winerror
  9. import win32evtlog
  10. import string
  11. error = win32api.error
  12. langid = win32api.MAKELANGID(win32con.LANG_NEUTRAL, win32con.SUBLANG_NEUTRAL)
  13.  
  14. def AddSourceToRegistry(appName, msgDLL = None, eventLogType = 'Application', eventLogFlags = None):
  15.     '''Add a source of messages to the event log.
  16.  
  17.     Allows Python program to register a custom source of messages in the
  18.     registry.  You must also provide the DLL name that has the message table, so the
  19.     full message text appears in the event log.
  20.  
  21.     Note that the win32evtlog.pyd file has a number of string entries with just "%1"
  22.     built in, so many Python programs can simply use this DLL.  Disadvantages are that
  23.     you do not get language translation, and the full text is stored in the event log,
  24.     blowing the size of the log up.
  25.     '''
  26.     if msgDLL is None:
  27.         msgDLL = win32evtlog.__file__
  28.     
  29.     hkey = win32api.RegCreateKey(win32con.HKEY_LOCAL_MACHINE, 'SYSTEM\\CurrentControlSet\\Services\\EventLog\\%s\\%s' % (eventLogType, appName))
  30.     win32api.RegSetValueEx(hkey, 'EventMessageFile', 0, win32con.REG_EXPAND_SZ, msgDLL)
  31.     if eventLogFlags is None:
  32.         eventLogFlags = win32evtlog.EVENTLOG_ERROR_TYPE | win32evtlog.EVENTLOG_WARNING_TYPE | win32evtlog.EVENTLOG_INFORMATION_TYPE
  33.     
  34.     win32api.RegSetValueEx(hkey, 'TypesSupported', 0, win32con.REG_DWORD, eventLogFlags)
  35.     win32api.RegCloseKey(hkey)
  36.  
  37.  
  38. def RemoveSourceFromRegistry(appName, eventLogType = 'Application'):
  39.     '''Removes a source of messages from the event log.
  40.     '''
  41.     
  42.     try:
  43.         win32api.RegDeleteKey(win32con.HKEY_LOCAL_MACHINE, 'SYSTEM\\CurrentControlSet\\Services\\EventLog\\%s\\%s' % (eventLogType, appName))
  44.     except win32api.error:
  45.         (hr, fn, desc) = None
  46.         if hr != winerror.ERROR_FILE_NOT_FOUND:
  47.             raise 
  48.         
  49.     except:
  50.         hr != winerror.ERROR_FILE_NOT_FOUND
  51.  
  52.  
  53.  
  54. def ReportEvent(appName, eventID, eventCategory = 0, eventType = win32evtlog.EVENTLOG_ERROR_TYPE, strings = None, data = None, sid = None):
  55.     '''Report an event for a previously added event source.
  56.     '''
  57.     hAppLog = win32evtlog.RegisterEventSource(None, appName)
  58.     win32evtlog.ReportEvent(hAppLog, eventType, eventCategory, eventID, sid, strings, data)
  59.     win32evtlog.DeregisterEventSource(hAppLog)
  60.  
  61.  
  62. def FormatMessage(eventLogRecord, logType = 'Application'):
  63.     '''Given a tuple from ReadEventLog, and optionally where the event
  64.     record came from, load the message, and process message inserts.
  65.  
  66.     Note that this function may raise win32api.error.  See also the
  67.     function SafeFormatMessage which will return None if the message can
  68.     not be processed.
  69.     '''
  70.     keyName = 'SYSTEM\\CurrentControlSet\\Services\\EventLog\\%s\\%s' % (logType, eventLogRecord.SourceName)
  71.     handle = win32api.RegOpenKey(win32con.HKEY_LOCAL_MACHINE, keyName)
  72.     
  73.     try:
  74.         dllNames = win32api.RegQueryValueEx(handle, 'EventMessageFile')[0].split(';')
  75.         data = None
  76.         for dllName in dllNames:
  77.             
  78.             try:
  79.                 dllName = win32api.ExpandEnvironmentStrings(dllName)
  80.                 dllHandle = win32api.LoadLibraryEx(dllName, 0, win32con.DONT_RESOLVE_DLL_REFERENCES)
  81.                 
  82.                 try:
  83.                     data = win32api.FormatMessageW(win32con.FORMAT_MESSAGE_FROM_HMODULE, dllHandle, eventLogRecord.EventID, langid, eventLogRecord.StringInserts)
  84.                 finally:
  85.                     win32api.FreeLibrary(dllHandle)
  86.  
  87.             except win32api.error:
  88.                 pass
  89.  
  90.             if data is not None:
  91.                 break
  92.                 continue
  93.     finally:
  94.         win32api.RegCloseKey(handle)
  95.  
  96.     if not data:
  97.         pass
  98.     return u''
  99.  
  100.  
  101. def SafeFormatMessage(eventLogRecord, logType = None):
  102.     '''As for FormatMessage, except returns an error message if
  103.     the message can not be processed.
  104.     '''
  105.     if logType is None:
  106.         logType = 'Application'
  107.     
  108.     
  109.     try:
  110.         return FormatMessage(eventLogRecord, logType)
  111.     except win32api.error:
  112.         if eventLogRecord.StringInserts is None:
  113.             desc = ''
  114.         else:
  115.             desc = u', '.join(eventLogRecord.StringInserts)
  116.         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)
  117.  
  118.  
  119.  
  120. def FeedEventLogRecords(feeder, machineName = None, logName = 'Application', readFlags = None):
  121.     if readFlags is None:
  122.         readFlags = win32evtlog.EVENTLOG_BACKWARDS_READ | win32evtlog.EVENTLOG_SEQUENTIAL_READ
  123.     
  124.     h = win32evtlog.OpenEventLog(machineName, logName)
  125.     
  126.     try:
  127.         while None:
  128.             objects = win32evtlog.ReadEventLog(h, readFlags, 0)
  129.             if not objects:
  130.                 break
  131.             
  132.     finally:
  133.         win32evtlog.CloseEventLog(h)
  134.  
  135.  
  136.