core/CDbgReporter.cpp
author Scott Ehlert <ds@alliedmods.net>
Thu Feb 08 21:41:28 2007 +0000 (2007-02-08)
changeset 457 9b8a85bf4175
parent 370 d3a484f98f6d
child 462 014aad37c7b4
permissions -rw-r--r--
Initial import of convar stuff
1) Natives to create, find, set, and get convars
2) "cvars" option added to sm menu
3) Some internal additions to CPlugin to store a convar list
Still some things left to do...
dvander@370
     1
/**
dvander@370
     2
 * ===============================================================
dvander@370
     3
 * SourceMod (C)2004-2007 AlliedModders LLC.  All rights reserved.
dvander@370
     4
 * ===============================================================
dvander@370
     5
 *
dvander@370
     6
 * This file is not open source and may not be copied without explicit
dvander@370
     7
 * written permission of AlliedModders LLC.  This file may not be redistributed 
dvander@370
     8
 * in whole or significant part.
dvander@370
     9
 * For information, see LICENSE.txt or http://www.sourcemod.net/license.php
dvander@370
    10
 *
dvander@370
    11
 * Version: $Id$
dvander@370
    12
 */
dvander@370
    13
faluco@288
    14
#include "CDbgReporter.h"
faluco@288
    15
#include "CLogger.h"
faluco@288
    16
#include "PluginSys.h"
faluco@288
    17
faluco@288
    18
void CDbgReporter::OnSourceModAllInitialized()
faluco@288
    19
{
faluco@288
    20
	g_pSourcePawn->SetDebugListener(this);
faluco@288
    21
}
faluco@288
    22
faluco@288
    23
void CDbgReporter::OnContextExecuteError(IPluginContext *ctx, IContextTrace *error)
faluco@288
    24
{
faluco@288
    25
	const char *lastname;
faluco@288
    26
	const char *plname = g_PluginSys.FindPluginByContext(ctx->GetContext())->GetFilename();
faluco@288
    27
	int n_err = error->GetErrorCode();
faluco@288
    28
faluco@288
    29
	if (n_err != SP_ERROR_NATIVE)
faluco@288
    30
	{
dvander@307
    31
		g_Logger.LogError("[SM] Plugin \"%s\" encountered error %d: %s",
faluco@288
    32
			plname,
faluco@288
    33
			n_err,
faluco@288
    34
			error->GetErrorString());
faluco@288
    35
	}
faluco@288
    36
faluco@288
    37
	if ((lastname=error->GetLastNative(NULL)) != NULL)
faluco@288
    38
	{
faluco@288
    39
		const char *custerr;
faluco@288
    40
		if ((custerr=error->GetCustomErrorString()) != NULL)
faluco@288
    41
		{
dvander@307
    42
			g_Logger.LogError("[SM] Native \"%s\" reported: \"%s\"", lastname, custerr);
faluco@288
    43
		} else {
dvander@307
    44
			g_Logger.LogError("[SM] Native \"%s\" encountered a generic error.", lastname);
faluco@288
    45
		}
faluco@288
    46
	}
faluco@288
    47
faluco@288
    48
	if (!error->DebugInfoAvailable())
faluco@288
    49
	{
dvander@307
    50
		g_Logger.LogError("[SM] Debug mode is not enabled for this plugin.");
dvander@307
    51
		g_Logger.LogError("[SM] To enable debug mode, edit plugin_settings.cfg, or type: sm plugins debug %d on",
faluco@288
    52
			_GetPluginIndex(ctx));
faluco@288
    53
		return;
faluco@288
    54
	}
faluco@288
    55
faluco@288
    56
	CallStackInfo stk_info;
faluco@288
    57
	int i = 0;
dvander@307
    58
	g_Logger.LogError("[SM] Displaying call stack trace:");
faluco@288
    59
	while (error->GetTraceInfo(&stk_info))
faluco@288
    60
	{
dvander@307
    61
		g_Logger.LogError("[SM]   [%d]  Line %d, %s::%s()",
faluco@288
    62
			i++,
faluco@288
    63
			stk_info.line,
faluco@288
    64
			stk_info.filename,
faluco@288
    65
			stk_info.function);
faluco@288
    66
	}
faluco@288
    67
}
faluco@288
    68
faluco@288
    69
int CDbgReporter::_GetPluginIndex(IPluginContext *ctx)
faluco@288
    70
{
faluco@288
    71
	int id = 1;
faluco@288
    72
	IPluginIterator *iter = g_PluginSys.GetPluginIterator();
faluco@288
    73
faluco@288
    74
	for (; iter->MorePlugins(); iter->NextPlugin(), id++)
faluco@288
    75
	{
faluco@288
    76
		IPlugin *pl = iter->GetPlugin();
faluco@288
    77
		if (pl->GetBaseContext() == ctx)
faluco@288
    78
		{
faluco@288
    79
			iter->Release();
faluco@288
    80
			return id;
faluco@288
    81
		}
faluco@288
    82
	}
faluco@288
    83
faluco@288
    84
	iter->Release();
faluco@288
    85
	return -1;
dvander@351
    86
}
dvander@351
    87