(svn r9593) [NoAI] -Add: on runtime-error, print the complete stack, so the user can see where it went wrong noai
authortruelight
Wed, 11 Apr 2007 16:48:42 +0000
branchnoai
changeset 9576 d650eb70a8d0
parent 9575 a4b6bbfa6c96
child 9577 8e93be390b45
(svn r9593) [NoAI] -Add: on runtime-error, print the complete stack, so the user can see where it went wrong
src/squirrel.cpp
src/squirrel.hpp
--- a/src/squirrel.cpp	Wed Apr 11 14:18:10 2007 +0000
+++ b/src/squirrel.cpp	Wed Apr 11 16:48:42 2007 +0000
@@ -8,6 +8,7 @@
 #include "stdafx.h"
 #include "debug.h"
 #include "squirrel.hpp"
+#include <sqstdaux.h>
 
 void Squirrel::CompileError(HSQUIRRELVM vm, const SQChar *desc, const SQChar *source, SQInteger line, SQInteger column)
 {
@@ -22,9 +23,24 @@
 	free(dsc);
 }
 
+void Squirrel::ErrorPrintFunc(HSQUIRRELVM vm, const SQChar *s, ...)
+{
+	va_list arglist;
+	va_start(arglist, s);
+	scvfprintf(stderr, s, arglist);
+	va_end(arglist);
+}
+
 void Squirrel::RunError(HSQUIRRELVM vm, const char *error)
 {
-	DEBUG(misc, 0, "Your script made an error: %s\n", error);
+	/* Set the print function to something that prints to stderr */
+	SQPRINTFUNCTION pf = sq_getprintfunc(vm);
+	sq_setprintfunc(vm, &Squirrel::ErrorPrintFunc);
+	DEBUG(misc, 0, "Your script made an error: %s", error);
+	/* Print below the error the stack, so the users knows what is happening */
+	sqstd_printcallstack(vm);
+	/* Reset the old print function */
+	sq_setprintfunc(vm, pf);
 }
 
 SQInteger Squirrel::_RunError(HSQUIRRELVM vm)
--- a/src/squirrel.hpp	Wed Apr 11 14:18:10 2007 +0000
+++ b/src/squirrel.hpp	Wed Apr 11 16:48:42 2007 +0000
@@ -30,6 +30,11 @@
 	 */
 	static void PrintFunc(HSQUIRRELVM vm, const SQChar *s, ...);
 
+	/**
+	 * If an error has to be print, this function is called.
+	 */
+	static void ErrorPrintFunc(HSQUIRRELVM vm, const SQChar *s, ...);
+
 public:
 	Squirrel();
 	~Squirrel();