123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201(*****************************************************************************)(* *)(* Open Source License *)(* Copyright (c) 2018 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. *)(* *)(*****************************************************************************)openLwt.SyntaxmoduleOutput=structtypet=|Null|Stdout|Stderr|Fileofstring|SyslogofLwt_log.syslog_facilityletto_string:t->string=function|Null->"/dev/null"|Stdout->"stdout"|Stderr->"stderr"|Filefp->fp|Syslog`Auth->"syslog:auth"|Syslog`Authpriv->"syslog:authpriv"|Syslog`Cron->"syslog:cron"|Syslog`Daemon->"syslog:daemon"|Syslog`FTP->"syslog:ftp"|Syslog`Kernel->"syslog:kernel"|Syslog`Local0->"syslog:local0"|Syslog`Local1->"syslog:local1"|Syslog`Local2->"syslog:local2"|Syslog`Local3->"syslog:local3"|Syslog`Local4->"syslog:local4"|Syslog`Local5->"syslog:local5"|Syslog`Local6->"syslog:local6"|Syslog`Local7->"syslog:local7"|Syslog`LPR->"syslog:lpr"|Syslog`Mail->"syslog:mail"|Syslog`News->"syslog:news"|Syslog`Syslog->"syslog:syslog"|Syslog`User->"syslog:user"|Syslog`UUCP->"syslog:uucp"|Syslog`NTP->"syslog:ntp"|Syslog`Security->"syslog:security"|Syslog`Console->"syslog:console"letof_string:string->t=function|"/dev/null"|"null"->Null|"stdout"->Stdout|"stderr"->Stderr|"syslog:auth"->Syslog`Auth|"syslog:authpriv"->Syslog`Authpriv|"syslog:cron"->Syslog`Cron|"syslog:daemon"->Syslog`Daemon|"syslog:ftp"->Syslog`FTP|"syslog:kernel"->Syslog`Kernel|"syslog:local0"->Syslog`Local0|"syslog:local1"->Syslog`Local1|"syslog:local2"->Syslog`Local2|"syslog:local3"->Syslog`Local3|"syslog:local4"->Syslog`Local4|"syslog:local5"->Syslog`Local5|"syslog:local6"->Syslog`Local6|"syslog:local7"->Syslog`Local7|"syslog:lpr"->Syslog`LPR|"syslog:mail"->Syslog`Mail|"syslog:news"->Syslog`News|"syslog:syslog"->Syslog`Syslog|"syslog:user"->Syslog`User|"syslog:uucp"->Syslog`UUCP|"syslog:ntp"->Syslog`NTP|"syslog:security"->Syslog`Security|"syslog:console"->Syslog`Console(* | s when start_with "syslog:" FIXME error or warning. *)|fp->(* TODO check absolute path *)Filefpletencoding=letopenData_encodinginconvto_stringof_stringstringletof_stringstr=trySome(Data_encoding.Json.destructencoding(`Stringstr))with_->Noneletto_stringoutput=matchData_encoding.Json.constructencodingoutputwith|`Stringres->res|#Data_encoding.json->assertfalseletppfmtoutput=Format.fprintffmt"%s"(to_stringoutput)endletdefault_template="$(date).$(milliseconds) - $(section): $(message)"typecfg={output:Output.t;default_level:Internal_event.level;rules:stringoption;template:Lwt_log_core.template;}letcreate_cfg?(output=Output.Stderr)?(default_level=Internal_event.Notice)?rules?(template=default_template)()={output;default_level;rules;template}letdefault_cfg=create_cfg()letcfg_encoding=letopenData_encodinginconv(fun{output;default_level;rules;template}->(output,default_level,rules,template))(fun(output,default_level,rules,template)->{output;default_level;rules;template})(obj4(dft"output"~description:"Output for the logging function. Either 'stdout', 'stderr' or the \
name of a log file ."Output.encodingdefault_cfg.output)(dft"level"~description:"Verbosity level: one of 'fatal', 'error', 'warn','notice', \
'info', 'debug'."Internal_event.Level.encodingdefault_cfg.default_level)(opt"rules"~description:"Fine-grained logging instructions. Same format as described in \
`octez-node run --help`, DEBUG section. In the example below, \
sections 'p2p' and all sections starting by 'client' will have \
their messages logged up to the debug level, whereas the rest of \
log sections will be logged up to the notice level."string)(dft"template"~description:"Format for the log file, see \
https://github.com/ocsigen/lwt_log/blob/1.1.1/src/core/lwt_log_core.mli#L229."stringdefault_cfg.template))letinit?(template=default_template)output=letopenOutputinlet*logger=matchoutputwith|Stderr->Lwt.return@@Lwt_log.channel~template~close_mode:`Keep~channel:Lwt_io.stderr()|Stdout->Lwt.return@@Lwt_log.channel~template~close_mode:`Keep~channel:Lwt_io.stdout()|Filefile_name->Lwt_log.file~file_name~template()|Null->Lwt.return@@Lwt_log.null|Syslogfacility->Lwt.return@@Lwt_log.syslog~template~facility()inLwt_log.default:=logger;Lwt.return_unitletinitialize?(cfg=default_cfg)()=Lwt_log_core.add_rule"*"(Internal_event.Level.to_lwt_logcfg.default_level);letorigin,rules=Level_config_rules.find_log_rulescfg.rulesinlet*()=matchruleswith|None->Lwt.return_unit|Somerules->(tryLwt_log_core.load_rulesrules~fail_on_error:true;Lwt.return_unitwith_->Printf.ksprintfLwt.fail_with"Incorrect log rules defined in %s"origin)ininit~template:cfg.templatecfg.output