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