caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Viet Le <vietlq85@gmail.com>
To: "caml-list@inria.fr" <caml-list@inria.fr>
Subject: [Caml-list] Macro expansion for logging with call site (file & line)
Date: Tue, 25 Jul 2017 22:17:50 +0100	[thread overview]
Message-ID: <CAG_8+G68ZcbdbbVgbfSVF9kTKJTG9FaM129a8=Q26Zf8NSvubA@mail.gmail.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 1243 bytes --]

Hi,

I would like to write a logger in OCaml. In C++ it's pretty straightforward
with macro expansion that gives correct call site (file & line):

$ cat -n cpp-logger.cpp
     1 #include <iostream>
     2
     3 #define LOG_START do {\
     4    std::cerr << __FILE__ << ":" << __LINE__ << ": "
     5 #define LOG_END std::endl; } while (0)
     6
     7 int main()
     8 {
     9    LOG_START << "Got problem!" << LOG_END;
    10    return 0;
    11 }

$ ./a.out
cpp-logger.cpp:9: Got problem!

In OCaml we have __LOC__ and __LOC_OF__:

http://caml.inria.fr/pub/docs/manual-ocaml/libref/Pervasives.html

However it's not trivial to get correct call site:

$ cat -n yahoo.ml
     1  print_string "yahoo\n";;
     2
     3  Printf.printf "__LOC__ = %s\n" __LOC__;;
     4
     5  let report s =
     6      let loc, _ = __LOC_OF__ s in
     7      Printf.printf "LOC of the expression (%s): %s\n" s loc ;;
     8
     9  report "bad bug" ;;

$ ./a.outyahoo
__LOC__ = File "yahoo.ml", line 3, characters 31-38
LOC of the expression (bad bug): File "yahoo.ml", line 6, characters 28-29

I expect line 9 but I get line 6 in OCaml. I would appreciate some help to
get this working. Does it involve campl4/5 / ppx?

Thanks.

-- 
Kind regards,
Viet

[-- Attachment #2: Type: text/html, Size: 4198 bytes --]

             reply	other threads:[~2017-07-25 21:17 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-07-25 21:17 Viet Le [this message]
2017-07-25 21:26 ` Viet Le

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAG_8+G68ZcbdbbVgbfSVF9kTKJTG9FaM129a8=Q26Zf8NSvubA@mail.gmail.com' \
    --to=vietlq85@gmail.com \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).