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