From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@sympa.inria.fr Delivered-To: caml-list@sympa.inria.fr Received: from mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by sympa.inria.fr (Postfix) with ESMTPS id E67597FEE7 for ; Tue, 25 Jul 2017 23:26:05 +0200 (CEST) Authentication-Results: mail3-smtp-sop.national.inria.fr; spf=None smtp.pra=vietlq85@gmail.com; spf=Pass smtp.mailfrom=vietlq85@gmail.com; spf=None smtp.helo=postmaster@mail-yw0-f176.google.com Received-SPF: None (mail3-smtp-sop.national.inria.fr: no sender authenticity information available from domain of vietlq85@gmail.com) identity=pra; client-ip=209.85.161.176; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="vietlq85@gmail.com"; x-sender="vietlq85@gmail.com"; x-conformance=sidf_compatible Received-SPF: Pass (mail3-smtp-sop.national.inria.fr: domain of vietlq85@gmail.com designates 209.85.161.176 as permitted sender) identity=mailfrom; client-ip=209.85.161.176; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="vietlq85@gmail.com"; x-sender="vietlq85@gmail.com"; x-conformance=sidf_compatible; x-record-type="v=spf1" Received-SPF: None (mail3-smtp-sop.national.inria.fr: no sender authenticity information available from domain of postmaster@mail-yw0-f176.google.com) identity=helo; client-ip=209.85.161.176; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="vietlq85@gmail.com"; x-sender="postmaster@mail-yw0-f176.google.com"; x-conformance=sidf_compatible IronPort-PHdr: =?us-ascii?q?9a23=3Acbe4dhRMmyu3WLeHcerpjt0QVNpsv+yvbD5Q0YIu?= =?us-ascii?q?jvd0So/mwa69YRSN2/xhgRfzUJnB7Loc0qyN4vCmATRIyK3CmUhKSIZLWR4BhJ?= =?us-ascii?q?detC0bK+nBN3fGKuX3ZTcxBsVIWQwt1Xi6NU9IBJS2PAWK8TW94jEIBxrwKxd+?= =?us-ascii?q?KPjrFY7OlcS30P2594HObwlSijewZbB/IA+qoQnNq8IbnZZsJqEtxxXTv3BGYf?= =?us-ascii?q?5WxWRmJVKSmxbz+MK994N9/ipTpvws6ddOXb31cKokQ7NYCi8mM30u683wqRbD?= =?us-ascii?q?VwqP6WACXWgQjxFFHhLK7BD+Xpf2ryv6qu9w0zSUMMHqUbw5Xymp4qF2QxHqlS?= =?us-ascii?q?gHLSY0/2PZisJwgqxVow+vqQJjzIPPeo6ZKOBzc7nBcd8GR2dMWNtaWSxbAoO7?= =?us-ascii?q?aosCF+UOPedGoInhulAAqwWxDhSrCePxyz9HmGP23akj0+kiEQzLxwMgH9cUv3?= =?us-ascii?q?TVqNX5LrsdUeewzKTRyzjIcv1Y2TD46IfScxAhp+mBXbN2ccrLyEkgDRnJjluK?= =?us-ascii?q?qYD9PzKazOQNs2+c7+Z6UOKvj2gqoBxyrDi33soglJXFi4YPxl3H9Sh12ps5KN?= =?us-ascii?q?ylREJhfNKpEppduieHPIVsWMwiWXtnuCMix70Gp5G7eC8KxYwixxHFavyHd5GE?= =?us-ascii?q?4xLtVeqNODt4im9pdbC+ihqo/kigzer8Vsaw0FlUtCZKjt7MtnUV2xzS7MiIVO?= =?us-ascii?q?d981+/1TqT0w3f8OJJLEAumabGKpMt36Q8mocRvEnCBiP2nV/5jK6SdkUq4Oio?= =?us-ascii?q?7OHnb634pp+dKYB0lgX+Mr0tl8G/Guk4PQ0OUHKa+eS4zrHs4Ur5QLBSgv0sjq?= =?us-ascii?q?bZqIzaJdgcpqOhHwBayIMj6xKmAzei0dQYhmUHIUleeBOHiojpI0vBLOr5Dfe5?= =?us-ascii?q?mVSskS1ky+rIPr37Ud3xKS2Jm77kefN56lVA4As119FWoZxOQPlVK/v2Xgr1tc?= =?us-ascii?q?fEJh4/KQ29hej9XoZTzIQbDFyOD7PRFK6a5UGJ6/4rOrnRTIAQsTf5bfMi4qi9?= =?us-ascii?q?3jcChVYBcPzxjtMsY3eiE6E+Lg=3D=3D?= X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: =?us-ascii?q?A0DOBABitndZh7ChVdFdHQEXAQYBCwEXA?= =?us-ascii?q?QYBgjeBED+BFAeFIppMjWCKOCyFGwKDMQdDFAEBAQEBAQEBAQEBEgEBAQgNCQg?= =?us-ascii?q?oJAuCMyKCRAECAgEjHQEbHgMBCwYFCzcCAiEBAREBBQEcBhOKFgEDDQgQpDc/j?= =?us-ascii?q?AqCBAUBHIMJBYNjChknAwpWR4JmAQsBHQIGEoMWiFKCV4UvgkIfBYEtAQGGUAy?= =?us-ascii?q?PSYdHMggBAYFQCoV1h2CEcIJjj1aMFIgNFB+BFQ8nYEt3eFyDZ0mBdD82AYk0A?= =?us-ascii?q?QEB?= X-IPAS-Result: =?us-ascii?q?A0DOBABitndZh7ChVdFdHQEXAQYBCwEXAQYBgjeBED+BFAe?= =?us-ascii?q?FIppMjWCKOCyFGwKDMQdDFAEBAQEBAQEBAQEBEgEBAQgNCQgoJAuCMyKCRAECA?= =?us-ascii?q?gEjHQEbHgMBCwYFCzcCAiEBAREBBQEcBhOKFgEDDQgQpDc/jAqCBAUBHIMJBYN?= =?us-ascii?q?jChknAwpWR4JmAQsBHQIGEoMWiFKCV4UvgkIfBYEtAQGGUAyPSYdHMggBAYFQC?= =?us-ascii?q?oV1h2CEcIJjj1aMFIgNFB+BFQ8nYEt3eFyDZ0mBdD82AYk0AQEB?= X-IronPort-AV: E=Sophos;i="5.40,412,1496095200"; d="scan'208,217";a="232790030" Received: from mail-yw0-f176.google.com ([209.85.161.176]) by mail3-smtp-sop.national.inria.fr with ESMTP/TLS/AES128-GCM-SHA256; 25 Jul 2017 23:26:04 +0200 Received: by mail-yw0-f176.google.com with SMTP id u207so15028734ywc.3 for ; Tue, 25 Jul 2017 14:26:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:in-reply-to:references:from:date:message-id:subject:to; bh=+2/+TL47B7cS1hQcqKISJ5LF/LoND+gSEc6TKf/qvrI=; b=YUqfcPDw/tmK5YZ6bcqJW0f4ea6Lq66GTY4vnVQODB4WBz5Sp/ydW+G955jKqw9V52 l97ep9+pOtSq84WNq0E3F4nanbKHfut37tWWPDf1JGHSu+vSzHSEew2dV0XiVKcGjUiB oJtmGy9eQRQQpGm5VLvjxFWsWlT9OBXksqmd+JkzXD0uSPeyuUCJv0P1fqf8jpjxTO5s gVh9r4oXgDDEMBnp3wfyWSglxmITDm1cUfhXt7iksKcp8r1UXn9e4GZshWyJ8sANBMhh QhOjZ8q8TCrLgc5vsVLZp7Zr9DauhddHiDahry9rLPWTgg20w0tDG0pJnPYwlFcVRt+L 97Yg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to; bh=+2/+TL47B7cS1hQcqKISJ5LF/LoND+gSEc6TKf/qvrI=; b=Orr0tPS/lPTFEpS1Mk5d1OTabkaZsUy2j8m7BG1X05YEb2ziiKoHGwwgJ5T3Jq95Vo ggT4osC0KsexgEGC31VrRf2yCCaPGW8fGUUOI2JH85mRtNiVxB6ogArfZp6sOjR+YzzP Jnp/duG35K818ljikJCyabuyqbyizLfLnaJlGJAB0Jlm8ISh+8g/v+ELXDi0b6yDtGHA li9QLMKdA65WAoDAZbtmy7Mv6uaJGL/8WtcKEjbbJHMrzx2OkmyUlFH61AV7BCRFJI4S ptRXlWnjU5eAnn2evvZ1wi8XDyMFQaGlwyM7Zg7zxGYdXOagVLoKo/kE2YwX3j6gixT2 ezOA== X-Gm-Message-State: AIVw112s/F/AZS9PaYQS/c36W1ID5o16P5ItAh4xrjAnL6nhADGEYHJv 6beksSzGdAL/d/TEb3FOhGVO8ArwHmMjdpM= X-Received: by 10.37.118.13 with SMTP id r13mr18589419ybc.240.1501017963092; Tue, 25 Jul 2017 14:26:03 -0700 (PDT) MIME-Version: 1.0 Received: by 10.129.164.76 with HTTP; Tue, 25 Jul 2017 14:26:02 -0700 (PDT) In-Reply-To: References: From: Viet Le Date: Tue, 25 Jul 2017 22:26:02 +0100 Message-ID: To: "caml-list@inria.fr" Content-Type: multipart/alternative; boundary="001a114bd6d67df8ff05552afac7" Subject: Re: [Caml-list] Macro expansion for logging with call site (file & line) --001a114bd6d67df8ff05552afac7 Content-Type: text/plain; charset="UTF-8" The main point is to avoid dragging __LOC__ to line 9, and use a function/operator instead. It would look pretty ugly to have: report __LOC__ "bad bug". Thanks. On 25 July 2017 at 22:17, Viet Le wrote: > 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 > 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 > -- Kind regards, Viet --001a114bd6d67df8ff05552afac7 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
The main point is to avoid dragging=C2=A0__LOC__ to line 9= , and use a function/operator instead. It would look pretty ugly to have: report=C2=A0__LOC__ "bad bug".

