A small package for tracing function calls
Project description
Call Traces
A small package for tracing function calls
Installing ⏬
pip install calltraces
Usage 📝
Python code:
from calltraces.linetrace import traceInfo
from calltraces.functiontrace import functiontrace
# ...
@functiontrace # Add a function decorator
def myFunction():
traceInfo("some log message...")
# ...
# Whenever the function is called:
myFunction()
This will produce the output:
[INFO:01] 2022-06-26 18:00:32,234 __main__.myFunction Starting Function
[INFO:02] 2022-06-26 18:00:32,235 some log message...
[INFO:01] 2022-06-26 18:00:32,235 __main__.myFunction Finished Function
Advanced Usage 🪆
Python code:
from calltraces.linetrace import traceInfo
from calltraces.functiontrace import functiontrace
# ...
@functiontrace
def myFunction1():
traceInfo("some log message...")
@functiontrace
def myFunction2():
myFunction1()
@functiontrace
def myFunction3():
# Calling a function twice
myFunction2()
myFunction2()
# ...
# Whenever the function is called:
myFunction3()
This will produce the output:
[INFO:01] 2022-06-26 18:06:38,625 __main__.myFunction3 Starting Function
[INFO:02] 2022-06-26 18:06:38,626 __main__.myFunction2 Starting Function
[INFO:03] 2022-06-26 18:06:38,634 __main__.myFunction1 Starting Function
[INFO:04] 2022-06-26 18:06:38,634 some log message...
[INFO:03] 2022-06-26 18:06:38,634 __main__.myFunction1 Finished Function
[INFO:02] 2022-06-26 18:06:38,634 __main__.myFunction2 Finished Function
[INFO:02] 2022-06-26 18:06:38,634 __main__.myFunction2 Starting Function
[INFO:03] 2022-06-26 18:06:38,635 __main__.myFunction1 Starting Function
[INFO:04] 2022-06-26 18:06:38,635 some log message...
[INFO:03] 2022-06-26 18:06:38,635 __main__.myFunction1 Finished Function
[INFO:02] 2022-06-26 18:06:38,635 __main__.myFunction2 Finished Function
[INFO:01] 2022-06-26 18:06:38,635 __main__.myFunction3 Finished Function
Usage with classes 📦
Adding the python decorator on a class will automatically add trace wrappers to public functions.
⚠️ Private functions will stay unaffected.
Python code:
from calltraces.linetrace import traceInfo
from calltraces.classtrace import classtrace
# ...
@classtrace # Add a class decorator
class MyObject:
def __init__(self, input) -> None:
self._x = input
def _privateFunction(self):
self._x = self._x * 2
def __superPrivateFunction(self):
self._x = self._x * 3
def publicBaseFunction(self):
self._privateFunction()
self.__superPrivateFunction()
traceInfo("some log message...")
def publicWrapperFunction(self) -> None:
return self.publicBaseFunction()
# ...
# Whenever the object functions are called
myObj = MyObject(input=12345)
myObj.publicWrapperFunction()
This will produce the output:
[INFO:01] 2022-06-26 18:17:15,456 __main__.MyObject.publicWrapperFunction Starting Function
[INFO:02] 2022-06-26 18:17:15,456 __main__.MyObject.publicBaseFunction Starting Function
[INFO:03] 2022-06-26 18:17:15,456 some log message...
[INFO:02] 2022-06-26 18:17:15,456 __main__.MyObject.publicBaseFunction Finished Function
[INFO:01] 2022-06-26 18:17:15,456 __main__.MyObject.publicWrapperFunction Finished Function
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
calltraces-2022.6.26.3.tar.gz
(2.8 kB
view hashes)
Built Distribution
Close
Hashes for calltraces-2022.6.26.3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 264383b30a9f6c9fc933c246265b9b866b6140eb9c691e870a593a763aed261c |
|
MD5 | a37805423e9a00140d0cf41827d4496b |
|
BLAKE2b-256 | 3297038b57a2a9fd0053d549e70ef3517e05a94b9de9b623c99daec64ce0ff43 |