Skip to content

Commit

Permalink
Add Syslog_message.encode_local
Browse files Browse the repository at this point in the history
When communicating with syslog locally, the syslog message should not
include the hostname.
  • Loading branch information
dra27 committed Aug 11, 2018
1 parent 9e41963 commit 041b8fe
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/syslog_message.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions src/syslog_message.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 041b8fe

Please sign in to comment.