Thanks.
<= /div>

On 25 July 2= 017 at 22:17, Viet Le <vietlq85@gmail.com> wrote:
Hi,

I would li= ke to write a logger in OCaml. In C++ it's pretty straightforward with = macro expansion that gives correct call site (file & line):
<= br>
$ cat -n cpp-logger.= cpp
=C2=A0 =C2=A0 =C2= =A01 #include <iostream><= /font>
=C2=A0 =C2=A0 =C2=A02<= /font>
=C2=A0 =C2=A0 =C2=A03<= span style=3D"white-space:pre-wrap"> #define LOG_START do {\<= /div>
=C2=A0 =C2=A0 =C2=A04 =C2=A0 =C2=A0std::cerr << __FIL= E__ << ":" << __LINE__ << ": "=
=C2=A0 =C2=A0 =C2=A05 #define LOG_END std::endl; } while (0= )
=C2=A0 =C2=A0 =C2=A0= 6
=C2=A0 =C2=A0 =C2=A0= 7 int main()
<= font face=3D"monospace, monospace">=C2=A0 =C2=A0 =C2=A08 {
=C2=A0 =C2=A0 =C2=A09 = =C2=A0 =C2=A0LOG_START << "Got problem!" << LOG_END;<= /font>
=C2=A0 =C2=A0 10 =C2=A0 =C2=A0return 0;
=
=C2=A0 =C2=A0 11 }

