#!/usr/bin/env python ''' a logging plugin for flauxtext. ''' import os, time, sys orig_methods = {} logs_dir = None def __init__(): ''' ''' global logs_dir logs_dir = os.path.join(flauxtext.plugindir, 'logs') if not os.path.exists(logs_dir): os.mkdir(logs_dir) self.log = flauxtext.logginginit # override methods -- change the condition to False to keep from overriding methods if True: orig_methods['open'] = self.open def open(filename): orig_methods['open'](filename) if self.log: log('switched to ' + filename) self.open = open if True: orig_methods['callback_previous_tab'] = self.callback_previous_tab def callback_previous_tab(action=None): orig_methods['callback_previous_tab'](action) if self.log: log('switched to ' + self.current_filename()) self.callback_previous_tab = callback_previous_tab if True: orig_methods['callback_previous_n_tab'] = self.callback_previous_n_tab def callback_previous_n_tab(action=None): orig_methods['callback_previous_n_tab'](action) if self.log: log('switched to ' + self.current_filename()) self.callback_previous_n_tab = callback_previous_n_tab if True: orig_methods['callback_next_tab'] = self.callback_next_tab def callback_next_tab(action=None): orig_methods['callback_next_tab'](action) if self.log: log('switched to ' + self.current_filename()) self.callback_next_tab = callback_next_tab if True: orig_methods['callback_next_n_tab'] = self.callback_next_n_tab def callback_next_n_tab(action=None): orig_methods['callback_next_n_tab'](action) if self.log: log('switched to ' + self.current_filename()) self.callback_next_n_tab = callback_next_n_tab if True: orig_methods['callback_new'] = self.callback_new def callback_new(action=None): orig_methods['callback_new'](action) if self.log: log('switched to ' + self.current_filename()) self.callback_new = callback_new if True: orig_methods['close_buffer'] = self.close_buffer def close_buffer(*a): orig_methods['close_buffer'](*a) if self.log: if len(self): log('switched to ' + self.current_filename()) else: log('STOPPING') self.close_buffer = close_buffer if True: orig_methods['callback_saveas'] = self.callback_saveas def callback_saveas(*a): orig_methods['callback_saveas'](*a) if self.log: log('switched to ' + self.current_filename()) self.callback_saveas = callback_saveas if True: try: self.w.connect('focus-out-event', lambda *a: log('STOPPING')) self.w.connect('focus-in-event', lambda *a: log('switched to ' + self.current_filename())) except: print >> sys.stderr, "flauxtext logging: couldn't bind focus-*-event for self.w" if self.log: log('STARTING') def log(msg): ''' append a timestamped message msg to a log file in logs_dir. ''' f = open(os.path.join(logs_dir, time.strftime(flauxtext.loggingfilenameformat)), 'a') f.write(time.strftime(flauxtext.loggingtimestampformat) + ' ' + msg + '\n') f.close() def analyze(fn=None): ''' ask for a date, open the log file for that date, analyze it, write the analysis to a new buffer. ''' if fn is None: d = self.mk_dialog('which?', (flauxtext.gtk.STOCK_OK, 1)) d.vbox.pack_start(flauxtext.gtk.Label('enter a date in yymmdd format:')) e = flauxtext.gtk.Entry() e.connect('activate', lambda *a: d.response(1)) e.set_text(time.strftime(flauxtext.loggingfilenameformat)) d.vbox.pack_start(e) if d.run(): fn = os.path.join(logs_dir, e.get_text()) else: return if os.path.exists(fn): date = os.path.basename(fn) strptime = lambda s: time.mktime(time.strptime(s + date, flauxtext.loggingtimestampformat + flauxtext.loggingfilenameformat)) previous_time = 0 file_times = {} current_file = flauxtext.UNTITLED_FILENAME for line in open(fn): i = line.index(' ') new_time = strptime(line[:i]) msg = line[i + 1 :] if msg == 'STARTING': current_file = flauxtext.UNTITLED_FILENAME elif msg == 'STOPPING': file_times[current_file] = file_times.get(current_file, 0) + new_time - previous_time current_file = flauxtext.UNTITLED_FILENAME elif msg.startswith('switched to '): file_times[current_file] = file_times.get(current_file, 0) + new_time - previous_time current_file = line[i + 13 : -1] previous_time = new_time self.new_buffer().set_text('\n'.join([ time.strftime(flauxtext.loggingtimestampformat, time.gmtime(v)) + ' ' + k for k, v in file_times.iteritems() if (k != 'untitled') ])) else: self.flash_info("cannot analyze a log file because it doesn't exist: " + fn, 4e3, 'bad_log_file') def toggle(): if self.log: log('STOPPING') self.log = not self.log def logging(*a, **kw): ''' ''' if logs_dir is None: __init__() if (len(a) > 0) and (str(a[0]) in globals()): eval(a[0])()