Skip to content

Commit 279be90

Browse files
zachChris Dickinson
andauthored
feat: make it possible to add custom logging handler (#18)
Support for extism/extism#578 Update to `extism_sys==^1.0.0-rc7` --------- Co-authored-by: Chris Dickinson <[email protected]>
1 parent 98e1087 commit 279be90

File tree

5 files changed

+140
-108
lines changed

5 files changed

+140
-108
lines changed

example.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,9 @@
55
import hashlib
66
import pathlib
77

8-
from extism import Function, host_fn, ValType, Plugin, set_log_file, Json
8+
from extism import Function, host_fn, ValType, Plugin, set_log_custom, Json
99
from typing import Annotated
1010

11-
set_log_file("stderr", "trace")
12-
1311

1412
@host_fn(user_data=b"Hello again!")
1513
def hello_world(inp: Annotated[dict, Json], *a_string) -> Annotated[dict, Json]:
@@ -25,7 +23,8 @@ def count_vowels(data):
2523

2624

2725
def main(args):
28-
set_log_file("stderr", "trace")
26+
logs = []
27+
logBuffer = set_log_custom(lambda s: logs.append(s.strip()), "trace")
2928
if len(args) > 1:
3029
data = args[1].encode()
3130
else:
@@ -48,6 +47,12 @@ def main(args):
4847
assert j["count"] == count_vowels(data)
4948
assert j["roundtrip"] == 1
5049

50+
# Drain logs and print
51+
logBuffer.drain()
52+
print("Dumping logs", len(logs))
53+
for line in logs:
54+
print(line)
55+
5156

5257
if __name__ == "__main__":
5358
main(sys.argv)

extism/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
Error,
88
Plugin,
99
set_log_file,
10+
set_log_custom,
1011
extism_version,
1112
host_fn,
1213
Function,
@@ -24,6 +25,7 @@
2425
"Error",
2526
"CurrentPlugin",
2627
"set_log_file",
28+
"set_log_custom",
2729
"extism_version",
2830
"Memory",
2931
"host_fn",

extism/extism.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,45 @@ def set_log_file(
163163
_lib.extism_log_file(file.encode(), c_level)
164164

165165

166+
class CustomLogger:
167+
def __init__(self, f):
168+
self.callback = None
169+
self.set_callback(f)
170+
171+
def set_callback(self, f):
172+
@_ffi.callback("void(char*, ExtismSize)")
173+
def callback(ptr, len):
174+
f(_ffi.string(ptr, len).decode())
175+
176+
self.callback = callback
177+
178+
def drain(self):
179+
if self.callback is not None:
180+
_lib.extism_log_drain(self.callback)
181+
182+
def __del__(self):
183+
self.drain()
184+
185+
186+
def set_log_custom(
187+
f, level: Optional[Literal["debug", "error", "trace", "warn"]] = None
188+
):
189+
"""
190+
Enables buffered logging, this is a global configuration
191+
192+
:param f: The callback function, takes a string argument and no return value.
193+
:param level: The debug level.
194+
195+
:returns: a CustomLogger with a `drain` method that can be used to handle the buffered logs.
196+
"""
197+
c_level = level or _ffi.NULL
198+
if isinstance(level, str):
199+
c_level = level.encode()
200+
201+
_lib.extism_log_custom(c_level)
202+
return CustomLogger(f)
203+
204+
166205
def extism_version() -> str:
167206
"""
168207
Gets the Extism version string

0 commit comments

Comments
 (0)