Skip to content

Commit

Permalink
feat: add set_log_callback
Browse files Browse the repository at this point in the history
  • Loading branch information
zshipko committed Nov 13, 2023
1 parent 0996741 commit aebdb49
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 11 deletions.
11 changes: 11 additions & 0 deletions src/bindings.ml
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,17 @@ let extism_plugin_output_data =
let extism_log_file =
fn "extism_log_file" (string @-> string_opt @-> returning bool)

let log_callback =
Foreign.dynamic_funptr ~runtime_lock:true
Ctypes.(ptr char @-> uintptr_t @-> returning void)

let extism_log_callback name f =
let (module Callback) = log_callback in
let cb = Callback.of_fun f in
fn "extism_log_callback"
(string_opt @-> Callback.t @-> returning bool)
name cb

let extism_version = fn "extism_version" (void @-> returning string)
let extism_plugin_free = fn "extism_plugin_free" (plugin @-> returning void)

Expand Down
48 changes: 37 additions & 11 deletions src/extism.ml
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,43 @@ let%test "with_plugin" =

let%test _ = String.length (extism_version ()) > 0

let parse_level =
Option.map (function
| `Error -> "error"
| `Warn -> "warn"
| `Info -> "info"
| `Debug -> "debug"
| `Trace -> "trace")

let set_log_file ?level filename =
let level =
Option.map
(function
| `Error -> "error"
| `Warn -> "warn"
| `Info -> "info"
| `Debug -> "debug"
| `Trace -> "trace")
level
Bindings.extism_log_file filename (parse_level level)

let callback = ref None

let set_log_callback ?level f =
callback := Some f;
let fx s length =
f @@ Ctypes.string_from_ptr s ~length:(Ctypes.Uintptr.to_int length)
in
Bindings.extism_log_file filename level
Bindings.extism_log_callback (parse_level level) fx

let%test _ = set_log_file ~level:`Trace "stderr"
let%test _ =
let log_file =
try
let _ = Unix.getenv "TEST_LOG_FILE" in
true
with Not_found -> false
in
if log_file then set_log_file ~level:`Trace "stderr"
else
let logs = ref [] in
let ok =
set_log_callback ~level:`Trace (fun line -> logs := line :: !logs)
in
let manifest = Manifest.(create [ Wasm.file "test/code.wasm" ]) in
let plugin = Plugin.of_manifest manifest |> Error.unwrap in
let _ =
Plugin.call Type.string Type.string plugin ~name:"count_vowels"
"this is a test"
in
ok && List.length !logs > 0
6 changes: 6 additions & 0 deletions src/extism.mli
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,12 @@ val set_log_file :
(** Set the log file and level for all Extism plugins, the names [stdout] or
[stderr] can be used to write to the terminal *)

val set_log_callback :
?level:[ `Error | `Warn | `Info | `Debug | `Trace ] ->
(string -> unit) ->
bool
(** Set the log level and callback function *)

(** Extism error type *)
module Error : sig
type t = [ `Msg of string ]
Expand Down

0 comments on commit aebdb49

Please sign in to comment.