From 7d99e7a94fe1eb7a261a5754cf1a840627fe5735 Mon Sep 17 00:00:00 2001 From: timofey Date: Fri, 4 Nov 2022 09:01:04 +0000 Subject: [PATCH] pain.py --- v6d2ctx/pain.py | 50 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 v6d2ctx/pain.py diff --git a/v6d2ctx/pain.py b/v6d2ctx/pain.py new file mode 100644 index 0000000..769c8ab --- /dev/null +++ b/v6d2ctx/pain.py @@ -0,0 +1,50 @@ +import os +import time + +from rainbowadn.instrument import Instrumentation + + +class ALog(Instrumentation): + start = time.time() + + def time(self): + return time.time() - self.start + + async def instrument(self, method, *args, **kwargs): + key = os.urandom(4).hex() + print(f'[{self.time(): 11.3f}] +{key} +A"{self.methodname}"') + try: + result = await method(*args, **kwargs) + except BaseException: + print(f'[{self.time(): 11.3f}] !{key} !A"{self.methodname}"') + raise + else: + print(f'[{self.time(): 11.3f}] -{key} -A"{self.methodname}"') + return result + + +class SLog(Instrumentation): + def time(self): + return time.time() - ALog.start + + def instrument(self, method, *args, **kwargs): + key = os.urandom(4).hex() + print(f'[{self.time(): 11.3f}] +{key} +S"{self.methodname}"') + try: + result = method(*args, **kwargs) + except BaseException: + print(f'[{self.time(): 11.3f}] !{key} !S"{self.methodname}"') + raise + else: + print(f'[{self.time(): 11.3f}] -{key} -S"{self.methodname}"') + return result + + +class FrameTrace(Instrumentation): + async def instrument(self, method, *args, **kwargs): + import inspect + frame = inspect.currentframe() + while frame is not None: + print(frame.f_code.co_filename, frame.f_lineno) + frame = frame.f_back + return await method(*args, **kwargs)