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 = [], storeall = []):
70 self.files = files 71 self.entries = entries 72 self.exits = exits 73 self.storeonce = storeonce 74 self.storeall = storeall
75
76 - def gen(self):
77 self.m = {} 78 try: 79 for line in fileinput.input(self.files): 80 ll = log_line(line) 81 if ll.contains_any(self.entries): 82 self.m[ll.thread] = ll 83 elif ll.contains_any(self.storeonce): 84 try: 85 value = self.m[ll.thread] 86 try: 87 value.once 88 except: 89 value.once = ll 90 except KeyError: 91 logging.debug("Not found: " + line) 92 elif ll.contains_any(self.storeall): 93 try: 94 value = self.m[ll.thread] 95 value.all.append(ll) 96 except AttributeError: 97 value.all = [ll] 98 except KeyError: 99 logging.debug("Not found: " + line) 100 elif ll.contains_any(self.exits): 101 try: 102 value = self.m[ll.thread] 103 del self.m[ll.thread] # Free memory 104 105 value.start = parse_time(value.date) 106 value.stop = parse_time(ll.date) 107 value.took = value.stop - value.start 108 yield value 109 except KeyError: 110 logging.debug("Not found: " + line) 111 finally: 112 fileinput.close()
113
114 -class allthreads_watcher(log_watcher):
115 - def __init__(self, files):
116 log_watcher.__init__(self, files, ["Meth:","Executor.doWork"],["Rslt:","Excp:"])
117
118 -class saveAndReturnObject_watcher(log_watcher):
119 - def __init__(self, files):
120 log_watcher.__init__(self, files, ["saveAndReturnObject"],["Rslt:","Excp:"],storeonce=["Args:"],storeall=["Adding log"])
121 122 # http://matplotlib.sourceforge.net/examples/api/line_with_text.html
123 -class MyLine(lines.Line2D):
124
125 - def __init__(self, *args, **kwargs):
126 # we'll update the position when the line data is set 127 self.text = mtext.Text(0, 0, '') 128 lines.Line2D.__init__(self, *args, **kwargs) 129 130 # we can't access the label attr until *after* the line is 131 # inited 132 self.text.set_text(self.get_label())
133
134 - def set_figure(self, figure):
135 self.text.set_figure(figure) 136 lines.Line2D.set_figure(self, figure)
137
138 - def set_axes(self, axes):
139 self.text.set_axes(axes) 140 lines.Line2D.set_axes(self, axes)
141
142 - def set_transform(self, transform):
143 # 2 pixel offset 144 texttrans = transform + mtransforms.Affine2D().translate(2, 2) 145 self.text.set_transform(texttrans) 146 lines.Line2D.set_transform(self, transform)
147
148 - def set_data(self, x, y):
149 if len(x): 150 self.text.set_position((x[-1], y[-1])) 151 152 lines.Line2D.set_data(self, x, y)
153
154 - def draw(self, renderer):
155 # draw my label at the end of the line with 2 pixel offset 156 lines.Line2D.draw(self, renderer) 157 self.text.draw(renderer)
158
159 -def plot_threads(watcher, all_colors = ["blue","red","yellow","green","pink","purple"]):
160 digit = re.compile(".*(\d+).*") 161 162 fig = plt.figure() 163 ax = fig.add_subplot(111) 164 165 first = None 166 last = None 167 colors = {} 168 for ll in watcher.gen(): 169 last = ll.stop 170 if first is None: 171 first = ll.start 172 173 if ll.thread.strip() == "main": 174 t = -1 175 else: 176 try: 177 t = digit.match(ll.thread).group(1) 178 except: 179 print "Error parsing thread:", ll.thread 180 raise 181 y = np.array([int(t),int(t)]) 182 x = np.array([ll.start-first, ll.stop-first]) 183 c = colors.get(t,all_colors[0]) 184 i = all_colors.index(c) 185 colors[t] = all_colors[ (i+1) % len(all_colors) ] 186 187 if True: 188 line = MyLine(x, y, c=c, lw=2, alpha=0.5)#, mfc='red')#, ms=12, label=str(len(ll.logs))) 189 #line.text.set_text('line label') 190 line.text.set_color('red') 191 #line.text.set_fontsize(16) 192 ax.add_line(line) 193 else: 194 # http://matplotlib.sourceforge.net/examples/pylab_examples/broken_barh.html 195 ax.broken_barh([ (110, 30), (150, 10) ] , (10, 9), facecolors='blue') 196 197 ax.set_ylim(-2,25) 198 ax.set_xlim(0, (last-first)) 199 plt.show()
200 201 if __name__ == "__main__": 202 for g in allthreads_watcher(sys.argv).gen(): 203 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) 204