123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110(*****************************************************************************)(* *)(* Open Source License *)(* Copyright (c) 2021 Nomadic Labs. <contact@nomadic-labs.com> *)(* Copyright (c) 2021 DaiLambda, Inc. <contact@dailambda.jp> *)(* *)(* Permission is hereby granted, free of charge, to any person obtaining a *)(* copy of this software and associated documentation files (the "Software"),*)(* to deal in the Software without restriction, including without limitation *)(* the rights to use, copy, modify, merge, publish, distribute, sublicense, *)(* and/or sell copies of the Software, and to permit persons to whom the *)(* Software is furnished to do so, subject to the following conditions: *)(* *)(* The above copyright notice and this permission notice shall be included *)(* in all copies or substantial portions of the Software. *)(* *)(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*)(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *)(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *)(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*)(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *)(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *)(* DEALINGS IN THE SOFTWARE. *)(* *)(*****************************************************************************)openInternal_event.Simpleletsection=["node";"protocol"]letmakenlevel=declare_1~section~name:(n^"_from_protocol")~msg:"{message}"~pp1:Format.pp_print_string~level("message",Data_encoding.string)letdebug=make"debug"Debugletinfo=make"info"Infoletnotice=make"notice"Noticeletwarning=make"warning"Warningleterror=make"error"Errorletfatal=make"fatal"Fatalletlogging_failure=declare_1~section~name:"logging_failure"~msg:"Failure to log a protocol message: {exc}"~pp1:Format.pp_print_string~level:Error("exc",Data_encoding.string)letmake_asynchronous_log_message_consumer()=letstream,push=Lwt_stream.create()inletalive=reftrueinLwt.dont_wait(fun()->Lwt_stream.iter_s(fun(level,s)->letopenLwt_syntaxin(* Pause to avoid interleaving of execution *)let*()=Lwt.pause()inLwt.catch(fun()->matchlevelwith|Internal_event.Debug->emitdebugs|Info->emitinfos|Notice->emitnotices|Warning->emitwarnings|Error->emiterrors|Fatal->emitfatals)(funexc->emitlogging_failure(Printexc.to_stringexc)))stream)(funexc->(* We ignore the exception because it can only be the exception raised
within the other exception handler which already attempted to log an
error. *)ignore(exc:exn);(* If the [iter_s] raises, then there are no more listeners on the stream
and we shouldn't push values on the stream. *)alive:=false);funlevels->if!alivethentrypush(Some(level,s))withLwt_stream.Closed->alive:=falseelse()letmake_log_message_consumer()levels=Lwt.dont_wait(fun()->matchlevelwith|Internal_event.Debug->emitdebugs|Info->emitinfos|Notice->emitnotices|Warning->emitwarnings|Error->emiterrors|Fatal->emitfatals)(funexc->Lwt.dont_wait(fun()->emitlogging_failure(Printexc.to_stringexc))(fun_exn->(* Ignoring: everything went wrong*)()))