core/DebugReporter.cpp
author David Anderson <dvander@alliedmods.net>
Sun Jun 17 07:32:31 2007 +0000 (2007-06-17)
changeset 969 0cd7fc27e054
parent 653 ef60870067ff
child 1036 2a073d5b3454
permissions -rw-r--r--
fixed a state bug in the gameconf parser that would break parsing after a signature section was done
     1 /**
     2  * vim: set ts=4 :
     3  * ===============================================================
     4  * SourceMod (C)2004-2007 AlliedModders LLC.  All rights reserved.
     5  * ===============================================================
     6  *
     7  * This file is not open source and may not be copied without explicit
     8  * written permission of AlliedModders LLC.  This file may not be redistributed 
     9  * in whole or significant part.
    10  * For information, see LICENSE.txt or http://www.sourcemod.net/license.php
    11  *
    12  * Version: $Id$
    13  */
    14 
    15 #include "DebugReporter.h"
    16 #include "Logger.h"
    17 #include "PluginSys.h"
    18 
    19 DebugReport g_DbgReporter;
    20 
    21 void DebugReport::OnSourceModAllInitialized()
    22 {
    23 	g_pSourcePawn->SetDebugListener(this);
    24 }
    25 
    26 void DebugReport::OnContextExecuteError(IPluginContext *ctx, IContextTrace *error)
    27 {
    28 	const char *lastname;
    29 	const char *plname = g_PluginSys.FindPluginByContext(ctx->GetContext())->GetFilename();
    30 	int n_err = error->GetErrorCode();
    31 
    32 	if (n_err != SP_ERROR_NATIVE)
    33 	{
    34 		g_Logger.LogError("[SM] Plugin \"%s\" encountered error %d: %s",
    35 			plname,
    36 			n_err,
    37 			error->GetErrorString());
    38 	}
    39 
    40 	if ((lastname=error->GetLastNative(NULL)) != NULL)
    41 	{
    42 		const char *custerr;
    43 		if ((custerr=error->GetCustomErrorString()) != NULL)
    44 		{
    45 			g_Logger.LogError("[SM] Native \"%s\" reported: %s", lastname, custerr);
    46 		} else {
    47 			g_Logger.LogError("[SM] Native \"%s\" encountered a generic error.", lastname);
    48 		}
    49 	}
    50 
    51 	if (!error->DebugInfoAvailable())
    52 	{
    53 		g_Logger.LogError("[SM] Debug mode is not enabled for this plugin.");
    54 		g_Logger.LogError("[SM] To enable debug mode, edit plugin_settings.cfg, or type: sm plugins debug %d on",
    55 			_GetPluginIndex(ctx));
    56 		return;
    57 	}
    58 
    59 	CallStackInfo stk_info;
    60 	int i = 0;
    61 	g_Logger.LogError("[SM] Displaying call stack trace:");
    62 	while (error->GetTraceInfo(&stk_info))
    63 	{
    64 		g_Logger.LogError("[SM]   [%d]  Line %d, %s::%s()",
    65 			i++,
    66 			stk_info.line,
    67 			stk_info.filename,
    68 			stk_info.function);
    69 	}
    70 }
    71 
    72 int DebugReport::_GetPluginIndex(IPluginContext *ctx)
    73 {
    74 	int id = 1;
    75 	IPluginIterator *iter = g_PluginSys.GetPluginIterator();
    76 
    77 	for (; iter->MorePlugins(); iter->NextPlugin(), id++)
    78 	{
    79 		IPlugin *pl = iter->GetPlugin();
    80 		if (pl->GetBaseContext() == ctx)
    81 		{
    82 			iter->Release();
    83 			return id;
    84 		}
    85 	}
    86 
    87 	iter->Release();
    88 	return -1;
    89 }
    90