core/CDbgReporter.cpp
author David Anderson <dvander@alliedmods.net>
Sat Feb 17 19:03:18 2007 +0000 (2007-02-17)
changeset 520 7676d61cde2d
parent 469 e9a53be35abd
permissions -rw-r--r--
added helper a core translation helper to the translator
regadmincmd is now officially done and tested, as are overrides
printtoplayers now prints to the server on id==0 as a convenience
fixed a crash bug in the admin system
fixed up console.inc a bit
     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 CDbgReporter g_DbgReporter;
    19 
    20 void CDbgReporter::OnSourceModAllInitialized()
    21 {
    22 	g_pSourcePawn->SetDebugListener(this);
    23 }
    24 
    25 void CDbgReporter::OnContextExecuteError(IPluginContext *ctx, IContextTrace *error)
    26 {
    27 	const char *lastname;
    28 	const char *plname = g_PluginSys.FindPluginByContext(ctx->GetContext())->GetFilename();
    29 	int n_err = error->GetErrorCode();
    30 
    31 	if (n_err != SP_ERROR_NATIVE)
    32 	{
    33 		g_Logger.LogError("[SM] Plugin \"%s\" encountered error %d: %s",
    34 			plname,
    35 			n_err,
    36 			error->GetErrorString());
    37 	}
    38 
    39 	if ((lastname=error->GetLastNative(NULL)) != NULL)
    40 	{
    41 		const char *custerr;
    42 		if ((custerr=error->GetCustomErrorString()) != NULL)
    43 		{
    44 			g_Logger.LogError("[SM] Native \"%s\" reported: %s", lastname, custerr);
    45 		} else {
    46 			g_Logger.LogError("[SM] Native \"%s\" encountered a generic error.", lastname);
    47 		}
    48 	}
    49 
    50 	if (!error->DebugInfoAvailable())
    51 	{
    52 		g_Logger.LogError("[SM] Debug mode is not enabled for this plugin.");
    53 		g_Logger.LogError("[SM] To enable debug mode, edit plugin_settings.cfg, or type: sm plugins debug %d on",
    54 			_GetPluginIndex(ctx));
    55 		return;
    56 	}
    57 
    58 	CallStackInfo stk_info;
    59 	int i = 0;
    60 	g_Logger.LogError("[SM] Displaying call stack trace:");
    61 	while (error->GetTraceInfo(&stk_info))
    62 	{
    63 		g_Logger.LogError("[SM]   [%d]  Line %d, %s::%s()",
    64 			i++,
    65 			stk_info.line,
    66 			stk_info.filename,
    67 			stk_info.function);
    68 	}
    69 }
    70 
    71 int CDbgReporter::_GetPluginIndex(IPluginContext *ctx)
    72 {
    73 	int id = 1;
    74 	IPluginIterator *iter = g_PluginSys.GetPluginIterator();
    75 
    76 	for (; iter->MorePlugins(); iter->NextPlugin(), id++)
    77 	{
    78 		IPlugin *pl = iter->GetPlugin();
    79 		if (pl->GetBaseContext() == ctx)
    80 		{
    81 			iter->Release();
    82 			return id;
    83 		}
    84 	}
    85 
    86 	iter->Release();
    87 	return -1;
    88 }
    89