home *** CD-ROM | disk | FTP | other *** search
/ PC World 2002 May / PCWorld_2002-05_cd.bin / Software / TemaCD / activepython / ActivePython-2.1.1.msi / Python21_win32_Demos_eventLogDemo.py < prev    next >
Encoding:
Python Source  |  2001-07-26  |  2.8 KB  |  96 lines

  1. import win32evtlog, traceback
  2. import win32api, win32con
  3. import win32security # To translate NT Sids to account names.
  4.  
  5. from win32evtlogutil import *
  6.  
  7. def ReadLog(computer, logType="Application", dumpEachRecord = 0):
  8.     # read the entire log back.
  9.     h=win32evtlog.OpenEventLog(computer, logType)
  10.     numRecords = win32evtlog.GetNumberOfEventLogRecords(h)
  11. #    print "There are %d records" % numRecords
  12.     
  13.     num=0
  14.     while 1:
  15.         objects = win32evtlog.ReadEventLog(h, win32evtlog.EVENTLOG_BACKWARDS_READ|win32evtlog.EVENTLOG_SEQUENTIAL_READ, 0)
  16.         if not objects:
  17.             break
  18.         for object in objects:
  19.             # get it for testing purposes, but dont print it.
  20.             msg = str(SafeFormatMessage(object, logType))
  21.             if object.Sid is not None:
  22.                 try:
  23.                     domain, user, typ = win32security.LookupAccountSid(computer, object.Sid)
  24.                     sidDesc = "%s/%s" % (domain, user)
  25.                 except win32security.error:
  26.                     sidDesc = str(object.Sid)
  27.                 user_desc = "Event associated with user %s" % (sidDesc,)
  28.             else:
  29.                 user_desc = None
  30.             if dumpEachRecord:
  31.                 if user_desc:
  32.                     print user_desc
  33.                 print msg
  34.         num = num + len(objects)
  35.  
  36.     if numRecords == num:
  37.         print "Successfully read all records"
  38.     else:
  39.         print "Couldn't get all records - reported %d, but found %d" % (numRecords, num)
  40.         print "(Note that some other app may have written records while we were running!)"
  41.     win32evtlog.CloseEventLog(h)
  42.  
  43. def Usage():
  44.     print "Writes an event to the event log."
  45.     print "-w : Dont write any test records."
  46.     print "-r : Dont read the event log"
  47.     print "-c : computerName : Process the log on the specified computer"
  48.     print "-v : Verbose"
  49.     print "-t : LogType - Use the specified log - default = 'Application'"
  50.  
  51.  
  52. def test():
  53.     # check if running on Windows NT, if not, display notice and terminate
  54.     if win32api.GetVersion() & 0x80000000:
  55.         print "This sample only runs on NT"
  56.         return
  57.         
  58.     import sys, getopt
  59.     opts, args = getopt.getopt(sys.argv[1:], "rwh?c:t:v")
  60.     computer = None
  61.     do_read = do_write = 1
  62.  
  63.     logType = "Application"
  64.     verbose = 0
  65.  
  66.     if len(args)>0:
  67.         print "Invalid args"
  68.         usage()
  69.         return 1    
  70.     for opt, val in opts:
  71.         if opt == '-t':
  72.             logType = val
  73.         if opt == '-c':
  74.             computer = val
  75.         if opt in ['-h', '-?']:
  76.             Usage()
  77.             return
  78.         if opt=='-r':
  79.             do_read = 0
  80.         if opt=='-w':
  81.             do_write = 0
  82.         if opt=='-v':
  83.             verbose = verbose + 1
  84.     if do_write:
  85.         ReportEvent(logType, 2, strings=["The message text for event 2"], data = "Raw\0Data")
  86.         ReportEvent(logType, 1, eventType=win32evtlog.EVENTLOG_WARNING_TYPE, strings=["A warning"], data = "Raw\0Data")
  87.         ReportEvent(logType, 1, eventType=win32evtlog.EVENTLOG_INFORMATION_TYPE, strings=["An info"], data = "Raw\0Data")
  88.         print "Successfully wrote 3 records to the log"
  89.  
  90.     if do_read:
  91.         ReadLog(computer, logType, verbose > 0)
  92.  
  93. if __name__=='__main__':
  94.     test()
  95.  
  96.