1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2025-02-22 12:47:13 +01:00

Include code line in runtime debug.

Include the line of code where the error occured into the debug message. Further adjustments to the oeverall implementation.
This commit is contained in:
Sandu Liviu Catalin 2019-06-16 02:51:16 +03:00
parent acea3db378
commit 061110914a
3 changed files with 55 additions and 28 deletions

View File

@ -214,6 +214,8 @@ bool Core::Initialize()
return false; return false;
} }
// See if debugging options should be enabled
m_Debugging = conf.GetBoolValue("Squirrel", "Debugging", m_Debugging);
// Configure the empty initialization // Configure the empty initialization
m_EmptyInit = conf.GetBoolValue("Squirrel", "EmptyInit", false); m_EmptyInit = conf.GetBoolValue("Squirrel", "EmptyInit", false);
// Configure the verbosity level // Configure the verbosity level
@ -272,8 +274,6 @@ bool Core::Initialize()
DefaultVM::Set(m_VM); DefaultVM::Set(m_VM);
// Configure error handling // Configure error handling
ErrorHandling::Enable(conf.GetBoolValue("Squirrel", "ErrorHandling", true)); ErrorHandling::Enable(conf.GetBoolValue("Squirrel", "ErrorHandling", true));
// See if debugging options should be enabled
m_Debugging = conf.GetBoolValue("Squirrel", "Debugging", m_Debugging);
// Prevent common null objects from using dead virtual machines // Prevent common null objects from using dead virtual machines
NullArray() = Array(); NullArray() = Array();
@ -748,6 +748,20 @@ void Core::SetIncomingName(CSStr name)
m_IncomingNameBuffer[len] = '\0'; m_IncomingNameBuffer[len] = '\0';
} }
// ------------------------------------------------------------------------------------------------
String Core::FetchCodeLine(CSStr src, SQInteger line, bool trim)
{
// Find the script we're looking for
Scripts::iterator script = FindScript(src);
// Do we have a valid script and line?
if ((script == m_Scripts.end()) || !(script->mInfo) || (script->mLine.size() < line))
{
return String{}; // No such script!
}
// Fetch the line of code
return script->FetchLine(line, trim);
}
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
bool Core::DoScripts(Scripts::iterator itr, Scripts::iterator end) bool Core::DoScripts(Scripts::iterator itr, Scripts::iterator end)
{ {
@ -886,29 +900,18 @@ void Core::CompilerErrorHandler(HSQUIRRELVM /*vm*/, CSStr desc, CSStr src, SQInt
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
bool Core::CompilerErrorHandlerEx(CSStr desc, CSStr src, SQInteger line, SQInteger column) bool Core::CompilerErrorHandlerEx(CSStr desc, CSStr src, SQInteger line, SQInteger column)
{ {
// Find the script we're looking for
Scripts::iterator script = FindScript(src);
// Found anything?
if (script == m_Scripts.end())
{
return false; // No such script!
}
// Have debug information?
else if (!(script->mInfo))
{
return false; // Nothing to show!
}
// Is this line outside the range that we have?
else if (script->mLine.size() < line)
{
return false; // No such line!
}
// Grab the associated line of code // Grab the associated line of code
String code = script->FetchLine(line, true); String code = FetchCodeLine(src, line, true);
// Valid line of code?
if (!code.empty())
{
// Display the error message with the code included // Display the error message with the code included
LogFtl("Message: %s\n[\n=>Location: %s\n=>Line: %" PRINT_SZ_FMT "\n=>Column: %" PRINT_INT_FMT "\n=>Code: %s\n]", desc, src, ++line, column, code.c_str()); LogFtl("Message: %s\n[\n=>Location: %s\n=>Line: %" PRINT_SZ_FMT "\n=>Column: %" PRINT_INT_FMT "\n=>Code: %s\n]", desc, src, ++line, column, code.c_str());
// We displayed the information // We displayed the information
return true; return true;
}
// No code to show. Fall back to normal message
return true;
} }
} // Namespace:: SqMod } // Namespace:: SqMod

View File

@ -958,13 +958,18 @@ public:
void SetIncomingName(CSStr name); void SetIncomingName(CSStr name);
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------
* retrieve the name for the currently assigned incoming connection.. * Retrieve the name for the currently assigned incoming connection.
*/ */
CSStr GetIncomingName() CSStr GetIncomingName()
{ {
return (!m_IncomingNameBuffer) ? _SC("") : m_IncomingNameBuffer; return (!m_IncomingNameBuffer) ? _SC("") : m_IncomingNameBuffer;
} }
/* --------------------------------------------------------------------------------------------
* Retrieves a line of code from a certain source.
*/
String FetchCodeLine(CSStr src, SQInteger line, bool trim = true);
protected: protected:
/* -------------------------------------------------------------------------------------------- /* --------------------------------------------------------------------------------------------

View File

@ -1,5 +1,6 @@
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
#include "Logger.hpp" #include "Logger.hpp"
#include "Core.hpp"
#include "Base/Utility.hpp" #include "Base/Utility.hpp"
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
@ -472,12 +473,30 @@ void Logger::Debug(CCStr fmt, va_list args)
Int32 ret = m_Buffer.WriteF(0, fmt, args); Int32 ret = m_Buffer.WriteF(0, fmt, args);
// Obtain information about the current stack level // Obtain information about the current stack level
if (SQ_SUCCEEDED(sq_stackinfos(vm, 1, &si))) if (SQ_SUCCEEDED(sq_stackinfos(vm, 1, &si)))
{
// Whether we should fall back to normal message
bool fall_back = true;
// Should (can) we include a snippet of code in the traceback?
if (Core::Get().IsDebugging() && si.source && (si.line > 0)) {
// Grab the associated line of code
String code = Core::Get().FetchCodeLine(si.source, si.line-1, true);
// Valid line of code?
if (!code.empty())
{
m_Buffer.WriteF(ret, "\n[\n=>Location: %s\n=>Line: %d\n=>Function: %s\n=>Code: %s\n]"
, si.source, si.line, si.funcname ? si.funcname : _SC("unknown"), code.c_str());
fall_back = false; // No need to fall back to normal message!
}
}
// Should the regular message be shown instead?
if (fall_back)
{ {
m_Buffer.WriteF(ret, "\n[\n=>Location: %s\n=>Line: %d\n=>Function: %s\n]" m_Buffer.WriteF(ret, "\n[\n=>Location: %s\n=>Line: %d\n=>Function: %s\n]"
, si.source ? si.source : _SC("unknown") , si.source ? si.source : _SC("unknown")
, si.line , si.line
, si.funcname ? si.funcname : _SC("unknown")); , si.funcname ? si.funcname : _SC("unknown"));
} }
}
else else
{ {
m_Buffer.WriteF(ret, "\n[\n=>Location: unknown\n=>Line: unknown\n=>Function: unknown\n]"); m_Buffer.WriteF(ret, "\n[\n=>Location: unknown\n=>Line: unknown\n=>Function: unknown\n]");