From 54863e0fb3f859d100f36588d5784387d7a82d50 Mon Sep 17 00:00:00 2001 From: nanotube Date: Tue, 22 Nov 2005 23:52:51 +0000 Subject: [PATCH] use a repeating timer for autoflushing the buffer, when logging to onefile. --- logwriter.py | 41 +++++++++++++++++++++++------------------ 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/logwriter.py b/logwriter.py index a305d19..8e9e326 100644 --- a/logwriter.py +++ b/logwriter.py @@ -3,7 +3,8 @@ import os, os.path import time import re import sys - +#from threading import Timer +import mytimer class LogWriter: @@ -99,6 +100,10 @@ class LogWriter: return False return True + def TimerAction(self): + self.PrintDebug("flushing onefile buffer due to timer\n") + self.log.flush() + def OpenLogFile(self, event): if self.options.oneFile != None: @@ -117,24 +122,14 @@ class LogWriter: return False self.PrintDebug("writing to: " + self.writeTarget + "\n") + self.timer = mytimer.MyTimer(60.0, 0, self.TimerAction) + self.timer.start() return True subDirName = self.filter.sub(r'__',self.processName) #our subdirname is the full path of the process owning the hwnd, filtered. WindowName = self.filter.sub(r'__',str(event.WindowName)) - try: - os.makedirs(os.path.join(self.options.dirName, subDirName), 0777) - except OSError, detail: - if(detail.errno==17): #if directory already exists, swallow the error - pass - else: - self.PrintDebug(sys.exc_info()[0] + ", " + sys.exc_info()[1] + "\n") - return False - except: - self.PrintDebug("Unexpected error: " + sys.exc_info()[0] + ", " + sys.exc_info()[1] + "\n") - return False - filename = time.strftime('%Y%m%d') + "_" + str(event.Window) + "_" + WindowName + ".txt" #make sure our filename plus path is not longer than 255 characters, as per filesystem limit. @@ -158,6 +153,18 @@ class LogWriter: self.writeTarget = os.path.join(self.options.dirName, subDirName, filename) self.PrintDebug("writeTarget:" + self.writeTarget + "\n") + try: + os.makedirs(os.path.join(self.options.dirName, subDirName), 0777) + except OSError, detail: + if(detail.errno==17): #if directory already exists, swallow the error + pass + else: + self.PrintDebug(sys.exc_info()[0] + ", " + sys.exc_info()[1] + "\n") + return False + except: + self.PrintDebug("Unexpected error: " + sys.exc_info()[0] + ", " + sys.exc_info()[1] + "\n") + return False + try: self.log = open(self.writeTarget, 'a') except OSError, detail: @@ -176,9 +183,7 @@ class LogWriter: if not self.options.debug: self.log.write(stuff) else: - sys.stdout.write(stuff) - if self.options.systemLog != None: - self.systemlog.write(stuff) + self.PrintDebug(stuff) def PrintDebug(self, stuff): if self.options.debug: @@ -200,7 +205,7 @@ class LogWriter: if __name__ == '__main__': #some testing code #put a real existing hwnd into event.Window to run test - #this testing code is now outdated. + #this testing code is now really outdated. lw = LogWriter() class Blank: pass @@ -211,6 +216,6 @@ if __name__ == '__main__': event.Key = 'A' options = Blank() options.parseBackspace = options.parseEscape = options.addLineFeed = options.debug = False - options.flushKey = 'F12' + options.flushKey = 'F11' lw.WriteToLogFile(event, options) -- 2.45.1