<commit_before>;;; -*- Gerbil -*-
;;; (C) vyzo
;;; Logging support
package: std

(import :gerbil/gambit/ports
        :gerbil/gambit/threads
        :gerbil/gambit/exceptions
        :std/sugar
        :std/format
        :std/srfi/19
        )
(export start-logger!
        debug warning
        log-error log-message)

(def current-logger
  (make-parameter #f))

(def (start-logger! (output (current-error-port)))
  (unless (current-logger)
    (let* (((values port own-port?)
            (cond
             ((output-port? output)
              (values output #f))
             ((string? output)
              (values (open-output-file [path: output append: #t]) #t))
             (else
              (error "Bad output; expected filename or port" output))))
           (srv (spawn logger-server port own-port?)))
      (current-logger srv)
      srv)))

(defstruct !log-message (source level e))

(def (logger-server port own-port?)
  (def (loop)
    (match (thread-receive)
      ((!log-message source level message)
       (fprintf port "~a [~a] ~a: ~a~n"
                (date->string (current-date) "~4")
                level source message)
       (force-output port)
       (loop))
      ('shutdown
       (void))<commit_msg>std/logger: start-logger! should always return a logger<commit_after>;;; -*- Gerbil -*-
;;; (C) vyzo
;;; Logging support
package: std

(import :gerbil/gambit/ports
        :gerbil/gambit/threads
        :gerbil/gambit/exceptions
        :std/sugar
        :std/format
        :std/srfi/19
        )
(export start-logger!
        debug warning
        log-error log-message)

(def current-logger
  (make-parameter #f))

(def (start-logger! (output (current-error-port)))
  (cond
   ((current-logger) => values)
   (else
    (let* (((values port own-port?)
            (cond
             ((output-port? output)
              (values output #f))
             ((string? output)
              (values (open-output-file [path: output append: #t]) #t))
             (else
              (error "Bad output; expected filename or port" output))))
           (srv (spawn logger-server port own-port?)))
      (current-logger srv)
      srv))))

(defstruct !log-message (source level e))

(def (logger-server port own-port?)
  (def (loop)
    (match (thread-receive)
      ((!log-message source level message)
       (fprintf port "~a [~a] ~a: ~a~n"
                (date->string (current-date) "~4")
                level source message)
       (force-output port)
       (loop))
      ('shutdown
       (void))
{'fraction_non_alphanumeric': 0.147534516765286, 'fraction_numerical': 0.002366863905325444, 'mean_word_length': 1.9976359338061467, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2310739', 'n_tokens_mistral': 801, 'n_tokens_neox': 791, 'n_words': 209}