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