[ocaml-ctypes] Logging function calls

Thomas Braibant thomas.braibant at gmail.com
Fri Sep 12 10:15:14 BST 2014


Hi list,

I would like to log function calls using something like the following code:

let rec log:
type a. a fn -> a -> a =
    function
    | Static.Returns ty ->
       fun result ->
       Printf.printf "<- %s\n%!" (string_of ty result);
       result
    | Static.Function (ty, fn) ->
       fun f x ->
       Printf.printf "-> %s\n%!" (string_of ty x);
       log fn (f x)

This works beautifully for simple enough function arguments. However,
I would also like to display a bit more than what is currently
available.
Two quick examples come to mind.

First, printing the name of manifest constants (e.g., defined in C
using #define FOO 0x0001337). I would like to log FOO rather than its
integer value. If I provide a correct printing function, it might even
produces correct C code.

Second, a bit more akward, printing the content of structs that are
passed by reference to functions. I do not see a way to print that
information as valid C, but it would be tremendously useful in my
logs.

Both cases can probably be solved by redefining string_of using
suitable extension points in my bindings, but I wonder if someone on
this list has done something similar.

Best,
Thomas


More information about the Ctypes mailing list