Add display, simple-format and format variants
Which call put-bytevector for performing output. When used in combination with an unbuffered port, this should be safer than using the normal Guile procedures, as I think it'll avoid writing to the buffers, while still avoiding single character at a time output. More research is needed though in to how to output to stdout/stderr when using fibers with a parallelism greater than 1.
This commit is contained in:
parent
39ae5177f2
commit
094259b049
3 changed files with 69 additions and 25 deletions
52
knots.scm
52
knots.scm
|
|
@ -1,7 +1,9 @@
|
||||||
(define-module (knots)
|
(define-module (knots)
|
||||||
#:use-module (srfi srfi-1)
|
#:use-module (srfi srfi-1)
|
||||||
#:use-module (ice-9 match)
|
#:use-module (ice-9 match)
|
||||||
|
#:use-module (ice-9 binary-ports)
|
||||||
#:use-module (ice-9 suspendable-ports)
|
#:use-module (ice-9 suspendable-ports)
|
||||||
|
#:use-module (rnrs bytevectors)
|
||||||
#:use-module (fibers)
|
#:use-module (fibers)
|
||||||
#:use-module (fibers conditions)
|
#:use-module (fibers conditions)
|
||||||
#:use-module (system repl debug)
|
#:use-module (system repl debug)
|
||||||
|
|
@ -11,6 +13,10 @@
|
||||||
|
|
||||||
call-with-sigint
|
call-with-sigint
|
||||||
|
|
||||||
|
display/knots
|
||||||
|
simple-format/knots
|
||||||
|
format/knots
|
||||||
|
|
||||||
&knots-exception
|
&knots-exception
|
||||||
make-knots-exception
|
make-knots-exception
|
||||||
knots-exception?
|
knots-exception?
|
||||||
|
|
@ -51,6 +57,34 @@
|
||||||
;; restore original C handler.
|
;; restore original C handler.
|
||||||
(sigaction SIGINT #f))))))
|
(sigaction SIGINT #f))))))
|
||||||
|
|
||||||
|
(define* (display/knots obj #:optional (port (current-output-port)))
|
||||||
|
(put-bytevector
|
||||||
|
port
|
||||||
|
(string->utf8
|
||||||
|
(call-with-output-string
|
||||||
|
(lambda (port)
|
||||||
|
(display obj port))))))
|
||||||
|
|
||||||
|
(define (simple-format/knots port s . args)
|
||||||
|
(let ((str (apply simple-format #f s args)))
|
||||||
|
(if (eq? #f port)
|
||||||
|
str
|
||||||
|
(display/knots
|
||||||
|
str
|
||||||
|
(if (eq? #t port)
|
||||||
|
(current-output-port)
|
||||||
|
port)))))
|
||||||
|
|
||||||
|
(define (format/knots port s . args)
|
||||||
|
(let ((str (apply format #f s args)))
|
||||||
|
(if (eq? #f port)
|
||||||
|
str
|
||||||
|
(display/knots
|
||||||
|
str
|
||||||
|
(if (eq? #t port)
|
||||||
|
(current-output-port)
|
||||||
|
port)))))
|
||||||
|
|
||||||
(define &knots-exception
|
(define &knots-exception
|
||||||
(make-exception-type '&knots-exception
|
(make-exception-type '&knots-exception
|
||||||
&exception
|
&exception
|
||||||
|
|
@ -135,13 +169,19 @@
|
||||||
(error-string
|
(error-string
|
||||||
(with-exception-handler
|
(with-exception-handler
|
||||||
(lambda (exn)
|
(lambda (exn)
|
||||||
(display (get-output-string string-port)
|
(display/knots (get-output-string string-port)
|
||||||
port)
|
port)
|
||||||
(close-output-port string-port)
|
(close-output-port string-port)
|
||||||
(display "\n\n" port)
|
(display/knots "\n\n" port)
|
||||||
|
|
||||||
(backtrace port)
|
(let* ((stack (make-stack #t))
|
||||||
(simple-format
|
(backtrace
|
||||||
|
(call-with-output-string
|
||||||
|
(lambda (port)
|
||||||
|
(display-backtrace stack port)
|
||||||
|
(newline port)))))
|
||||||
|
(display/knots backtrace))
|
||||||
|
(simple-format/knots
|
||||||
port
|
port
|
||||||
"\nexception in print-backtrace-and-exception/knots: ~A\n"
|
"\nexception in print-backtrace-and-exception/knots: ~A\n"
|
||||||
exn)
|
exn)
|
||||||
|
|
@ -151,14 +191,14 @@
|
||||||
(let ((str (get-output-string string-port)))
|
(let ((str (get-output-string string-port)))
|
||||||
(close-output-port string-port)
|
(close-output-port string-port)
|
||||||
str)))))
|
str)))))
|
||||||
(display error-string port)))
|
(display/knots error-string port)))
|
||||||
|
|
||||||
(define* (spawn-fiber/knots thunk #:optional scheduler #:key parallel?)
|
(define* (spawn-fiber/knots thunk #:optional scheduler #:key parallel?)
|
||||||
(spawn-fiber
|
(spawn-fiber
|
||||||
(lambda ()
|
(lambda ()
|
||||||
(with-exception-handler
|
(with-exception-handler
|
||||||
(lambda (exn)
|
(lambda (exn)
|
||||||
(display "Uncaught exception in task:\n"
|
(display/knots "Uncaught exception in task:\n"
|
||||||
(current-error-port))
|
(current-error-port))
|
||||||
(print-backtrace-and-exception/knots exn))
|
(print-backtrace-and-exception/knots exn))
|
||||||
(lambda ()
|
(lambda ()
|
||||||
|
|
|
||||||
|
|
@ -90,7 +90,7 @@
|
||||||
(set-record-type-printer!
|
(set-record-type-printer!
|
||||||
<resource-pool>
|
<resource-pool>
|
||||||
(lambda (resource-pool port)
|
(lambda (resource-pool port)
|
||||||
(display
|
(display/knots
|
||||||
(simple-format #f "#<resource-pool name: \"~A\">"
|
(simple-format #f "#<resource-pool name: \"~A\">"
|
||||||
(resource-pool-name resource-pool))
|
(resource-pool-name resource-pool))
|
||||||
port)))
|
port)))
|
||||||
|
|
@ -488,7 +488,7 @@
|
||||||
(stack-ref stack 3)
|
(stack-ref stack 3)
|
||||||
'%exception
|
'%exception
|
||||||
(list exn))))))
|
(list exn))))))
|
||||||
(display error-string
|
(display/knots error-string
|
||||||
(current-error-port)))
|
(current-error-port)))
|
||||||
(raise-exception exn))
|
(raise-exception exn))
|
||||||
(lambda ()
|
(lambda ()
|
||||||
|
|
@ -1146,7 +1146,7 @@
|
||||||
(stack-ref stack 3)
|
(stack-ref stack 3)
|
||||||
'%exception
|
'%exception
|
||||||
(list exn))))))
|
(list exn))))))
|
||||||
(display error-string
|
(display/knots error-string
|
||||||
(current-error-port)))
|
(current-error-port)))
|
||||||
(raise-exception exn))
|
(raise-exception exn))
|
||||||
(lambda ()
|
(lambda ()
|
||||||
|
|
|
||||||
|
|
@ -289,7 +289,7 @@ on the procedure being called at any particular time."
|
||||||
(not (memq 'close (response-connection response))))
|
(not (memq 'close (response-connection response))))
|
||||||
|
|
||||||
(define (default-read-request-exception-handler exn)
|
(define (default-read-request-exception-handler exn)
|
||||||
(display "While reading request:\n" (current-error-port))
|
(display/knots "While reading request:\n" (current-error-port))
|
||||||
(print-exception
|
(print-exception
|
||||||
(current-error-port)
|
(current-error-port)
|
||||||
#f
|
#f
|
||||||
|
|
@ -302,12 +302,12 @@ on the procedure being called at any particular time."
|
||||||
(if (and (exception-with-origin? exn)
|
(if (and (exception-with-origin? exn)
|
||||||
(string=? (exception-origin exn)
|
(string=? (exception-origin exn)
|
||||||
"fport_write"))
|
"fport_write"))
|
||||||
(simple-format
|
(simple-format/knots
|
||||||
(current-error-port)
|
(current-error-port)
|
||||||
"~A ~A: error replying to client\n"
|
"~A ~A: error replying to client\n"
|
||||||
(request-method request)
|
(request-method request)
|
||||||
(uri-path (request-uri request)))
|
(uri-path (request-uri request)))
|
||||||
(simple-format
|
(simple-format/knots
|
||||||
(current-error-port)
|
(current-error-port)
|
||||||
"knots web server: ~A ~A: exception replying to client: ~A\n"
|
"knots web server: ~A ~A: exception replying to client: ~A\n"
|
||||||
(request-method request)
|
(request-method request)
|
||||||
|
|
@ -329,7 +329,7 @@ on the procedure being called at any particular time."
|
||||||
(print-backtrace-and-exception/knots
|
(print-backtrace-and-exception/knots
|
||||||
exn
|
exn
|
||||||
#:port port)))))
|
#:port port)))))
|
||||||
(display error-string
|
(display/knots error-string
|
||||||
(current-error-port)))
|
(current-error-port)))
|
||||||
|
|
||||||
(values (build-response #:code 500)
|
(values (build-response #:code 500)
|
||||||
|
|
@ -476,13 +476,17 @@ on the procedure being called at any particular time."
|
||||||
(unless (and (exception-with-origin? exn)
|
(unless (and (exception-with-origin? exn)
|
||||||
(string=? (exception-origin exn)
|
(string=? (exception-origin exn)
|
||||||
"fport_read"))
|
"fport_read"))
|
||||||
(display "knots web-server, exception in client loop:\n"
|
(display/knots "knots web-server, exception in client loop:\n"
|
||||||
(current-error-port))
|
(current-error-port))
|
||||||
|
(display/knots
|
||||||
|
(call-with-output-string
|
||||||
|
(lambda (port)
|
||||||
(print-exception
|
(print-exception
|
||||||
(current-error-port)
|
port
|
||||||
#f
|
#f
|
||||||
'%exception
|
'%exception
|
||||||
(list exn)))
|
(list exn))))
|
||||||
|
(current-error-port)))
|
||||||
#t)
|
#t)
|
||||||
(lambda ()
|
(lambda ()
|
||||||
(or
|
(or
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue