Module Brr.Console
Browser console.
See Console
. Take a few minutes to understand this.
val get : unit -> t
get ()
is the console object on which the functions below act. Initially this isG.console
.
val set : t -> unit
set o
sets the console object too
.
val clear : unit -> unit
clear ()
clears the console.
Log functions
type 'a msgr
= 'a -> msg
The type for functions turning values of type
'a
into log messages.
val msg : 'a msgr
msg v
is[v]
.
type log
= msg -> unit
The type for log functions.
Log messages rebind OCaml's list syntax. This allows to write heterogeneous logging statements concisely.
let () = Console.(log [1; 2.; true; Jv.true'; str "🐫"; G.navigator])
The console logs JavaScript values. For OCaml values this means that their
js_of_ocaml
representation is logged; see the FFI manual for details. Most OCaml values behindBrr
types are however direct JavaScript values and logging them as is will be valuable. For other values you can use thestr
function which invokes the JavaScripttoString
method on the value. It works on OCaml strings and is mostly equivalent and shorter than callingJstr.v
before logging them.In the JavaScript
console
API, if the first argument is a JavaScript string it can have formatting specifications. Just remember this should be a JavaScript string, so wrap OCaml literals bystr
orJstr.v
:let () = Console.(log [str "This is:\n%o\n the navigator"; G.navigator])
val str : 'a -> Jstr.t
str v
is the result of invoking the JavaScripttoString
method on the representation ofv
. Ifv
isJv.null
andJv.undefined
a string representing them is directly returned.
Result
logging
val log_result : ?ok:'a msgr -> ?error:'b msgr -> ('a, 'b) Stdlib.result -> ('a, 'b) Stdlib.result
val log_if_error : ?l:log -> ?error_msg:'b msgr -> use:'a -> ('a, 'b) Stdlib.result -> 'a
log_if_error ~l ~error_msg ~use r
isv
ifr
isOk v
anduse
ifr
isError e
. In this casee
is logged withl
(defaults toerror
) anderror_msg
(defaults tostr e
).
val log_if_error' : ?l:log -> ?error_msg:'b msgr -> use:'a -> ('a, 'b) Stdlib.result -> ('a, 'b) Stdlib.result
log_if_error'
islog_if_error
wrapped byResult
.ok.
Levelled logging
val trace : log
trace m
logsm
with no specific level but with a stack trace, likeerror
andwarn
do.
Asserting and dumping
val dir : 'a -> unit
dir o
logs a listing of the properties of the objecto
– this explains the difference withConsole.(log [o])
.
val table : ?cols:Jstr.t list -> 'a -> unit
table v
outputsv
as tabular data. Ifcols
is specified only the specified properties are printed.
Grouping
val group : ?closed:bool -> log
group ~closed msg
logsmsg
and pushes a new inline group in the console. This indents messages untilgroup_end
is called. Ifclosed
istrue
(defaults tofalse
) the group's content is hidden behind a disclosure button.
val group_end : unit -> unit
group_end ()
pops the last inline group.
Counting
val count : Jstr.t -> unit
count label
logslabel
with the number of timescount label
was called.