abstract exception handler/logger into its own file

This commit is contained in:
2025-08-04 09:03:47 +01:00
parent f4c9039af1
commit ce0f700253
9 changed files with 60 additions and 47 deletions

View File

@@ -5,4 +5,4 @@ run:
./build-unix.sh && ./shf ./build-unix.sh && ./shf
tests: tests:
mlton shf-tests.mlb && ./shf-tests mlton -const "Exn.keepHistory true" shf-tests.mlb && ./shf-tests

View File

@@ -569,6 +569,7 @@ struct
fun deleteLineBack (app: app_type, count) = fun deleteLineBack (app: app_type, count) =
let let
val _ = raise Empty
val {buffer, cursorIdx, ...} = app val {buffer, cursorIdx, ...} = app
val low = Cursor.vi0 (buffer, cursorIdx) val low = Cursor.vi0 (buffer, cursorIdx)
val high = Cursor.viDlr (buffer, cursorIdx, 1) + 1 val high = Cursor.viDlr (buffer, cursorIdx, 1) + 1

View File

@@ -130,6 +130,9 @@ struct
end end
fun fromRange (startIdx, length, buffer, searchString, searchList) = fun fromRange (startIdx, length, buffer, searchString, searchList) =
if String.size searchString = 0 then
(buffer, searchList)
else
let let
val finishIdx = startIdx + length + String.size searchString val finishIdx = startIdx + length + String.size searchString
val bufferIdx = startIdx - String.size searchString val bufferIdx = startIdx - String.size searchString

View File

@@ -0,0 +1,38 @@
structure ExceptionLogger =
struct
open InputMsg
val textCommands = ref ""
fun addCommand inputMsg =
case inputMsg of
CHAR_EVENT chr =>
let
val chr = CharVector.fromList [chr]
val newInput = !textCommands ^ chr
in
textCommands := newInput
end
| _ => ()
fun log e =
let
(* print stack trace for debugging purposes,
* and then raise another exception to exit the program *)
val stackTrace = MLton.Exn.history e
val stackTrace = String.concatWith "\n" stackTrace
val () = print "ERROR:\n"
val () = print (stackTrace ^ "\n\n")
val history = !textCommands ^ "\n\n"
val () = print ("HISTORY: " ^ history)
val textOutput = stackTrace ^ "\n" ^ history
val io = TextIO.openAppend "exceptions.log"
val () = TextIO.output (io, textOutput)
val () = TextIO.closeOut io
in
raise Empty
end
end

View File

@@ -2,7 +2,6 @@ structure UpdateThread =
struct struct
open CML open CML
open MailboxType open MailboxType
open InputMsg
fun sendMsg (msg, drawMailbox) = fun sendMsg (msg, drawMailbox) =
case msg of DRAW msg => Mailbox.send (drawMailbox, msg) case msg of DRAW msg => Mailbox.send (drawMailbox, msg)
@@ -15,46 +14,12 @@ struct
end end
| [] => () | [] => ()
val textCommands = ref ""
fun addTextCommand inputMsg =
case inputMsg of
CHAR_EVENT chr =>
let
val chr = CharVector.fromList [chr]
val newInput = !textCommands ^ chr
in
textCommands := newInput
end
| _ => ()
fun handleException e =
let
(* print stack trace for debugging purposes,
* and then raise another exception to exit the program *)
val stackTrace = MLton.Exn.history e
val stackTrace = String.concatWith "\n" stackTrace
val () = print "ERROR:\n"
val () = print (stackTrace ^ "\n\n")
val history = !textCommands ^ "\n\n"
val () = print ("HISTORY: " ^ history)
val textOutput = stackTrace ^ "\n" ^ history
val io = TextIO.openAppend "exceptions.log"
val () = TextIO.output (io, textOutput)
val () = TextIO.closeOut io
in
raise Empty
end
fun loop (app: AppType.app_type, inputMailbox, drawMailbox) = fun loop (app: AppType.app_type, inputMailbox, drawMailbox) =
let let
val inputMsg = Mailbox.recv inputMailbox val inputMsg = Mailbox.recv inputMailbox
val () = addTextCommand inputMsg val () = ExceptionLogger.addCommand inputMsg
val app = AppUpdate.update (app, inputMsg) handle e => handleException e val app = AppUpdate.update (app, inputMsg) handle e => ExceptionLogger.log e
val () = sendMsgs (#msgs app, drawMailbox) val () = sendMsgs (#msgs app, drawMailbox)
in in

View File

@@ -35,6 +35,10 @@ fcore/move.sml
fcore/app-update.sml fcore/app-update.sml
(* TEST FILES *) (* TEST FILES *)
$(SML_LIB)/basis/mlton.mlb
shell/exception-logger.sml
test/Railroad/src/railroad.mlb test/Railroad/src/railroad.mlb
test/normal-move.sml test/normal-move.sml
test/normal-delete.sml test/normal-delete.sml

View File

@@ -46,6 +46,7 @@ in
ffi/glfw-input.sml ffi/glfw-input.sml
end end
shell/exception-logger.sml
shell/update-thread.sml shell/update-thread.sml
shell/gl-shaders.sml shell/gl-shaders.sml
shell/gl-draw.sml shell/gl-draw.sml

View File

@@ -9,8 +9,8 @@ struct
else else
let let
val chr = String.sub (str, pos) val chr = String.sub (str, pos)
val () = ExceptionLogger.addCommand (InputMsg.CHAR_EVENT chr)
val app = AppUpdate.update (app, InputMsg.CHAR_EVENT chr) val app = AppUpdate.update (app, InputMsg.CHAR_EVENT chr)
handle _ => raise Fail (Int.toString pos)
in in
updateLoop (pos + 1, str, app) updateLoop (pos + 1, str, app)
end end

View File

@@ -10,6 +10,7 @@ struct
val tests = concat tests val tests = concat tests
in in
runWithConfig [Configuration.PrintPassed false] tests runWithConfig [Configuration.PrintPassed false] tests
handle e => ExceptionLogger.log e
end end
end end