Skip to content

Commit

Permalink
set current-error-port for error-display-handler (for uncaught-except…
Browse files Browse the repository at this point in the history
…ion-handler)

I'm not convinced the load-game code is right, and the "Show error logs"
menu item is not great (I want to copy/paste the path), but it will do
for now.

Close #109
  • Loading branch information
benknoble committed Dec 31, 2024
1 parent 268abe5 commit d6edcdf
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 11 deletions.
19 changes: 18 additions & 1 deletion gui/common-menu.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@
[send-feedback-menu-item (-> (is-a?/c view<%>))]
[how-to-play-menu-item (-> (is-a?/c view<%>))]
[launch-server-menu-item (-> state? (is-a?/c view<%>))]
[gc-menu-item (-> (is-a?/c view<%>))]))
[gc-menu-item (-> (is-a?/c view<%>))]
[error-logs-menu-item (-> (obs/c (or/c #f path?)) (is-a?/c view<%>))]))

(require frosthaven-manager/gui/markdown
frosthaven-manager/gui/render
frosthaven-manager/gui/server
frosthaven-manager/manager
frosthaven-manager/observable-operator
net/sendurl
racket/gui/easy
racket/gui/easy/contract
racket/runtime-path
(prefix-in gui: racket/gui))

Expand Down Expand Up @@ -108,6 +111,20 @@
(gui:make-monochrome-bitmap 80 80 (make-bytes (sqr 80) #xff))
(gui:make-monochrome-bitmap 80 80))))))))))

(define (error-logs-menu-item @error-logs)
(menu-item
"Show error logs"
(thunk
(with-closing-custodian/eventspace
(render/eventspace
#:eventspace closing-eventspace
(window
#:mixin close-custodian-mixin
#:title "Error logs"
(text (@> @error-logs
{~> (if _ path->string "standard error on your terminal device")
(~a "Errors logs are written to " _ ".")}))))))))

