Package omero :: Package install :: Module logs_library
[hide private]
[frames] | no frames]

Source Code for Module omero.install.logs_library

  1  #!/usr/bin/env python 
  2  # -*- coding: utf-8 -*- 
  3   
  4  """ 
  5   
  6     Function for parsing OMERO log files. 
  7     The format expected is defined for Python in 
  8     omero.util.configure_logging. 
  9   
 10     Copyright 2010 Glencoe Software, Inc. All rights reserved. 
 11     Use is subject to license terms supplied in LICENSE.txt 
 12   
 13     :author: Josh Moore <josh@glencoesoftware.com> 
 14   
 15  """ 
 16   
 17  import numpy as np 
 18  import matplotlib.pyplot as plt 
 19  import matplotlib.lines as lines 
 20  import matplotlib.transforms as mtransforms 
 21  import matplotlib.text as mtext 
 22   
 23  from time import mktime, strptime 
 24   
 25  import fileinput 
 26  import logging 
 27  import sys 
 28  import os 
 29  import re 
 30   
31 -def parse_time(value):
32 """ 33 parse the time format used by log4j into seconds (float) 34 since the epoch 35 """ 36 parts = value.split(",") 37 value = parts[0] 38 millis = float(parts[1]) / 1000.0 39 t = mktime(strptime(value, "%Y-%m-%d %H:%M:%S")) 40 t = float(t) 41 t += millis 42 return t
43
44 -class log_line(object):
45 """ 46 2009-04-09 15:11:58,029 INFO [ ome.services.util.ServiceHandler] (l.Server-6) Meth: interface ome.api.IQuery.findByQuery 47 01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789 48 """
49 - def __init__(self, line):
50 self.line = line 51 line.strip() 52 self.date = line[0:23] 53 self.level = line[24:28] 54 self.thread = line[74:84] 55 self.message = line[85:].strip() 56 self.status = line[86:91] 57 self.method = line[96:].strip()
58
59 - def contains(self, s):
60 return 0 <= self.line.find(s)
61
62 - def contains_any(self, l):
63 for i in l: 64 if self.contains(i): 65 return True 66 return False
67
68 -class log_watcher(object):
69
70 - def __init__(self, files, entries, exits, storeonce = None, storeall = None):
71 if storeonce is None: storeonce = [] 72 if storeall is None: storeall = [] 73 self.files = files 74 self.entries = entries 75 self.exits = exits 76 self.storeonce = storeonce 77 self.storeall = storeall
78
79 - def gen(self):
80 self.m = {} 81 try: 82 for line in fileinput.input(self.files): 83 ll = log_line(line) 84 if ll.contains_any(self.entries): 85 self.m[ll.thread] = ll 86 elif ll.contains_any(self.storeonce): 87 try: 88 value = self.m[ll.thread] 89 try: 90 value.once 91 except: 92 value.once = ll 93 except KeyError: 94 logging.debug("Not found: " + line) 95 elif ll.contains_any(self.storeall): 96 try: 97 value = self.m[ll.thread] 98 value.all.append(ll) 99 except AttributeError: 100 value.all = [ll] 101 except KeyError: 102 logging.debug("Not found: " + line) 103 elif ll.contains_any(self.exits): 104 try: 105 value = self.m[ll.thread] 106 del self.m[ll.thread] # Free memory 107 108 value.start = parse_time(value.date) 109 value.stop = parse_time(ll.date) 110 value.took = value.stop - value.start 111 yield value 112 except KeyError: 113 logging.debug("Not found: " + line) 114 finally: 115 fileinput.close()
116
117 -class allthreads_watcher(log_watcher):
118 - def __init__(self, files):
119 log_watcher.__init__(self, files, ["Meth:","Executor.doWork"],["Rslt:","Excp:"])
120
121 -class saveAndReturnObject_watcher(log_watcher):
122 - def __init__(self, files):
123 log_watcher.__init__(self, files, ["saveAndReturnObject"],["Rslt:","Excp:"],storeonce=["Args:"],storeall=["Adding log"])
124 125 # http://matplotlib.sourceforge.net/examples/api/line_with_text.html
126 -class MyLine(lines.Line2D):
127
128 - def __init__(self, *args, **kwargs):
129 # we'll update the position when the line data is set 130 self.text = mtext.Text(0, 0, '') 131 lines.Line2D.__init__(self, *args, **kwargs) 132 133 # we can't access the label attr until *after* the line is 134 # inited 135 self.text.set_text(self.get_label())
136
137 - def set_figure(self, figure):
138 self.text.set_figure(figure) 139 lines.Line2D.set_figure(self, figure)
140
141 - def set_axes(self, axes):
142 self.text.set_axes(axes) 143 lines.Line2D.set_axes(self, axes)
144
145 - def set_transform(self, transform):
146 # 2 pixel offset 147 texttrans = transform + mtransforms.Affine2D().translate(2, 2) 148 self.text.set_transform(texttrans) 149 lines.Line2D.set_transform(self, transform)
150
151 - def set_data(self, x, y):
152 if len(x): 153 self.text.set_position((x[-1], y[-1])) 154 155 lines.Line2D.set_data(self, x, y)
156
157 - def draw(self, renderer):
158 # draw my label at the end of the line with 2 pixel offset 159 lines.Line2D.draw(self, renderer) 160 self.text.draw(renderer)
161
162 -def plot_threads(watcher, all_colors = ("blue","red","yellow","green","pink","purple")):
163 digit = re.compile(".*(\d+).*") 164 165 fig = plt.figure() 166 ax = fig.add_subplot(111) 167 168 first = None 169 last = None 170 colors = {} 171 for ll in watcher.gen(): 172 last = ll.stop 173 if first is None: 174 first = ll.start 175 176 if ll.thread.strip() == "main": 177 t = -1 178 else: 179 try: 180 t = digit.match(ll.thread).group(1) 181 except: 182 print "Error parsing thread:", ll.thread 183 raise 184 y = np.array([int(t),int(t)]) 185 x = np.array([ll.start-first, ll.stop-first]) 186 c = colors.get(t,all_colors[0]) 187 i = all_colors.index(c) 188 colors[t] = all_colors[ (i+1) % len(all_colors) ] 189 190 if True: 191 line = MyLine(x, y, c=c, lw=2, alpha=0.5)#, mfc='red')#, ms=12, label=str(len(ll.logs))) 192 #line.text.set_text('line label') 193 line.text.set_color('red') 194 #line.text.set_fontsize(16) 195 ax.add_line(line) 196 else: 197 # http://matplotlib.sourceforge.net/examples/pylab_examples/broken_barh.html 198 ax.broken_barh([ (110, 30), (150, 10) ] , (10, 9), facecolors='blue') 199 200 ax.set_ylim(-2,25) 201 ax.set_xlim(0, (last-first)) 202 plt.show()
203 204 if __name__ == "__main__": 205 for g in allthreads_watcher(sys.argv).gen(): 206 print "Date:%s\nElapsed:%s\nLevel:%s\nThread:%s\nMethod:%s\nStatus:%s\n\n" % (g.date, g.took, g.level, g.thread, g.message, g.status) 207