blob: 0bb403c43cb85855e5e2d675ceabb41c7157ebb7 [file] [log] [blame]
mblighb05a6ac2007-11-26 21:46:54 +00001"""\
2Logging helper tools.
3"""
4
5__author__ = 'jadmanski@google.com (John Admanski)'
6
7
mbligh119c12a2007-11-12 22:13:44 +00008def record(fn):
9 """
10 Generic method decorator for logging calls under the
11 assumption that return=GOOD, exception=FAIL. The method
12 determines parameters as:
13 subdir = self.subdir if it exists, or None
14 operation = "class name"."method name"
15 status = None on GOOD, str(exception) on FAIL
16 The object using this method must have a job attribute
17 for the logging to actually occur, otherwise the logging
18 will silently fail.
19
20 Logging can explicitly be disabled for a call by passing
21 a logged=False parameter
22 """
23 def recorded_func(self, *args, **dargs):
24 logged = dargs.pop('logged', True)
25 job = getattr(self, 'job', None)
26 # if logging is disabled/unavailable, just
27 # call the method
28 if not logged or job is None:
29 return fn(self, *args, **dargs)
30 # logging is available, so wrap the method call
31 # in success/failure logging
32 subdir = getattr(self, 'subdir', None)
33 operation = '%s.%s' % (self.__class__.__name__,
34 fn.__name__)
35 try:
36 result = fn(self, *args, **dargs)
37 job.record('GOOD', subdir, operation)
38 except Exception, detail:
39 job.record('FAIL', subdir, operation, str(detail))
40 raise
41 return result
42 return recorded_func