* [Caml-list] Macro expansion for logging with call site (file & line)
@ 2017-07-25 21:17 Viet Le
2017-07-25 21:26 ` Viet Le
0 siblings, 1 reply; 2+ messages in thread
From: Viet Le @ 2017-07-25 21:17 UTC (permalink / raw)
To: caml-list
[-- 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 --]
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [Caml-list] Macro expansion for logging with call site (file & line)
2017-07-25 21:17 [Caml-list] Macro expansion for logging with call site (file & line) Viet Le
@ 2017-07-25 21:26 ` Viet Le
0 siblings, 0 replies; 2+ messages in thread
From: Viet Le @ 2017-07-25 21:26 UTC (permalink / raw)
To: caml-list
[-- Attachment #1: Type: text/plain, Size: 1587 bytes --]
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 <vietlq85@gmail.com> 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 <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
>
--
Kind regards,
Viet
[-- Attachment #2: Type: text/html, Size: 5512 bytes --]
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2017-07-25 21:26 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-07-25 21:17 [Caml-list] Macro expansion for logging with call site (file & line) Viet Le
2017-07-25 21:26 ` Viet Le
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox