Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Syslog_message.encode_local #17

Merged
merged 1 commit into from
Aug 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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