$ ./a.out<= /font>
cpp-logger.cpp:9: Got = problem!

In OCaml we have __LOC__ and __LOC= _OF__:

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

$ cat -n=C2=A0yaho= o.ml
=C2=A0 =C2=A0 =C2=A01 =C2=A0print_string "yahoo\n";= ;
=C2=A0 =C2=A0 =C2=A02
=C2=A0 =C2=A0 =C2=A03 =C2=A0Printf.printf= "__LOC__ =3D %s\n" __LOC__;;
=C2=A0 =C2=A0 =C2=A04
=C2= =A0 =C2=A0 =C2=A05 =C2=A0let report s =3D
=C2=A0 =C2=A0 =C2=A06 =C2=A0= =C2=A0 =C2=A0let loc, _ =3D __LOC_OF__ s in
=C2=A0 =C2=A0 =C2=A07 =C2= =A0 =C2=A0 =C2=A0Printf.printf "LOC of the expression (%s): %s\n"= s loc ;;
=C2=A0 =C2=A0 =C2=A08
=C2=A0 =C2=A0 =C2=A09 =C2=A0repor= t "bad bug" ;;

$ ./a.outyahoo
__LOC__ =3D File "yahoo.ml", line 3, characters 31-38<= /div>
LO= C 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 ap= preciate some help to get this working. Does it involve campl4/5 / ppx?
=
Thanks.

--
Kind regards,
Viet



--
Kind regards,
Viet
--001a114bd6d67df8ff05552afac7--