dbuenzli / logs

Logging infrastructure for OCaml
http://erratique.ch/software/logs
ISC License
86 stars 19 forks source link

Usage with js_of_ocaml #30

Closed dboris closed 4 years ago

dboris commented 4 years ago

Here's a minimal example:

open Js_of_ocaml
open Js_of_ocaml_lwt

let on_btn_click _ _ =
    Logs_lwt.debug (fun m -> m "Clicked")

let main _ =
    let btn =
        Js.Opt.get
            (Dom_html.document##getElementById (Js.string "btn"))
            (fun () -> assert false)
    in
    Lwt_js_events.(async (fun () -> clicks btn on_btn_click));
    Js._false

let () =
    Logs.set_reporter (Logs_browser.console_reporter ());
    Logs.set_level (Some Logs.Debug);
    Dom_html.window##.onload := Dom_html.handler main

Compiles fine. When I run this in the browser, I get an error:

Uncaught TypeError: Cannot read property '1' of undefined
    at test.ml:18
    at bigarray.ml:0

Seems to be in this line:

Logs.set_reporter (Logs_browser.console_reporter ())

Am I doing something wrong?

dbuenzli commented 4 years ago

The example from the distribution runs fine here. If you want me to dig this further please post a full build system for your example somwhere (e.g. in a gist).

dbuenzli commented 4 years ago

Also I don't understand where that bigarray.ml is coming from please and could you try without lwt first.

dboris commented 4 years ago

Here's a repo with the example: https://github.com/dboris/logs-test

dbuenzli commented 4 years ago

Thanks @dboris.

I suspect there's something wrong with dune and/or js_of_ocaml separate compilation. What we see here may be due to an OCaml runtime system initialization error because some stuff is not being linked in, you may want to open an issue there.

It became clear when I did:

> dune build --profile=release test.bc.js
File "_none_", line 1:
Error: Required module `Logs_browser' is unavailable

Basically you forgot to specify logs.browser and log.lwt in your dune file:

> git diff
diff --git a/dune b/dune
index 69f095d..4afe763 100644
--- a/dune
+++ b/dune
@@ -1,4 +1,4 @@
 (executable
  (name test)
- (libraries js_of_ocaml-lwt logs)
+ (libraries js_of_ocaml-lwt logs logs.browser logs.lwt)
  (preprocess (pps js_of_ocaml-ppx)))
\ No newline at end of file

Please make sure to report this to dune so that they can see if something better can be done.

dboris commented 4 years ago

Thanks, will do. What is a good way to figure what package needs to be specified in the dune file? Neither opam show, nor odig doc mention logs.browser and logs.lwt. I can see them in the package META file, but is there a better way?

dbuenzli commented 4 years ago

I'd say there's no good way at the moment ocamlfind list will however list the packages/libraries which might give hints.

The problem here is that the libraries are all installed in the same directory so specifying e.g. only logs adds -I LIBDIR/logs and allows to see the cmi of the other libraries so separate compilation works.

If we move to https://github.com/ocaml/RFCs/pull/7 where each library has to live in its own directory. Then you would have gotten an undefined Logs_browser at compilation time which would have helped to specify the right libraries.

I bet the problem here is that when you use separate compilation in jsoo the linker becomes your html file so it doesn't check if everything is here like when you link an executable.

I'm not sure if something could maybe added to jsoo_link for that /cc @hhugo.