core/DebugReporter.cpp
changeset 592 c2a2c38f1331
child 616 d5b1dcf38c07
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/core/DebugReporter.cpp	Sat Mar 10 21:26:04 2007 +0000
     1.3 @@ -0,0 +1,89 @@
     1.4 +/**
     1.5 + * ===============================================================
     1.6 + * SourceMod (C)2004-2007 AlliedModders LLC.  All rights reserved.
     1.7 + * ===============================================================
     1.8 + *
     1.9 + * This file is not open source and may not be copied without explicit
    1.10 + * written permission of AlliedModders LLC.  This file may not be redistributed 
    1.11 + * in whole or significant part.
    1.12 + * For information, see LICENSE.txt or http://www.sourcemod.net/license.php
    1.13 + *
    1.14 + * Version: $Id$
    1.15 + */
    1.16 +
    1.17 +#include "DebugReporter.h"
    1.18 +#include "Logger.h"
    1.19 +#include "PluginSys.h"
    1.20 +
    1.21 +CDbgReporter g_DbgReporter;
    1.22 +
    1.23 +void CDbgReporter::OnSourceModAllInitialized()
    1.24 +{
    1.25 +	g_pSourcePawn->SetDebugListener(this);
    1.26 +}
    1.27 +
    1.28 +void CDbgReporter::OnContextExecuteError(IPluginContext *ctx, IContextTrace *error)
    1.29 +{
    1.30 +	const char *lastname;
    1.31 +	const char *plname = g_PluginSys.FindPluginByContext(ctx->GetContext())->GetFilename();
    1.32 +	int n_err = error->GetErrorCode();
    1.33 +
    1.34 +	if (n_err != SP_ERROR_NATIVE)
    1.35 +	{
    1.36 +		g_Logger.LogError("[SM] Plugin \"%s\" encountered error %d: %s",
    1.37 +			plname,
    1.38 +			n_err,
    1.39 +			error->GetErrorString());
    1.40 +	}
    1.41 +
    1.42 +	if ((lastname=error->GetLastNative(NULL)) != NULL)
    1.43 +	{
    1.44 +		const char *custerr;
    1.45 +		if ((custerr=error->GetCustomErrorString()) != NULL)
    1.46 +		{
    1.47 +			g_Logger.LogError("[SM] Native \"%s\" reported: %s", lastname, custerr);
    1.48 +		} else {
    1.49 +			g_Logger.LogError("[SM] Native \"%s\" encountered a generic error.", lastname);
    1.50 +		}
    1.51 +	}
    1.52 +
    1.53 +	if (!error->DebugInfoAvailable())
    1.54 +	{
    1.55 +		g_Logger.LogError("[SM] Debug mode is not enabled for this plugin.");
    1.56 +		g_Logger.LogError("[SM] To enable debug mode, edit plugin_settings.cfg, or type: sm plugins debug %d on",
    1.57 +			_GetPluginIndex(ctx));
    1.58 +		return;
    1.59 +	}
    1.60 +
    1.61 +	CallStackInfo stk_info;
    1.62 +	int i = 0;
    1.63 +	g_Logger.LogError("[SM] Displaying call stack trace:");
    1.64 +	while (error->GetTraceInfo(&stk_info))
    1.65 +	{
    1.66 +		g_Logger.LogError("[SM]   [%d]  Line %d, %s::%s()",
    1.67 +			i++,
    1.68 +			stk_info.line,
    1.69 +			stk_info.filename,
    1.70 +			stk_info.function);
    1.71 +	}
    1.72 +}
    1.73 +
    1.74 +int CDbgReporter::_GetPluginIndex(IPluginContext *ctx)
    1.75 +{
    1.76 +	int id = 1;
    1.77 +	IPluginIterator *iter = g_PluginSys.GetPluginIterator();
    1.78 +
    1.79 +	for (; iter->MorePlugins(); iter->NextPlugin(), id++)
    1.80 +	{
    1.81 +		IPlugin *pl = iter->GetPlugin();
    1.82 +		if (pl->GetBaseContext() == ctx)
    1.83 +		{
    1.84 +			iter->Release();
    1.85 +			return id;
    1.86 +		}
    1.87 +	}
    1.88 +
    1.89 +	iter->Release();
    1.90 +	return -1;
    1.91 +}
    1.92 +