remove PLC.Debug.log, use PLC.Logger.logger instead
[plcapi.git] / PLC / Debug.py
1 # log system for PLCAPI
2 import time
3 import sys
4 import syslog
5
6 from PLC.Logger import logger
7
8 def profile(callable):
9     """
10     Prints the runtime of the specified callable. Use as a decorator, e.g.,
11
12         @profile
13         def foo(...):
14             ...
15
16     Or, equivalently,
17
18         def foo(...):
19             ...
20         foo = profile(foo)
21
22     Or inline:
23
24         result = profile(foo)(...)
25     """
26
27     def wrapper(*args, **kwds):
28         start = time.time()
29         result = callable(*args, **kwds)
30         end = time.time()
31         args = map(str, args)
32         args += ["%s = %s" % (name, str(value)) for (name, value) in kwds.items()]
33         logger.info("%s (%s): %f s" % (callable.__name__, ", ".join(args), end - start))
34         return result
35
36     return wrapper
37
38 if __name__ == "__main__":
39     def sleep(seconds = 1):
40         time.sleep(seconds)
41
42     sleep = profile(sleep)
43
44     sleep(1)