Subject: [PATCH 3/3] services: mcron: Add log-file and date-format fields.

From: Bruno Victal <[email-masked]>
Date: Mon, 13 Mar 2023 19:30:50 +0000

* doc/guix.texi (Scheduled Job Execution): Document it.
* gnu/services/mcron.scm (mcron-configuration)[log-file, date-format]: New field.
(mcron-shepherd-services): Add log-file and date-format support.
Use file-append instead of string-append.
---
 doc/guix.texi          |  6 ++++++
 gnu/services/mcron.scm | 31 +++++++++++++++++++++++++------
 2 files changed, 31 insertions(+), 6 deletions(-)

diff --git a/doc/guix.texi b/doc/guix.texi
index 45d96b8fbb..6d47d36d22 100644
--- a/doc/guix.texi
+++ b/doc/guix.texi
@@ -19347,12 +19347,18 @@ Scheduled Job Execution
 @item @code{log?} (default: @code{#t}) (type: boolean)
 Log messages to standard output.
 
+@item @code{log-file} (default: @code{"/var/log/mcron.log"}) (type: string)
+Log file location.
+
 @item @code{log-format} (default: @code{"~1@@*~a ~a: ~a~%"}) (type: string)
 @code{(ice-9 format)} format string for log messages.  The default value
 produces messages like @samp{@var{pid} @var{name}: @var{message}}
 (@pxref{Invoking mcron, Invoking,, mcron,GNU@tie{}mcron}).  Each message
 is also prefixed by a timestamp by GNU Shepherd.
 
+@item @code{date-format} (type: maybe-string)
+@code{(srfi srfi-19)} format string for date.
+
 @end table
 @end deftp
 @c %end of fragment
diff --git a/gnu/services/mcron.scm b/gnu/services/mcron.scm
index 9f3afecf62..dac213358c 100644
--- a/gnu/services/mcron.scm
+++ b/gnu/services/mcron.scm
@@ -1,6 +1,7 @@
 ;;; GNU Guix --- Functional package management for GNU
 ;;; Copyright © 2016, 2017, 2018, 2019, 2020 Ludovic Courtès <[email-masked]>
 ;;; Copyright © 2022 Maxim Cournoyer <[email-masked]>
+;;; Copyright © 2023 Bruno Victal <[email-masked]>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -33,7 +34,9 @@ (define-module (gnu services mcron)
             mcron-configuration-mcron
             mcron-configuration-jobs
             mcron-configuration-log?
+            mcron-configuration-log-file
             mcron-configuration-log-format
+            mcron-configuration-date-format
 
             mcron-service-type))
 
@@ -55,6 +58,8 @@ (define-module (gnu services mcron)
 (define list-of-gexps?
   (list-of gexp?))
 
+(define-maybe/no-serialization string)
+
 (define-configuration/no-serialization mcron-configuration
   (mcron
    (file-like mcron)
@@ -70,12 +75,20 @@ (define-configuration/no-serialization mcron-configuration
    (boolean #t)
    "Log messages to standard output.")
 
+  (log-file
+   (string "/var/log/mcron.log")
+   "Log file location.")
+
   (log-format
    (string "~1@*~a ~a: ~a~%")
    "@code{(ice-9 format)} format string for log messages.  The default value
 produces messages like @samp{@var{pid} @var{name}: @var{message}}
 (@pxref{Invoking mcron, Invoking,, mcron, GNU@tie{}mcron}).
-Each message is also prefixed by a timestamp by GNU Shepherd."))
+Each message is also prefixed by a timestamp by GNU Shepherd.")
+
+  (date-format
+   maybe-string
+   "@code{(srfi srfi-19)} format string for date."))
 
 (define (job-files mcron jobs)
   "Return a list of file-like object for JOBS, a list of gexps."
@@ -144,7 +157,9 @@ (define (shepherd-schedule-action mcron files)
                (loop)))))))))
 
 (define (mcron-shepherd-services config)
-  (match-record config <mcron-configuration> (mcron jobs log? log-format)
+  (match-record config <mcron-configuration> (mcron jobs
+                                              log? log-file log-format
+                                              date-format)
     (if (eq? jobs '())
         '()  ; nothing to do
         (let ((files (job-files mcron jobs)))
@@ -158,10 +173,14 @@ (define (mcron-shepherd-services config)
                             (ice-9 match)
                             ,@%default-modules))
                  (start #~(make-forkexec-constructor
-                           (list (string-append #$mcron "/bin/mcron")
+                           (list #$(file-append mcron "/bin/mcron")
                                  #$@(if log?
-                                        #~("--log" "--log-format" #$log-format)
-                                        #~())
+                                        `("--log" "--log-format" ,log-format
+                                          ,@(if (maybe-value-set? date-format)
+                                                (list "--date-format"
+                                                      date-format)
+                                                '()))
+                                        '())
                                  #$@files)
 
                            ;; Disable auto-compilation of the job files and
@@ -172,7 +191,7 @@ (define (mcron-shepherd-services config)
                                   (remove (cut string-prefix? "PATH=" <>)
                                           (environ)))
 
-                           #:log-file "/var/log/mcron.log"))
+                           #:log-file #$log-file))
                  (stop #~(make-kill-destructor))
                  (actions
                   (list (shepherd-schedule-action mcron files)))))))))
-- 
2.39.1