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

59 lines
1.8 KiB
Racket

11 years ago
#lang racket/base
(require racket/date)
(require racket/string)
(require racket/format)
(provide (all-defined-out))
; todo: contracts, tests, docs
(require (prefix-in williams: (planet williams/describe/describe)))
(define (describe x)
(williams:describe x)
x)
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
(- now then)
"--"))
11 years ago
(define (message . items)
(define (zero-fill str count)
(set! str (~a str))
(if (> (string-length str) count)
str
(string-append (make-string (- count (string-length str)) #\0) str)))
(define (make-date-string)
(define date (current-date))
11 years ago
(define date-fields (map (λ(x) (zero-fill x 2))
(list
11 years ago
; (date-day date)
; (list-ref months (sub1 (date-month date)))
11 years ago
(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)
(date-second date)
; (if (< (date-hour date) 12) "am" "pm")
11 years ago
(seconds-since-last-message)
11 years ago
)))
11 years ago
(apply format "[~a:~a:~a ~as]" date-fields))
11 years ago
(displayln (string-join `(,(make-date-string) ,@(map (λ(x)(if (string? x) x (~v x))) items))) (current-error-port)))
; report the current value of the variable, then return it
(define-syntax-rule (report var)
(begin
(message 'var "=" var)
var))