From 041b8fe943747ed3b93d79c98cf89368705c3425 Mon Sep 17 00:00:00 2001 From: David Allsopp Date: Sat, 11 Aug 2018 15:02:05 +0100 Subject: [PATCH] Add Syslog_message.encode_local When communicating with syslog locally, the syslog message should not include the hostname. --- src/syslog_message.ml | 14 ++++++++++++-- src/syslog_message.mli | 4 ++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/syslog_message.ml b/src/syslog_message.ml index 7975241..7ca2d59 100644 --- a/src/syslog_message.ml +++ b/src/syslog_message.ml @@ -235,17 +235,27 @@ let to_string msg = let pp ppf msg = Format.pp_print_string ppf (to_string msg) -let encode ?(len=1024) msg = +let encode_gen encode ?(len=1024) msg = let facse = int_of_facility msg.facility * 8 + int_of_severity msg.severity and ts = Rfc3164_Timestamp.encode msg.timestamp in - let msgstr = Printf.sprintf "<%d>%s %s %s" facse ts msg.hostname msg.message + let msgstr = encode facse ts msg.hostname msg.message in if len > 0 && String.length msgstr > len then String.with_range ~first:0 ~len:len msgstr else msgstr +let encode ?len msg = + let encode facse ts hostname msg = + Printf.sprintf "<%d>%s %s %s" facse ts hostname msg + in + encode_gen encode ?len msg + +let encode_local ?len msg = + let encode facse ts _ msg = Printf.sprintf "<%d>%s %s" facse ts msg in + encode_gen encode ?len msg + let parse_priority_value s = let l = String.length s in if String.get s 0 <> '<' then diff --git a/src/syslog_message.mli b/src/syslog_message.mli index e5b4276..c253ff7 100644 --- a/src/syslog_message.mli +++ b/src/syslog_message.mli @@ -94,6 +94,10 @@ val decode : ctx:ctx -> string -> t option truncated. *) val encode : ?len:int -> t -> string +(** [encode_local ~len t] behaves as {!encode} except that the message is + formatted for sending to the local syslog daemon (e.g. on [/dev/log]). *) +val encode_local : ?len:int -> t -> string + (** RFC 3164 Timestamps *) module Rfc3164_Timestamp : sig