You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
pollen/debug.rkt

78 lines
2.8 KiB
Racket

11 years ago
#lang racket/base
(require (for-syntax racket/base racket/syntax))
(require racket/date racket/string)
(require sugar/debug sugar/define)
11 years ago
11 years ago
(provide (all-from-out sugar/debug))
11 years ago
; todo: contracts, tests, docs
11 years ago
; debug utilities
(define months (list "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"))
11 years ago
(define last-message-time #f)
(define (seconds-since-last-message)
(define now (current-seconds))
(define then last-message-time)
(set! last-message-time now)
(if then
11 years ago
(- now then)
11 years ago
"0"))
11 years ago
11 years ago
(define (zero-fill str count)
(set! str (format "~a" str))
11 years ago
(if (> (string-length str) count)
str
(string-append (make-string (- count (string-length str)) #\0) str)))
11 years ago
(define+provide (make-datestamp)
11 years ago
(define date (current-date))
(define date-fields (map (λ(x) (zero-fill x 2))
(list
(date-day date)
(list-ref months (sub1 (date-month date)))
(date-year date)
)))
(string-join date-fields "-"))
11 years ago
(define+provide (make-timestamp)
11 years ago
(define date (current-date))
(define time-fields (map (λ(x) (zero-fill x 2))
(list
; (date-day date)
; (list-ref months (sub1 (date-month date)))
(if (<= (date-hour date) 12)
(date-hour date) ; am hours + noon hour
(modulo (date-hour date) 12)) ; pm hours after noon hour
(date-minute date)
11 years ago
(date-second date))))
11 years ago
(string-append (string-join time-fields ":") (if (< (date-hour date) 12) "am" "pm")))
(define (make-debug-timestamp)
11 years ago
(format "[~a ∆~as]" (make-timestamp) (seconds-since-last-message)))
11 years ago
;; creates pollen-logger and associated functions:
;; log-pollen-fatal, log-pollen-error, log-pollen-warning,
;; log-pollen-info, and log-pollen-debug
(define-logger pollen)
11 years ago
(define-syntax (make-message-logger-functions stx)
(syntax-case stx ()
[(_ stem)
(with-syntax ([message-stem (format-id stx "message-~a" #'stem)]
[log-pollen-stem (format-id stx "log-pollen-~a" #'stem)])
#'(begin
;; does file have particular extension
(define+provide (message-stem . items)
(log-pollen-stem (string-join `(,(make-debug-timestamp) ,@(map (λ(x)(if (string? x) x (format "~v" x))) items)))))))]))
11 years ago
(make-message-logger-functions fatal)
(make-message-logger-functions error)
(make-message-logger-functions warning)
(make-message-logger-functions info)
(make-message-logger-functions debug)
11 years ago
(define+provide (message . items)
11 years ago
(displayln (string-join `(,@(map (λ(x)(if (string? x) x (format "~v" x))) items)))))