From 80f383dc0cf66beae66aef07fb35b47851ae902e Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Tue, 4 Jun 2019 12:30:46 -0700 Subject: [PATCH] introduce logger --- quad/quad/scribblings/quad.scrbl | 14 ++++++++------ quad/quadwriter/lang-helper.rkt | 14 ++++++++++---- quad/quadwriter/layout.rkt | 6 +++--- quad/quadwriter/render.rkt | 19 ++++++++++--------- 4 files changed, 31 insertions(+), 22 deletions(-) diff --git a/quad/quad/scribblings/quad.scrbl b/quad/quad/scribblings/quad.scrbl index 448bd602..b9441412 100644 --- a/quad/quad/scribblings/quad.scrbl +++ b/quad/quad/scribblings/quad.scrbl @@ -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: diff --git a/quad/quadwriter/lang-helper.rkt b/quad/quadwriter/lang-helper.rkt index 1727ae04..1f04af5c 100644 --- a/quad/quadwriter/lang-helper.rkt +++ b/quad/quadwriter/lang-helper.rkt @@ -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) diff --git a/quad/quadwriter/layout.rkt b/quad/quadwriter/layout.rkt index e30e92bd..6da3664d 100644 --- a/quad/quadwriter/layout.rkt +++ b/quad/quadwriter/layout.rkt @@ -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) diff --git a/quad/quadwriter/render.rkt b/quad/quadwriter/render.rkt index 5b2ecda7..866f45cd 100644 --- a/quad/quadwriter/render.rkt +++ b/quad/quadwriter/render.rkt @@ -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)))) \ No newline at end of file