123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384open!Coreopen!Async_kernelopen!Import(* This module is intended to be blocking, so we explicitly don't open [Async] at the top.
*)moduleOutput=structtypet=Message.t->unitletcreate=Fn.idletwriteprintmsg=print(Message.to_write_only_textmsg)letstdout=write(Core.Printf.printf"%s\n%!")letstderr=write(Core.Printf.eprintf"%s\n%!")endletlevel:Level.tref=ref`Infoletwrite=refOutput.stderrlettime_source=ref(ifPpx_inline_test_lib.am_runningthenSynchronous_time_source.(read_only(create~now:Time_ns.epoch()))elseSynchronous_time_source.wall_clock());;lettransform=refNoneletset_levell=level:=lletlevel()=!levelletset_outputoutput=write:=outputletset_time_sourcets=time_source:=tsletset_transformf=transform:=fletwritemsg=ifAsync_unix.Scheduler.is_running()thenfailwith"Log.Global.Blocking function called after scheduler started";letmsg=match!transformwith|None->msg|Somef->fmsgin!writemsg;;letwould_logmsg_level=(* we don't need to test for empty output here because the interface only allows one
Output.t and ensures that it is always set to something. *)Level.as_or_more_verbose_than~log_level:(level())~msg_level;;letcreate_message?level?time?tagsmsg=lettime=matchtimewith|None->Synchronous_time_source.now!time_source|>Time_ns.to_time_float_round_nearest|Sometime->timeinMessage.create?level?tagsmsg~time;;letgen?level:msg_level?time?tagsk=ksprintf(funmsg->ifwould_logmsg_levelthen(letmsg=`Stringmsginwrite(create_message?level:msg_level?time?tagsmsg)))k;;letprintf?level?time?tagsk=gen?level?time?tagskletraw?time?tagsk=gen?time?tagskletdebug?time?tagsk=gen~level:`Debug?time?tagskletinfo?time?tagsk=gen~level:`Info?time?tagskleterror?time?tagsk=gen~level:`Error?time?tagskletsexp?level?time?tagssexp=ifwould_loglevelthenwrite(create_message?level?time?tags(`Sexpsexp));;letraw_s?time?tagsthe_sexp=sexp?time?tagsthe_sexpletdebug_s?time?tagsthe_sexp=sexp~level:`Debug?time?tagsthe_sexpletinfo_s?time?tagsthe_sexp=sexp~level:`Info?time?tagsthe_sexpleterror_s?time?tagsthe_sexp=sexp~level:`Error?time?tagsthe_sexp