123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196(******************************************************************)(* *)(* Open Source License *)(* Copyright (c) 2019 Dynamic Ledger Solutions, Inc. <contact@tezos.com> *)(* *)(* 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. *)(* *)(*****************************************************************************)openError_monadtypet={output:Lwt_unix.file_descr;format:[`One_per_line|`Netstring];(* Hopefully temporary hack to handle event which are emitted with
the non-cooperative log functions in `Legacy_logging`: *)lwt_bad_citizen_hack:Data_encoding.jsonlistref;level_at_least:Internal_event.Level.t;}lethostname=Option.value(Sys.getenv_opt"TEZOS_NODE_HOSTNAME")~default:(Unix.gethostname())type'eventwrapped={time_stamp:float;section:Internal_event.Section.t;event:'event;}letwraptime_stampsectionevent={time_stamp;section;event}letwrapped_encodingevent_encoding=letopenData_encodinginletv0=conv(fun{time_stamp;section;event}->(hostname,time_stamp,section,event))(fun(_,time_stamp,section,event)->{time_stamp;section;event})(obj4(req"hostname"string)(req"time_stamp"float)(req"section"Internal_event.Section.encoding)(req"event"event_encoding))inWith_version.(encoding~name:"fd-sink-item"(first_versionv0))moduleMake_sink(K:sigvalkind:[`Path|`Stdout|`Stderr]end):Internal_event.SINKwithtypet=t=structtypenonrect=tleturi_scheme=matchK.kindwith|`Path->"file-descriptor-path"|`Stdout->"file-descriptor-stdout"|`Stderr->"file-descriptor-stderr"letconfigureuri=letlevel_at_least=let(>??)=Option.bindinUri.get_query_paramuri"level-at-least">??Internal_event.Level.of_string|>Option.value~default:Internal_event.Level.defaultinletfail_parsingfmt=Format.kasprintf(failwith"Parsing URI: %s: %s"(Uri.to_stringuri))fmtin(matchUri.get_query_paramuri"format"with|Some"netstring"->return`Netstring|None|Some"one-per-line"->return`One_per_line|Someother->fail_parsing"Unknown format: %S"other)>>=?funformat->(matchK.kindwith|`Path->(letflagname=matchUri.get_query_paramurinamewith|Some"true"->true|_->falseinletwith_pid=flag"with-pid"inletfresh=flag"fresh"in(matchUri.get_query_paramuri"chmod"with|Somen->(tryreturn(int_of_stringn)with_->fail_parsing"Access-rights parameter should be an integer: %S"n)|None->return0o600)>>=?funrights->matchUri.pathuriwith|""|"/"->fail_parsing"Missing path configuration."|path->letfixed_path=ifwith_pidthenletext=Filename.extensionpathinletchopped=ifext=""thenpathelseFilename.chop_extensionpathinFmt.strf"%s-%d%s"chopped(Unix.getpid())extelsepathinprotect(fun()->Lwt_unix.(letflags=[O_WRONLY;O_CREAT]@iffreshthen[O_TRUNC]else[O_APPEND]inopenfilefixed_pathflagsrights)>>=funfd->returnfd))|`Stdout->returnLwt_unix.stdout|`Stderr->returnLwt_unix.stderr)>>=?funoutput->lett={output;lwt_bad_citizen_hack=ref[];level_at_least;format}inreturntletwrite_mutex=Lwt_mutex.create()letoutput_oneoutputformatevent_json=letto_write=matchformatwith|`One_per_line->Ezjsonm.value_to_string~minify:trueevent_json^"\n"|`Netstring->letbytes=Ezjsonm.value_to_string~minify:trueevent_jsoninFmt.str"%d:%s,"(String.lengthbytes)bytesinprotect(fun()->Lwt_mutex.with_lockwrite_mutex(fun()->Lwt_utils_unix.write_stringoutputto_write)>>=fun()->return_unit)lethandle(typea){output;lwt_bad_citizen_hack;level_at_least;format;_}m?(section=Internal_event.Section.empty)(v:unit->a)=letmoduleM=(valm:Internal_event.EVENT_DEFINITIONwithtypet=a)inletnow=Unix.gettimeofday()inletforced_event=v()inletlevel=M.levelforced_eventinifInternal_event.Level.comparelevellevel_at_least>=0then(letwrapped_event=wrapnowsectionforced_eventinletevent_json=Data_encoding.Json.construct(wrapped_encodingM.encoding)wrapped_eventinlwt_bad_citizen_hack:=event_json::!lwt_bad_citizen_hack;output_oneoutputformatevent_json>>=function|Error[Exn(Unix.Unix_error(Unix.EBADF,_,_))]->(* The file descriptor was closed before the event arrived,
ignore it. *)return_unit|Error_aserr->Lwt.returnerr|Ok()->lwt_bad_citizen_hack:=List.filter((=)event_json)!lwt_bad_citizen_hack;return_unit)elsereturn_unitletclose{lwt_bad_citizen_hack;output;format;_}=List.iter_es(funevent_json->output_oneoutputformatevent_json)!lwt_bad_citizen_hack>>=?fun()->Lwt_unix.closeoutput>>=fun()->return_unitendmoduleSink_implementation_path=Make_sink(structletkind=`Pathend)moduleSink_implementation_stdout=Make_sink(structletkind=`Stdoutend)moduleSink_implementation_stderr=Make_sink(structletkind=`Stderrend)let()=Internal_event.All_sinks.register(moduleSink_implementation_path)let()=Internal_event.All_sinks.register(moduleSink_implementation_stdout)let()=Internal_event.All_sinks.register(moduleSink_implementation_stderr)