forked from google-deepmind/deepmind-research
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.rkt
69 lines (60 loc) · 2.26 KB
/
log.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#lang racket/base
;***************************************************************************************;
;**** Logging To File With Consistent Debugging Information ****;
;***************************************************************************************;
(require bazaar/date
bazaar/debug
define2
global
racket/file
racket/port
racket/pretty
racket/string
racket/system)
(provide call-with-log
*log*)
(define-global:boolean *log* #false
"Output to a log file?")
(define-global:boolean *git?* #false
"Commit to git if needed and include the last git commit hash in the globals.")
;; Calls thunk. Outputs to a log file if `log?` is not #false.
;; When `git?` is not #false, also commits to git to ensure consistency of the code base
;; with the experiment, and adds the git commit number to the global variables.
;;
;; thunk : thunk?
;; dir : path-string?
;; filename : string?
;; filepath : path-string?
;; log? : boolean?
;; git? : boolean?
;; quiet? : boolean?
(define (call-with-log thunk
#:? [dir "logs"]
#:? [filename (string-append "log-" (date-iso-file) ".txt")]
; if given, dir and filename have no effect:
#:? [filepath (build-path dir filename)]
#:? [log? (*log*)]
#:? [git? (*git?*)]
#:? [quiet? #false])
(when git?
(define cmd "git commit -am \".\" ")
(displayln cmd)
(system cmd))
;; Non-quiet mode.
(define (thunk2)
; Also write the last commit hash for easy retrieval.
(pretty-write
(list* `(cmd-line . ,(current-command-line-arguments))
`(git-commit . ,(and git?
(string-normalize-spaces
(with-output-to-string (λ () (system "git rev-parse HEAD"))))))
(globals->assoc)))
(thunk))
(cond [log?
(make-parent-directory* filepath)
(assert (not (file-exists? filepath)) filepath)
(printf "Logging to: ~a\n" filepath)
(pretty-write (globals->assoc))
(with-output-to-file filepath thunk2)]
[quiet? (thunk)]
[else (thunk2)]))