core/CDbgReporter.cpp
author David Anderson <dvander@alliedmods.net>
Tue Jan 16 20:36:09 2007 +0000 (2007-01-16)
changeset 300 186b2f7bf277
parent 288 d89bf250dac4
child 307 cab87ea66ab1
permissions -rw-r--r--
fixed up bad includes
     1 #include "CDbgReporter.h"
     2 #include "CLogger.h"
     3 #include "PluginSys.h"
     4 
     5 void CDbgReporter::OnSourceModAllInitialized()
     6 {
     7 	g_pSourcePawn->SetDebugListener(this);
     8 }
     9 
    10 void CDbgReporter::OnContextExecuteError(IPluginContext *ctx, IContextTrace *error)
    11 {
    12 	const char *lastname;
    13 	const char *plname = g_PluginSys.FindPluginByContext(ctx->GetContext())->GetFilename();
    14 	int n_err = error->GetErrorCode();
    15 
    16 	if (n_err != SP_ERROR_NATIVE)
    17 	{
    18 		g_Logger.LogError("[SOURCEMOD] Plugin \"%s\" encountered error %d: %s",
    19 			plname,
    20 			n_err,
    21 			error->GetErrorString());
    22 	}
    23 
    24 	if ((lastname=error->GetLastNative(NULL)) != NULL)
    25 	{
    26 		const char *custerr;
    27 		if ((custerr=error->GetCustomErrorString()) != NULL)
    28 		{
    29 			g_Logger.LogError("[SOURCEMOD] Native \"%s\" reported: \"%s\"", lastname, custerr);
    30 		} else {
    31 			g_Logger.LogError("[SOURCEMOD] Native \"%s\" encountered a generic error.", lastname);
    32 		}
    33 	}
    34 
    35 	if (!error->DebugInfoAvailable())
    36 	{
    37 		g_Logger.LogError("[SOURCEMOD] Debug mode is not enabled for this plugin.");
    38 		g_Logger.LogError("[SOURCEMOD] To enable debug mode, edit plugin_settings.cfg, or type: sm plugins debug %d on",
    39 			_GetPluginIndex(ctx));
    40 		return;
    41 	}
    42 
    43 	CallStackInfo stk_info;
    44 	int i = 0;
    45 	g_Logger.LogError("[SOURCEMOD] Displaying call stack trace:");
    46 	while (error->GetTraceInfo(&stk_info))
    47 	{
    48 		g_Logger.LogError("[SOURCEMOD]   [%d]  Line %d, %s::%s()",
    49 			i++,
    50 			stk_info.line,
    51 			stk_info.filename,
    52 			stk_info.function);
    53 	}
    54 }
    55 
    56 int CDbgReporter::_GetPluginIndex(IPluginContext *ctx)
    57 {
    58 	int id = 1;
    59 	IPluginIterator *iter = g_PluginSys.GetPluginIterator();
    60 
    61 	for (; iter->MorePlugins(); iter->NextPlugin(), id++)
    62 	{
    63 		IPlugin *pl = iter->GetPlugin();
    64 		if (pl->GetBaseContext() == ctx)
    65 		{
    66 			iter->Release();
    67 			return id;
    68 		}
    69 	}
    70 
    71 	iter->Release();
    72 	return -1;
    73 }