introduce logger

main
Matthew Butterick 5 years ago
parent 29d0f223e5
commit 80f383dc0c

@ -96,12 +96,14 @@ Save the document. Any place, any name is fine.
@onscreen{Run} the document. You'll get REPL output like this:
@repl-output{
hyphenate: cpu time: 0 real time: 0 gc time: 0
line-wrap: cpu time: 27 real time: 30 gc time: 0
page-wrap: cpu time: 0 real time: 1 gc time: 0
position: cpu time: 1 real time: 0 gc time: 0
draw: cpu time: 77 real time: 76 gc time: 23
wrote PDF to /Desktop/test.pdf
quadwriter: atomize: 2ms
quadwriter: hyphenate: 1ms
quadwriter: line-wrap: 21ms
quadwriter: col-wrap: 0ms
quadwriter: page-wrap: 0ms
quadwriter: position: 1ms
quadwriter: draw: 75ms
quadwriter: wrote PDF to /Users/Desktop/test.pdf
}
Congratulations — you just made your first PDF. If you want to have a look, either open the file manually, or enter this command on the REPL, which will open the PDF in your default viewer:

@ -8,7 +8,8 @@
syntax/strip-context
scribble/reader
quadwriter/core
txexpr)
txexpr
"log.rkt")
(provide (all-defined-out))
(define q (default-tag-function 'q))
@ -29,8 +30,8 @@
[(list _ kw val) (loop (cons (list kw val) acc))]))]
;; reverse in case of multiple values with same keyword, latest takes precedence (by becoming first)
[else (reverse (for/list ([item (in-list acc)])
(match-define (list kw val) (map bytes->string/utf-8 item))
(list (string->symbol (string-trim kw "#:")) val)))])))
(match-define (list kw val) (map bytes->string/utf-8 item))
(list (string->symbol (string-trim kw "#:")) val)))])))
(strip-context
(with-syntax ([PATH-STRING path-string]
[((ATTR-NAME ATTR-VAL) ...) kw-attrs]
@ -63,7 +64,12 @@
(when (file-exists? pdf-path)
(void (system (format open-string pdf-path)))))
(module+ main
(render-pdf DOC pdf-path))))]))))
(with-logging-to-port
(current-output-port)
(λ () (render-pdf DOC pdf-path))
#:logger quadwriter-logger
'info
'quadwriter))))]))))
(define (path-string->pdf-path path-string)
(match (format "~a" path-string)

@ -537,7 +537,7 @@
(hash-copy (quad-attrs col-quad)))]
[elems (from-parent (insert-blocks lns) 'nw)])))
(define (col-wrap qs vertical-height col-gap [col-quad q:column])
(define (column-wrap qs vertical-height column-gap [column-quad q:column])
(unless (positive? vertical-height)
(raise-argument-error 'col-wrap "positive number" vertical-height))
@ -546,7 +546,7 @@
;; could do it after, but it would require going back inside each col quad
;; which seems overly interdependent, because `insert-blocks` is used to determine break locations.
;; `col-wrap` should emit quads that are complete.
(define col-spacer (quad-copy q:column-spacer [size (pt col-gap 100)]))
(define col-spacer (quad-copy q:column-spacer [size (pt column-gap 100)]))
(add-between
(wrap qs vertical-height
#:soft-break #true
@ -556,7 +556,7 @@
;; do trial block insertions
(for/sum ([x (in-list (insert-blocks wrap-qs))])
(pt-y (size x))))
#:finish-wrap (col-finish-wrap col-quad))
#:finish-wrap (col-finish-wrap column-quad))
col-spacer))
(define ((page-finish-wrap page-quad path) cols q0 q page-idx)

@ -11,7 +11,8 @@
"attrs.rkt"
"param.rkt"
"font.rkt"
"layout.rkt")
"layout.rkt"
"log.rkt")
(provide (all-defined-out))
(define default-page-size "letter")
@ -64,14 +65,14 @@
:line-height (number->string (floor (* default-line-height-multiplier default-font-size)))) qexpr))]
(setup-font-path-table! pdf-path)
[define atomized-qs
(time-name atomize (atomize the-quad
(time-log atomize (atomize the-quad
#:attrs-proc handle-cascading-attrs
#:missing-glyph-action 'fallback
#:fallback "fallback"
#:emoji "emoji"
#:math "math"
#:font-path-resolver resolve-font-path))]
[define hyphenated-qs (time-name hyphenate (handle-hyphenate atomized-qs))]
[define hyphenated-qs (time-log hyphenate (handle-hyphenate atomized-qs))]
[define stringified-qs (map ->string-quad hyphenated-qs)]
[define indented-qs (insert-first-line-indents stringified-qs)]
indented-qs)
@ -149,22 +150,22 @@
(define column-gap (setup-column-gap qs))
(define line-wrap-size (/ (- printable-width (* (sub1 column-count) column-gap)) column-count))
(define line-qs (time-name line-wrap (apply-keeps (line-wrap qs line-wrap-size))))
(define line-qs (time-log line-wrap (apply-keeps (line-wrap qs line-wrap-size))))
(define col-quad-prototype (struct-copy quad q:column
[size (pt line-wrap-size printable-height)]))
(define column-qs (time-name col-wrap (col-wrap line-qs printable-height column-gap col-quad-prototype)))
(define column-qs (time-log column-wrap (column-wrap line-qs printable-height column-gap col-quad-prototype)))
(define page-quad-prototype (struct-copy quad q:page
[shift (pt left-margin top-margin)]
[size (pt line-wrap-size printable-height)]))
(define page-qs (time-name page-wrap (page-wrap column-qs printable-width page-quad-prototype)))
(define page-qs (time-log page-wrap (page-wrap column-qs printable-width page-quad-prototype)))
(define positioned-qs (time-name position (position (struct-copy quad q:doc [elems page-qs]))))
(time-name draw (draw positioned-qs (current-pdf))))
(define positioned-qs (time-log position (position (struct-copy quad q:doc [elems page-qs]))))
(time-log draw (draw positioned-qs (current-pdf))))
(if pdf-path-arg
(displayln (format "wrote PDF to ~a" pdf-path))
(log-quadwriter-info (format "wrote PDF to ~a" pdf-path))
(begin0
(file->bytes pdf-path)
(delete-file pdf-path))))
Loading…
Cancel
Save