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