Skip to content

Commit

Permalink
cleanup: use new API
Browse files Browse the repository at this point in the history
  • Loading branch information
zshipko committed Nov 14, 2023
1 parent 46a4f6b commit 90cda0a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 24 deletions.
12 changes: 4 additions & 8 deletions src/bindings.ml
Original file line number Diff line number Diff line change
Expand Up @@ -126,17 +126,13 @@ let extism_plugin_output_data =
let extism_log_file =
fn "extism_log_file" (string @-> string_opt @-> returning bool)

let extism_log_custom = fn "extism_log_custom" (string_opt @-> returning bool)

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

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

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

Expand Down
21 changes: 10 additions & 11 deletions src/extism.ml
Original file line number Diff line number Diff line change
Expand Up @@ -41,32 +41,31 @@ let parse_level =
let set_log_file ?level filename =
Bindings.extism_log_file filename (parse_level level)

let callback = ref None
let set_log_custom ?level () = Bindings.extism_log_custom (parse_level level)

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

let%test _ =
let log_file =
try
let _ = Unix.getenv "TEST_LOG_FILE" in
true
try String.length @@ Unix.getenv "TEST_LOG_FILE" > 0
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 ok = set_log_custom ~level:`Trace () 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
let () = drain_logs (fun line -> logs := line :: !logs) in
let () = Printf.printf "%d\n" (List.length !logs) in
let () = List.iter print_string !logs in
ok && List.length !logs > 0
12 changes: 7 additions & 5 deletions src/extism.mli
Original file line number Diff line number Diff line change
Expand Up @@ -425,11 +425,13 @@ 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 *)
val set_log_custom :
?level:[ `Error | `Warn | `Info | `Debug | `Trace ] -> unit -> bool
(** Set the log level and enable buffered logging. Logs can be accessed using
{!drain_logs} *)

val drain_logs : (string -> unit) -> unit
(** Drain buffered logs *)

(** Extism error type *)
module Error : sig
Expand Down

0 comments on commit 90cda0a

Please sign in to comment.