[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[shepherd] 01/03: system-log: Add #:date-format.
From: |
Ludovic Courtès |
Subject: |
[shepherd] 01/03: system-log: Add #:date-format. |
Date: |
Sat, 9 Nov 2024 16:34:51 -0500 (EST) |
civodul pushed a commit to branch devel
in repository shepherd.
commit b38e89255b75293f7a21971e6cf57a1f19186ed2
Author: Ludovic Courtès <ludo@gnu.org>
AuthorDate: Sat Nov 9 17:02:06 2024 +0100
system-log: Add #:date-format.
This allows users to have traditional syslogd format, should they
prefer it.
* modules/shepherd/logger.scm (log-line): Add #:date-format and honor
it.
* modules/shepherd/service/system-log.scm (log-dispatcher):
Add #:date-format and pass it to ‘log-line’.
(spawn-log-dispatcher): Add #:date-format and pass it to
‘log-dispatcher’.
(system-log-service): Add #:date-format and pass it to
‘spawn-log-dispatcher’.
* doc/shepherd.texi (System Log Service): Document it.
---
doc/shepherd.texi | 4 ++++
modules/shepherd/logger.scm | 10 +++++-----
modules/shepherd/service/system-log.scm | 28 ++++++++++++++++++++--------
3 files changed, 29 insertions(+), 13 deletions(-)
diff --git a/doc/shepherd.texi b/doc/shepherd.texi
index b164fe8..da9a57a 100644
--- a/doc/shepherd.texi
+++ b/doc/shepherd.texi
@@ -2029,6 +2029,7 @@ host (see below).
[#:requirement '()] @
[#:kernel-log-file (kernel-log-file)] @
[#:message-destination (default-message-destination-procedure)] @
+ [#:date-format default-logfile-date-format] @
[#:history-size (default-log-history-size)] @
[#:max-silent-time (default-max-silent-time)]
Return the system log service (@dfn{syslogd}) with the given
@@ -2040,6 +2041,9 @@ it also reads messages from @code{#:kernel-log-file},
which defaults to
Log messages are passed to @var{message-destination}, a one-argument procedure
that must return the list of files to write it to. Write a mark to log files
when no message has been logged for more than @var{max-silent-time} seconds.
+Timestamps in log files are formatted according to @var{date-format}, a format
+string for @code{strftime}, including delimiting space---e.g., @code{\"%c \"}
+for a format identical to that of traditional syslogd implementations.
Keep up to @var{history-size} messages in memory for the purposes of allowing
users to view recent messages without opening various files.
diff --git a/modules/shepherd/logger.scm b/modules/shepherd/logger.scm
index 39f05ae..f739dbe 100644
--- a/modules/shepherd/logger.scm
+++ b/modules/shepherd/logger.scm
@@ -100,11 +100,11 @@ first message received."
(get-operation channel1)
(get-operation channel2))))
-(define* (log-line line output #:optional (now (current-time)))
- "Write @var{line} to @var{output} with @var{now} as its timestamp; return
-@var{now}."
- (let ((prefix (strftime default-logfile-date-format
- (localtime now))))
+(define* (log-line line output #:optional (now (current-time))
+ #:key (date-format default-logfile-date-format))
+ "Write @var{line} to @var{output} with @var{now} as its timestamp,
+formatting according to @var{date-format}; return @var{now}."
+ (let ((prefix (strftime date-format (localtime now))))
;; Avoid (ice-9 format) to reduce heap allocations.
(put-string output prefix)
(put-string output line)
diff --git a/modules/shepherd/service/system-log.scm
b/modules/shepherd/service/system-log.scm
index 89fad46..c7e1d6b 100644
--- a/modules/shepherd/service/system-log.scm
+++ b/modules/shepherd/service/system-log.scm
@@ -255,7 +255,8 @@ and passing them to @var{dispatcher}."
(define* (log-dispatcher channel message-destination
#:key
max-silent-time
- (history-size (default-log-history-size)))
+ (history-size (default-log-history-size))
+ (date-format default-logfile-date-format))
"Dispatch system log messages received on @var{channel} to log files. Call
@var{message-destination} for each system log message to determine the
destination file(s)."
@@ -285,7 +286,8 @@ in message destination procedure: "))
(catch 'system-error
(lambda ()
(let ((port (open-log-file file)))
- (log-line line port now)
+ (log-line line port now
+ #:date-format date-format)
(vhash-cons file port ports)))
(lambda args
(local-output
@@ -293,7 +295,8 @@ in message destination procedure: "))
file (strerror (system-error-errno args)))
ports)))
((_ . port)
- (log-line line port now)
+ (log-line line port now
+ #:date-format date-format)
ports)))
ports
files)
@@ -301,7 +304,8 @@ in message destination procedure: "))
('timeout
;; Write a mark to all the files indiscriminately.
(vhash-fold (lambda (file port _)
- (log-line %heartbeat-message port))
+ (log-line %heartbeat-message port
+ #:date-format date-format))
#t
ports)
(loop ports messages))
@@ -342,14 +346,16 @@ system log file '~a'.")
(define* (spawn-log-dispatcher message-destination
#:key
max-silent-time
- (history-size (default-log-history-size)))
+ (history-size (default-log-history-size))
+ (date-format default-logfile-date-format))
"Spawn the log dispatcher, responsible for writing system log messages to
the file(s) returned by @var{message-destination} for each message. Keep up
to @var{history-size} messages in a ring buffer."
(let ((channel (make-channel)))
(spawn-fiber (log-dispatcher channel message-destination
#:max-silent-time max-silent-time
- #:history-size history-size))
+ #:history-size history-size
+ #:date-format date-format))
channel))
(define (default-message-destination-procedure)
@@ -418,7 +424,8 @@ default destination to log it to."
(message-destination
(default-message-destination-procedure))
(history-size (default-log-history-size))
- (max-silent-time (default-max-silent-time)))
+ (max-silent-time (default-max-silent-time))
+ (date-format default-logfile-date-format))
"Return the system log service (@dfn{syslogd}) with the given
@var{provision} and @var{requirement} (lists of symbols). The service accepts
connections on @var{sources}, a list of @code{<endpoint>} objects; optionally
@@ -428,6 +435,9 @@ it also reads messages from @code{#:kernel-log-file}, which
defaults to
Log messages are passed to @var{message-destination}, a one-argument procedure
that must return the list of files to write it to. Write a mark to log files
when no message has been logged for more than @var{max-silent-time} seconds.
+Timestamps in log files are formatted according to @var{date-format}, a format
+string for @code{strftime}, including delimiting space---e.g., @code{\"%c \"}
+for a format identical to that of traditional syslogd implementations.
Keep up to @var{history-size} messages in memory for the purposes of allowing
users to view recent messages without opening various files."
@@ -447,7 +457,9 @@ users to view recent messages without opening various
files."
#:max-silent-time
max-silent-time
#:history-size
- history-size)))
+ history-size
+ #:date-format
+ date-format)))
(register-service-logger this-system-log dispatcher)
(spawn-fiber
(lambda ()