fix JOURNAL_STREAM detection to make sure its actually a sd service

This commit is contained in:
milo 2024-02-03 00:04:09 -05:00
parent d936d36a6a
commit 096cd62b2b
4 changed files with 12 additions and 4 deletions

View File

@ -14,12 +14,11 @@ let no_timestamp = Option.is_some (Sys.getenv_opt "LOG_NO_TIMESTAMP")
let no_namespace = Option.is_some (Sys.getenv_opt "LOG_NO_NAMESPACE")
let () =
match Sys.getenv_opt "JOURNAL_STREAM" with
| Some _ ->
if Logging.should_upgrade_to_journald () then
Logging.init_journald_writer ()
~min_level
| None ->
Logging.init_pretty_writer stderr
else
Logging.init_pretty_writer stdout
~min_level
~color:(not no_color)
~timestamp:(not no_timestamp)

View File

@ -9,6 +9,11 @@ type t = {
buf : Buffer.t;
}
let should_upgrade () =
let stderr = Unix.fstat Unix.stderr in
let dev_ino = Printf.sprintf "%d:%d" stderr.st_dev stderr.st_ino in
Sys.getenv_opt "JOURNAL_STREAM" = Some dev_ino
let make ?(path = default_socket_path) () = {
mutex = Mutex.create ();
sock_fd = Unix.socket PF_UNIX SOCK_DGRAM 0 ~cloexec:true;

View File

@ -107,3 +107,5 @@ let init_journald_writer
Journald.make () ?path |>
Journald.writer |>
add_writer ?min_level
let should_upgrade_to_journald = Journald.should_upgrade

View File

@ -36,3 +36,5 @@ val init_journald_writer :
?min_level:level ->
?path:string ->
unit -> unit
val should_upgrade_to_journald : unit -> bool