package logs-ppx

  1. Overview
  2. Docs
PPX to cut down on boilerplate when using Logs

Install

Dune Dependency

Authors

Maintainers

Sources

logs-ppx-0.2.0.tbz
sha256=053f043ef959d6dc079503eab482e91ac1509685d393c4b51f6991c210c0bfb1
sha512=15f5d2f88305bf26c653500f2c883e4774039b2b34a8f7cda3c4e6ae3dcc21939981a23d787e9a89a869c0bd76d0299cdcce2db548df108296f754e8c1584cd3

doc/src/logs_ppx/Ppx.ml.html

Source file Ppx.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
open Ppxlib
module Builder = Ast_builder.Default

let expander ~ctxt e l =
  let loc = Expansion_context.Extension.extension_point_loc ctxt in
  let log_loc, log_type =
    match e.pexp_desc with
    | Pexp_ident { loc; txt = Lident log_type } -> (loc, log_type)
    | _ -> (loc, "info")
  in
  Builder.eapply ~loc
    (Builder.pexp_ident ~loc:log_loc
       { txt = Ldot (Lident "Log", log_type); loc })
    [
      Builder.pexp_fun ~loc Nolabel None
        (Builder.ppat_var ~loc { txt = "logger-function"; loc })
        (Builder.eapply ~loc
           (Builder.pexp_ident ~loc { txt = Lident "logger-function"; loc })
           (List.map snd l));
    ]

let extension =
  let pattern =
    let open Ast_pattern in
    (* this grabs the first argument from the apply and
       then passes it into Log.sexp's [log] parameter.
       All the arguments of apply are parsed as a message. *)
    pstr (pstr_eval (pexp_apply __ __) nil ^:: nil)
  in
  Context_free.Rule.extension
    (Extension.V3.declare "log" Expression pattern expander)

let () = Driver.register_transformation ~rules:[ extension ] "logs-ppx"
OCaml

Innovation. Community. Security.