(module+ main
(render
(window
Expand Down
5 changes: 5 additions & 0 deletions gui/manager.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
(collect-garbage 'incremental)
(base-event-handler es)))
(define s (make-state))
(unless (terminal-port? (current-error-port))
(define temp (make-temporary-file "frosthaven-manager-~a"))
(current-error-port (open-output-file temp #:exists 'truncate #:mode 'text))
(:= (state-@error-logs s) temp))
(command-line
#:once-each
[("--debug") "Enable debugging"
Expand Down Expand Up @@ -91,6 +95,7 @@
(menu "Utilities"
(formula-menu-item (state-@env s)))
(menu "Debug"
(error-logs-menu-item (state-@error-logs s))
(gc-menu-item))
(menu "Help"
(about-menu-item)
Expand Down
16 changes: 14 additions & 2 deletions manager/save.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
[load-game (-> state? (-> path-string? any))]))

(require frosthaven-manager/files
frosthaven-manager/manager/state)
frosthaven-manager/manager/state
frosthaven-manager/observable-operator)

(define ((save-game s) p)
(call-with-output-file* p (curry serialize-state s) #:exists 'replace))
Expand All @@ -17,7 +18,18 @@

(define ((load-game s) p)
(define saved-state (call-with-input-file* p deserialize-state))
(copy-state saved-state s))
(copy-state saved-state s)
;; Restore error logs, unless there was no file but now stderr isn't hooked up
;; to a TTY. See gui/manager.rkt startup sequence.
(cond
[(@! (state-@error-logs s))
=>
(λ (logs)
(current-error-port (open-output-file logs #:exists 'append #:mode 'text)))]
[(not (terminal-port? (current-error-port)))
(define temp (make-temporary-file "frosthaven-manager-~a"))
(current-error-port (open-output-file temp #:exists 'truncate #:mode 'text))
(:= (state-@error-logs s) temp)]))

(define (do-load-game s)
(cond [(get-file/filter "Load Game" '("Saved Games" "*.fasl")) => (load-game s)]))
19 changes: 13 additions & 6 deletions manager/state.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
[@bestiary-path (obs/c (or/c #f path-string?))]
[@ability-decks (obs/c (hash/c string? ability-decks?))]
[@prompts (obs/c (listof prompt/c))]
[@type->deck (obs/c (hash/c loot-type/c (listof loot-card?)))])]
[@type->deck (obs/c (hash/c loot-type/c (listof loot-card?)))]
[@error-logs (obs/c (or/c #f path?))])]
[make-state
(->* ()
((maybe-obs/c level/c)
Expand All @@ -50,7 +51,8 @@
(maybe-obs/c (or/c #f path-string?))
(maybe-obs/c (hash/c string? ability-decks?))
(maybe-obs/c (listof prompt/c))
(maybe-obs/c (hash/c loot-type/c (listof loot-card?))))
(maybe-obs/c (hash/c loot-type/c (listof loot-card?)))
(maybe-obs/c (or/c #f path?)))
state?)]
[state-@env (-> state? (obs/c env/c))]
[state-@info-db (-> state? (obs/c info-db/c))]
Expand Down Expand Up @@ -131,7 +133,8 @@
@bestiary-path
@ability-decks
@prompts
@type->deck]
@type->deck
@error-logs]
#:transparent ;; for struct->vector
#:property prop:serializable
(make-serialize-info
Expand Down Expand Up @@ -160,7 +163,8 @@
[@bestiary-path (@ #f)]
[@ability-decks (@ (hash))]
[@prompts (@ empty)]
[@type->deck (@ standard-loot-deck)])
[@type->deck (@ standard-loot-deck)]
[@error-logs (@ #f)])
(state (@ @level)
(@ @num-players)
(@ @creatures)
Expand Down Expand Up @@ -189,7 +193,8 @@
(@ @bestiary-path)
(@ @ability-decks)
(@ @prompts)
(@ @type->deck)))
(@ @type->deck)
(@ @error-logs)))

(define (state-@env s)
(obs-combine (λ (c l) (hash "C" c "L" l))
Expand Down Expand Up @@ -295,7 +300,9 @@
(:= (state-@prompts to)
(@! (state-@prompts from)))
(:= (state-@type->deck to)
(@! (state-@type->deck from))))
(@! (state-@type->deck from)))
(:= (state-@error-logs to)
(@! (state-@error-logs from))))

(define (state-@info-db s)
(@> (state-@bestiary-path s)
Expand Down
1 change: 1 addition & 0 deletions scribblings/how-to-play.scrbl
Original file line number Diff line number Diff line change
Expand Up @@ -328,5 +328,6 @@ In the @onscreen{Utilities} menu, you will the find following entries:

In the @onscreen{Debug} menu, you will find the following entries:
@itemlist[
@item{@onscreen{Show error logs}: Find out where errors are being logged to.}
@item{@onscreen{Observe GC}: This is used by developers to show a visual indicator of @tech[#:doc '(lib "scribblings/reference/reference.scrbl")]{garbage collection}. This is not something normal players need to worry about.}
]
6 changes: 4 additions & 2 deletions scribblings/manager/state.scrbl
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ True iff @racket[c] holds a @racket[monster-group*].
[|@|bestiary-path (obs/c (or/c #f path-string?))]
[|@|ability-decks (obs/c (hash/c string? ability-decks?))]
[|@|prompts (obs/c (listof prompt/c))]
[|@|type->deck (maybe-obs/c (hash/c loot-type/c (listof loot-card?)))])]{
[|@|type->deck (maybe-obs/c (hash/c loot-type/c (listof loot-card?)))]
[|@|error-logs (obs/c (or/c #f path?))])]{
All of the "global" manager state.
}

Expand All @@ -86,7 +87,8 @@ All of the "global" manager state.
[|@|bestiary-path (maybe-obs/c (or/c #f path-string?)) (|@| #f)]
[|@|ability-decks (maybe-obs/c (hash/c string? ability-decks?)) (|@| (hash))]
[|@|prompts (maybe-obs/c (listof prompt/c)) (|@| empty)]
[|@|type->deck (maybe-obs/c (hash/c loot-type/c (listof loot-card?))) (|@| standard-loot-deck)])
[|@|type->deck (maybe-obs/c (hash/c loot-type/c (listof loot-card?))) (|@| standard-loot-deck)]
[|@|error-logs (maybe-obs/c (or/c #f path?))])
state?]{
Create an initial state.
}
Expand Down

0 comments on commit d6edcdf

Please sign in to comment.