Skip to content

Ppx deriver for logging function calls in OCaml for debugging purposes

License

Notifications You must be signed in to change notification settings

jrochel/ppx_log_calls

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ppx_log_calls

ppx_log_calls is a tool that helps OCaml developers debugging their programs. It is a ppx deriver that adds logging to any function whose definition is annotated by a %log extension point. Any call to a function with such an annotation is logged at execution time, including any parameter value for which an explicit type anotation is given.

Check out ppx_minidebug for a potentially more mature project offering similar functionality.

Example

let%log f ?(x : int option) (y : bool) z = ignore (x, y, z)

let () = f true "z"

The program above leads to the following console output:

f ?x:None true _

Note that the value "z" is not printed since no explicit type is given for the parameter z.

If you add a type annotation for the result like so:

let%log f ?(x : int option) (y : bool) z : unit = ignore (x, y, z)

Then in addition to the previous console output (before the execution of the function) the following output is displayed (after its execution):

f ?x:None true _ = ()

Usage

When using dune it suffices to add the following stanzas to your project's dune file:

(preprocess (pps ppx_deriving.show ppx_log_calls))
(libraries ppx_log_calls.runtime)

Then simply add a %log annotation to any let-binding for the defined function(s) to be logged. In order for the supplied arguments to be displayed as well an explicit type annotation have to be supplied as well as a pretty-printing function for that type.

If you write for example :

let%log f (x : mytype) = ignore x

Then there should be a pretty-printing function with the name pp_mytype in scope. If the type name is t then the pretty-printer is simply called pp.

Note that this naming scheme is compatible with the naming of pretty-printing functions generated by ppx_deriving.show.

Look at test/basic.ml for a simple example. It can be executed by running dune test -f.

License

ppx_log_calls is distributed under the terms of the BSD3 license (see LICENSE file).

About

Ppx deriver for logging function calls in OCaml for debugging purposes

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages