123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101openLwt.InfixopenLogs_syslog_lwt__Logs_syslog_lwt_commonopenLogs_sysloglettcp_tls_reporter?hostnameip?(port=6514)~cacert~cert~priv_key?(truncate=0)?(framing=`Null)?facility()=letsa=Lwt_unix.ADDR_INET(ip,port)inlettls=refNoneinletm=Lwt_mutex.create()inX509_lwt.private_of_pems~cert~priv_key>>=funpriv->X509_lwt.authenticator(`Ca_filecacert)>>=funauthenticator->letconf=Tls.Config.client~authenticator~certificates:(`Singlepriv)()in(matchhostnamewith|Somex->Lwt.returnx|None->Lwt_unix.gethostname())>>=funhost->letconnect()=letsock=Lwt_unix.(socketPF_INETSOCK_STREAM0)inLwt_unix.(setsockoptsockSO_REUSEADDRtrue);Lwt_unix.(setsockoptsockSO_KEEPALIVEtrue);Lwt.catch(fun()->Lwt_unix.connectsocksa>>=fun()->Tls_lwt.Unix.client_of_fdconfsock>|=funt->tls:=Somet;Ok())(function|Unix.Unix_error(e,f,_)->leterr=Printf.sprintf"error %s in function %s while connecting to %s:%d"(Unix.error_messagee)f(Unix.string_of_inet_addrip)portinLwt.return@@Errorerr|Tls_lwt.Tls_failuref->leterr=Tls.Engine.string_of_failurefinLwt.return@@Error(Printf.sprintf"TLS failure %s"err)|exn->Lwt.failexn)inletreconnectkmsg=Lwt_mutex.lockm>>=fun()->(match!tlswith|None->connect()|Some_->Lwt.return(Ok()))>>=function|Ok()->Lwt_mutex.unlockm;kmsg|Errore->Printf.eprintf"%s while sending syslog message\n%s %s\n"e(Ptime.to_rfc3339(Ptime_clock.now()))msg;Lwt_mutex.unlockm;Lwt.return_unitinconnect()>>=function|Errore->Lwt.return(Errore)|Ok()->letrecsendomsg=match!tlswith|None->reconnectsendomsg|Somet->letmsg=Cstruct.of_string(frame_messageomsgframing)inLwt.catch(fun()->Tls_lwt.Unix.writetmsg)(function|Unix.Unix_error(e,f,_)->tls:=None;leterr=Unix.error_messageeinPrintf.eprintf"error %s in function %s, reconnecting\n"errf;Lwt.catch(fun()->Tls_lwt.Unix.closet)(fun_->Lwt.return_unit)>>=fun()->reconnectsendomsg|Tls_lwt.Tls_failuref->tls:=None;Printf.eprintf"TLS error %s\n"(Tls.Engine.string_of_failuref);Lwt.catch(fun()->Tls_lwt.Unix.closet)(fun_->Lwt.return_unit)>>=fun()->reconnectsendomsg|exn->Lwt.failexn)inat_exit(fun()->match!tlswith|None->()|Sometls->Lwt.async(fun()->Tls_lwt.Unix.closetls));Lwt.return(Ok(syslog_report_commonfacilityhosttruncatePtime_clock.nowsendSyslog_message.encode))(*
let main () =
let lo = Unix.inet_addr_of_string "127.0.0.1" in
tcp_tls_reporter lo
~cacert:"cacert.pem" ~cert:"client.pem" ~priv_key:"client.key" ()
>>= function
| Error e -> print_endline e ; Lwt.return_unit
| Ok r ->
Logs.set_reporter r ;
Logs.set_level ~all:true (Some Logs.Debug) ;
Logs_lwt.warn (fun l -> l "foobar") >>= fun () ->
Logs_lwt.err (fun l -> l "bar foofoobar") >>= fun () ->
Logs_lwt.info (fun l -> l "foofoobar") >>= fun () ->
Logs_lwt.debug (fun l -> l "debug foofoobar")
let _ = Lwt_main.run (main ())
*)