Skip to content

Commit e9255ba

Browse files
committed
Trying to implement a crash handler?
1 parent 9bda5a7 commit e9255ba

File tree

2 files changed

+99
-61
lines changed

2 files changed

+99
-61
lines changed

leafy/LfEngine.hx

Lines changed: 66 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class LfEngine {
3030
/**
3131
* The current version of the engine
3232
*/
33-
public static final VERSION:String = "1.5.0";
33+
public static final VERSION:String = "1.5.1";
3434

3535
/**
3636
* Function to be called when the engine exits
@@ -65,81 +65,86 @@ class LfEngine {
6565
* @param state The initial state
6666
*/
6767
public static function initEngine(gamePath:String, renderMode:LfWindowType = LfWindowType.DRC, state:LfState):Void {
68-
Proc.WHBProcInit();
69-
Log_udp.WHBLogUdpInit();
70-
Crash.WHBInitCrashHandler(); // This really works?
68+
try {
69+
Proc.WHBProcInit();
70+
Log_udp.WHBLogUdpInit();
71+
Crash.WHBInitCrashHandler(); // This really works?
7172

72-
Sys.println("[Leafy Engine initial state - no logger started] Starting Leafy Engine [" + VERSION + "]");
73+
Sys.println("[Leafy Engine initial state - no logger started] Starting Leafy Engine [" + VERSION + "]");
7374

74-
if (renderMode == null) {
75-
Sys.println("[Leafy Engine initial state - no logger started -> WARNING] Render mode cannot be null, defaulting to DRC mode");
76-
renderMode = LfWindowType.DRC;
77-
}
75+
if (renderMode == null) {
76+
Sys.println("[Leafy Engine initial state - no logger started -> WARNING] Render mode cannot be null, defaulting to DRC mode");
77+
renderMode = LfWindowType.DRC;
78+
}
7879

79-
if (gamePath == null || gamePath == "") {
80-
Sys.println("[Leafy Engine initial state - no logger started -> WARNING] Game path cannot be null, defaulting to LEAFY_GAME");
81-
gamePath = "LEAFY_GAME";
82-
}
80+
if (gamePath == null || gamePath == "") {
81+
Sys.println("[Leafy Engine initial state - no logger started -> WARNING] Game path cannot be null, defaulting to LEAFY_GAME");
82+
gamePath = "LEAFY_GAME";
83+
}
8384

84-
windowMode = renderMode;
85+
windowMode = renderMode;
8586

86-
// initialize the engine systems
87-
LfSystemPaths.initFSSystem();
88-
LeafyDebug.initLogger();
89-
SubEngines.startSDL();
87+
// initialize the engine systems
88+
LfSystemPaths.initFSSystem();
89+
LeafyDebug.initLogger();
90+
SubEngines.startSDL();
9091

91-
// Set the engine main path
92-
LfSystemPaths.setEngineMainPath(gamePath);
92+
// Set the engine main path
93+
LfSystemPaths.setEngineMainPath(gamePath);
9394

94-
// Initialize the Wii U Gamepad
95-
LfGamepadInternal.initDRC();
95+
// Initialize the Wii U Gamepad
96+
LfGamepadInternal.initDRC();
9697

97-
#if debug
98-
LeafyDebug.log("Haxe debug mode is enabled in this build, lag may occur when printing many more logs!", WARNING);
99-
#end
98+
#if debug
99+
LeafyDebug.log("Haxe debug mode is enabled in this build, lag may occur when printing many more logs!", WARNING);
100+
#end
100101

101-
LeafyDebug.log("Leafy Engine [" + VERSION + "] initialized", INFO);
102+
LeafyDebug.log("Leafy Engine [" + VERSION + "] initialized", INFO);
102103

103-
// Call the onEngineInitFinished function
104-
if (untyped __cpp__("onEngineInitFinished != NULL")) {
105-
onEngineInitFinished();
106-
}
107-
108-
// Set and initialize the initial state
109-
if (state == null) {
110-
LeafyDebug.criticalError("initial state cannot be null.");
111-
}
112-
LfStateHandler.initFirstState(state);
113-
initState = state;
114-
Leafy.currentState = state;
104+
// Call the onEngineInitFinished function
105+
if (untyped __cpp__("onEngineInitFinished != NULL")) {
106+
onEngineInitFinished();
107+
}
108+
109+
// Set and initialize the initial state
110+
if (state == null) {
111+
LeafyDebug.criticalError("initial state cannot be null.");
112+
}
113+
LfStateHandler.initFirstState(state);
114+
initState = state;
115+
Leafy.currentState = state;
115116

116-
///////////////////////////////////////////
117+
///////////////////////////////////////////
117118

118-
_isRunning = Proc.WHBProcIsRunning();
119-
// Start the main loop, the engine will shutdown when the main loop ends
120-
while(_isRunning) {
121119
_isRunning = Proc.WHBProcIsRunning();
122-
LfWindowRender.updateRenderers();
123-
LeafyDebug.updateLogTime();
124-
Leafy.update();
125-
126-
if (!_isRunning) {
127-
Leafy.paused = true;
120+
// Start the main loop, the engine will shutdown when the main loop ends
121+
while(_isRunning) {
122+
_isRunning = Proc.WHBProcIsRunning();
123+
LfWindowRender.updateRenderers();
124+
LeafyDebug.updateLogTime();
125+
Leafy.update();
126+
127+
if (!_isRunning) {
128+
Leafy.paused = true;
129+
}
130+
};
131+
132+
// Shutdown the engine //////////
133+
// Call the onEngineExit function
134+
if (onEngineExit != null) {
135+
onEngineExit();
128136
}
129-
};
130-
131-
// Shutdown the engine //////////
132-
// Call the onEngineExit function
133-
if (onEngineExit != null) {
134-
onEngineExit();
135-
}
136137

137-
LeafyDebug.log("Shutting down Leafy Engine [" + VERSION + "]", INFO);
138+
LeafyDebug.log("Shutting down Leafy Engine [" + VERSION + "]", INFO);
138139

139-
LfStateHandler.destroyCurrentState();
140-
SubEngines.shutdownSDL();
141-
LfSystemPaths.deinitFSSystem();
142-
Log_udp.WHBLogUdpDeinit();
143-
Proc.WHBProcShutdown();
140+
LfStateHandler.destroyCurrentState();
141+
SubEngines.shutdownSDL();
142+
LfSystemPaths.deinitFSSystem();
143+
Log_udp.WHBLogUdpDeinit();
144+
Proc.WHBProcShutdown();
145+
}
146+
catch (e) {
147+
LeafyDebug.crashHandler(e.message);
148+
}
144149
}
145150
}

leafy/backend/LeafyDebug.hx

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ package leafy.backend;
88
import haxe.PosInfos;
99
import Std;
1010
import cxx.std.Exception;
11+
import haxe.Exception;
12+
import haxe.CallStack;
1113

1214
import wut.coreinit.Debug;
1315

@@ -259,4 +261,35 @@ class LeafyDebug {
259261

260262
Debug.OSFatal(strPtr);
261263
}
264+
265+
/**
266+
* A crash handler that logs the Haxe call stack and error message
267+
* @param e The error message
268+
*/
269+
public static function crashHandler(e:String):Void {
270+
var callStack:Array<StackItem> = CallStack.exceptionStack(true);
271+
var callStackText:String = "Call stack:\n";
272+
273+
for (stackItem in callStack)
274+
{
275+
switch (stackItem)
276+
{
277+
case FilePos(s, file, line, column):
278+
callStackText += file + ":" + line + "\n";
279+
case CFunction:
280+
callStackText += "Non-Haxe (C++) Function";
281+
case Module(c):
282+
callStackText += 'Module ${c}';
283+
default:
284+
callStackText += "Unknown stack item: " + Std.string(stackItem) + "\n";
285+
}
286+
}
287+
288+
callStackText += "\nError: " + LfStringUtils.stringReplacer(e, "Error: ", "");
289+
LfFile.appendToFile(currentLogFile, "-- CRASH ------------");
290+
log(callStackText, ERROR);
291+
LfFile.appendToFile(currentLogFile, "---------------------");
292+
log("Executing WUT OSFatal call for crash...", ERROR);
293+
Debug.OSFatal(ConstCharPtr.fromString("[Leafy Engine " + LfEngine.VERSION + " logger - " + getCurrentTime() + " - CRASH]\n\n" + callStackText));
294+
}
262295
}

0 commit comments

Comments
 (0)