Package Peach :: Module logger
[hide private]

Source Code for Module Peach.logger

  1  ''' 
  2  Peach Logging System 
  3   
  4  This is the Peach logging sub-system.  To implement a new logging method 
  5  extend from Logger. 
  6   
  7  @author: Michael Eddington 
  8  @version: $Id: logger.py 1051 2008-07-17 04:52:54Z meddingt $ 
  9  ''' 
 10   
 11  # 
 12  # Copyright (c) 2007-2008 Michael Eddington 
 13  # 
 14  # Permission is hereby granted, free of charge, to any person obtaining a copy  
 15  # of this software and associated documentation files (the "Software"), to deal 
 16  # in the Software without restriction, including without limitation the rights  
 17  # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell  
 18  # copies of the Software, and to permit persons to whom the Software is  
 19  # furnished to do so, subject to the following conditions: 
 20  # 
 21  # The above copyright notice and this permission notice shall be included in     
 22  # all copies or substantial portions of the Software. 
 23  # 
 24  # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR  
 25  # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,  
 26  # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE  
 27  # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER  
 28  # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 29  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
 30  # SOFTWARE. 
 31  # 
 32   
 33  # Authors: 
 34  #   Michael Eddington (mike@phed.org) 
 35   
 36  # $Id: logger.py 1051 2008-07-17 04:52:54Z meddingt $ 
 37   
 38  from Engine.engine import EngineWatcher 
 39   
40 -class Logger(EngineWatcher):
41 ''' 42 Parent class for all logger implementations. 43 ''' 44
45 - def OnRunStarting(self, run):
46 ''' 47 Called when a run is starting. 48 ''' 49 pass
50
51 - def OnRunFinished(self, run):
52 ''' 53 Called when a run is finished. 54 ''' 55 pass
56
57 - def OnTestStarting(self, run, test, totalVariations):
58 ''' 59 Called on start of a test. Each test has multiple variations. 60 ''' 61 pass
62
63 - def OnTestFinished(self, run, test):
64 ''' 65 Called on completion of a test. 66 ''' 67 pass
68
69 - def OnTestCaseStarting(self, run, test, variationCount):
70 ''' 71 Called on start of a test case. 72 ''' 73 pass
74
75 - def OnTestCaseReceived(self, run, test, variationCount, value):
76 ''' 77 Called when data is received from test case. 78 ''' 79 pass
80
81 - def OnTestCaseException(self, run, test, variationCount, exception):
82 ''' 83 Called when an exception occurs during a test case. 84 ''' 85 pass
86
87 - def OnTestCaseFinished(self, run, test, variationCount, actionValues):
88 ''' 89 Called when a test case has completed. 90 ''' 91 pass
92
93 - def OnFault(self, run, test, variationCount, monitorData, value):
94 pass
95
96 - def OnStopRun(self, run, test, variationCount, monitorData, value):
97 pass
98 99 import os,uuid 100 from time import * 101
102 -class Filesystem(Logger):
103 ''' 104 A file system logger. 105 ''' 106
107 - def __init__(self, params):
108 self.name = str(uuid.uuid1()) 109 self.elementType = 'logger' 110 self.params = params 111 self.heartBeat = 512
112
113 - def _writeMsg(self, line):
114 self.file.write(asctime() + ": " + line + "\n") 115 self.file.flush()
116
117 - def OnRunStarting(self, run):
118 suppliedPath = eval(str(self.params['path'])) 119 120 self.path = os.path.join(suppliedPath, run.name + "_" + strftime("%Y_%b_%d_%H_%M_%S", gmtime())) 121 self.faultPath = os.path.join(self.path, "Faults") 122 try: 123 os.mkdir(suppliedPath) 124 except: 125 pass 126 try: 127 os.mkdir(self.path) 128 except: 129 pass 130 131 self.file = open(os.path.join(self.path,"status.txt"), "w") 132 133 self.file.write("Peach 2.0 Fuzzer Run\n") 134 self.file.write("====================\n\n") 135 self.file.write("Date of run: " + asctime() + "\n") 136 self.file.write("Run name: " + run.name + "\n\n")
137
138 - def OnRunFinished(self, run):
139 self.file.write("\n\n== Run completed ==\n" + asctime() + "\n") 140 self.file.close() 141 self.file = None
142
143 - def OnTestStarting(self, run, test, totalVariations):
144 self._writeMsg("") 145 self._writeMsg("Test starting: " + test.name) 146 #self._writeMsg("Test has %d variations" % totalVariations) 147 self._writeMsg("")
148
149 - def OnTestFinished(self, run, test):
150 self._writeMsg("") 151 self._writeMsg("Test completed: " + test.name) 152 self._writeMsg("")
153
154 - def OnTestCaseException(self, run, test, variationCount, exception):
155 pass
156
157 - def OnFault(self, run, test, variationCount, monitorData, actionValues):
158 self._writeMsg("Fault was detected on test %d" % variationCount) 159 160 path = os.path.join(self.faultPath,str(variationCount)) 161 try: 162 os.mkdir(self.faultPath) 163 except: 164 pass 165 try: 166 os.mkdir(path) 167 except: 168 pass 169 170 # Expand actionValues 171 172 for i in range(len(actionValues)): 173 fileName = os.path.join(path, "data_%d_%s_%s.txt" % (i, actionValues[i][1], actionValues[i][0])) 174 175 if len(actionValues[i]) > 2: 176 fout = open(fileName, "w+b") 177 fout.write(actionValues[i][2]) 178 179 if len(actionValues[i]) > 3: 180 fout.write(repr(actionValues[i][3])) 181 182 fout.close() 183 184 for key in monitorData.keys(): 185 fout = open(path + key, "wb") 186 fout.write(monitorData[key]) 187 fout.close()
188
189 - def OnStopRun(self, run, test, variationCount, monitorData, value):
190 self._writeMsg("") 191 self._writeMsg("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!") 192 self._writeMsg("!!!! TEST ABORTING AT %d" % variationCount) 193 self._writeMsg("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!") 194 self._writeMsg("")
195 196
197 - def OnTestCaseStarting(self, run, test, variationCount):
198 ''' 199 Called on start of a test case. 200 ''' 201 if variationCount % self.heartBeat == 0: 202 self._writeMsg("On test variation # %d" % variationCount)
203 204 # end 205