core/DebugReporter.cpp
changeset 3792 d8a07e40bcb6
parent 3791 47e1ee9cea86
child 3793 7818566b29f4
     1.1 --- a/core/DebugReporter.cpp	Sat Feb 16 15:52:11 2013 -0800
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,201 +0,0 @@
     1.4 -/**
     1.5 - * vim: set ts=4 :
     1.6 - * =============================================================================
     1.7 - * SourceMod
     1.8 - * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
     1.9 - * =============================================================================
    1.10 - *
    1.11 - * This program is free software; you can redistribute it and/or modify it under
    1.12 - * the terms of the GNU General Public License, version 3.0, as published by the
    1.13 - * Free Software Foundation.
    1.14 - * 
    1.15 - * This program is distributed in the hope that it will be useful, but WITHOUT
    1.16 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    1.17 - * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    1.18 - * details.
    1.19 - *
    1.20 - * You should have received a copy of the GNU General Public License along with
    1.21 - * this program.  If not, see <http://www.gnu.org/licenses/>.
    1.22 - *
    1.23 - * As a special exception, AlliedModders LLC gives you permission to link the
    1.24 - * code of this program (as well as its derivative works) to "Half-Life 2," the
    1.25 - * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    1.26 - * by the Valve Corporation.  You must obey the GNU General Public License in
    1.27 - * all respects for all other code used.  Additionally, AlliedModders LLC grants
    1.28 - * this exception to all derivative works.  AlliedModders LLC defines further
    1.29 - * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    1.30 - * or <http://www.sourcemod.net/license.php>.
    1.31 - *
    1.32 - * Version: $Id$
    1.33 - */
    1.34 -
    1.35 -#include "DebugReporter.h"
    1.36 -#include "Logger.h"
    1.37 -#include "PluginSys.h"
    1.38 -#include "sm_stringutil.h"
    1.39 -
    1.40 -DebugReport g_DbgReporter;
    1.41 -
    1.42 -void DebugReport::OnSourceModAllInitialized()
    1.43 -{
    1.44 -	g_pSourcePawn->SetDebugListener(this);
    1.45 -}
    1.46 -
    1.47 -void DebugReport::OnDebugSpew(const char *msg, ...)
    1.48 -{
    1.49 -	va_list ap;
    1.50 -	char buffer[512];
    1.51 -
    1.52 -	va_start(ap, msg);
    1.53 -	UTIL_FormatArgs(buffer, sizeof(buffer), msg, ap);
    1.54 -	va_end(ap);
    1.55 -
    1.56 -	g_Logger.LogMessage("[SM] %s", buffer);
    1.57 -}
    1.58 -
    1.59 -void DebugReport::GenerateError(IPluginContext *ctx, cell_t func_idx, int err, const char *message, ...)
    1.60 -{
    1.61 -	va_list ap;
    1.62 -
    1.63 -	va_start(ap, message);
    1.64 -	GenerateErrorVA(ctx, func_idx, err, message, ap);
    1.65 -	va_end(ap);
    1.66 -}
    1.67 -
    1.68 -void DebugReport::GenerateErrorVA(IPluginContext *ctx, cell_t func_idx, int err, const char *message, va_list ap)
    1.69 -{
    1.70 -	char buffer[512];
    1.71 -	UTIL_FormatArgs(buffer, sizeof(buffer), message, ap);
    1.72 -
    1.73 -	const char *plname = g_PluginSys.FindPluginByContext(ctx->GetContext())->GetFilename();
    1.74 -	const char *error = g_pSourcePawn2->GetErrorString(err);
    1.75 -
    1.76 -	if (error)
    1.77 -	{
    1.78 -		g_Logger.LogError("[SM] Plugin \"%s\" encountered error %d: %s", plname, err, error);
    1.79 -	} else {
    1.80 -		g_Logger.LogError("[SM] Plugin \"%s\" encountered unknown error %d", plname, err);
    1.81 -	}
    1.82 -
    1.83 -	g_Logger.LogError("[SM] %s", buffer);
    1.84 -
    1.85 -	if (func_idx != -1)
    1.86 -	{
    1.87 -		if (func_idx & 1)
    1.88 -		{
    1.89 -			func_idx >>= 1;
    1.90 -			sp_public_t *function;
    1.91 -			if (ctx->GetRuntime()->GetPublicByIndex(func_idx, &function) == SP_ERROR_NONE)
    1.92 -			{
    1.93 -				g_Logger.LogError("[SM] Unable to call function \"%s\" due to above error(s).", function->name);
    1.94 -			}
    1.95 -		}
    1.96 -	}
    1.97 -}
    1.98 -
    1.99 -void DebugReport::GenerateCodeError(IPluginContext *pContext, uint32_t code_addr, int err, const char *message, ...)
   1.100 -{
   1.101 -	va_list ap;
   1.102 -	char buffer[512];
   1.103 -
   1.104 -	va_start(ap, message);
   1.105 -	UTIL_FormatArgs(buffer, sizeof(buffer), message, ap);
   1.106 -	va_end(ap);
   1.107 -
   1.108 -	const char *plname = g_PluginSys.FindPluginByContext(pContext->GetContext())->GetFilename();
   1.109 -	const char *error = g_pSourcePawn2->GetErrorString(err);
   1.110 -
   1.111 -	if (error)
   1.112 -	{
   1.113 -		g_Logger.LogError("[SM] Plugin \"%s\" encountered error %d: %s", plname, err, error);
   1.114 -	} else {
   1.115 -		g_Logger.LogError("[SM] Plugin \"%s\" encountered unknown error %d", plname, err);
   1.116 -	}
   1.117 -
   1.118 -	g_Logger.LogError("[SM] %s", buffer);
   1.119 -
   1.120 -	IPluginDebugInfo *pDebug;
   1.121 -	if ((pDebug = pContext->GetRuntime()->GetDebugInfo()) == NULL)
   1.122 -	{
   1.123 -		g_Logger.LogError("[SM] Debug mode is not enabled for \"%s\"", plname);
   1.124 -		g_Logger.LogError("[SM] To enable debug mode, edit plugin_settings.cfg, or type: sm plugins debug %d on",
   1.125 -			_GetPluginIndex(pContext));
   1.126 -		return;
   1.127 -	}
   1.128 -
   1.129 -	const char *name;
   1.130 -	if (pDebug->LookupFunction(code_addr, &name) == SP_ERROR_NONE)
   1.131 -	{
   1.132 -		g_Logger.LogError("[SM] Unable to call function \"%s\" due to above error(s).", name);
   1.133 -	} else {
   1.134 -		g_Logger.LogError("[SM] Unable to call function (name unknown, address \"%x\").", code_addr);
   1.135 -	}
   1.136 -}
   1.137 -
   1.138 -void DebugReport::OnContextExecuteError(IPluginContext *ctx, IContextTrace *error)
   1.139 -{
   1.140 -	const char *lastname;
   1.141 -	const char *plname = g_PluginSys.FindPluginByContext(ctx->GetContext())->GetFilename();
   1.142 -	int n_err = error->GetErrorCode();
   1.143 -
   1.144 -	if (n_err != SP_ERROR_NATIVE)
   1.145 -	{
   1.146 -		g_Logger.LogError("[SM] Plugin encountered error %d: %s",
   1.147 -			n_err,
   1.148 -			error->GetErrorString());
   1.149 -	}
   1.150 -
   1.151 -	if ((lastname=error->GetLastNative(NULL)) != NULL)
   1.152 -	{
   1.153 -		const char *custerr;
   1.154 -		if ((custerr=error->GetCustomErrorString()) != NULL)
   1.155 -		{
   1.156 -			g_Logger.LogError("[SM] Native \"%s\" reported: %s", lastname, custerr);
   1.157 -		} else {
   1.158 -			g_Logger.LogError("[SM] Native \"%s\" encountered a generic error.", lastname);
   1.159 -		}
   1.160 -	}
   1.161 -
   1.162 -	if (!error->DebugInfoAvailable())
   1.163 -	{
   1.164 -		g_Logger.LogError("[SM] Debug mode is not enabled for \"%s\"", plname);
   1.165 -		g_Logger.LogError("[SM] To enable debug mode, edit plugin_settings.cfg, or type: sm plugins debug %d on",
   1.166 -			_GetPluginIndex(ctx));
   1.167 -		return;
   1.168 -	}
   1.169 -
   1.170 -	CallStackInfo stk_info;
   1.171 -	int i = 0;
   1.172 -	g_Logger.LogError("[SM] Displaying call stack trace for plugin \"%s\":", plname);
   1.173 -	while (error->GetTraceInfo(&stk_info))
   1.174 -	{
   1.175 -		g_Logger.LogError("[SM]   [%d]  Line %d, %s::%s()",
   1.176 -			i++,
   1.177 -			stk_info.line,
   1.178 -			stk_info.filename,
   1.179 -			stk_info.function);
   1.180 -	}
   1.181 -}
   1.182 -
   1.183 -int DebugReport::_GetPluginIndex(IPluginContext *ctx)
   1.184 -{
   1.185 -	int id = 1;
   1.186 -	IPluginIterator *iter = g_PluginSys.GetPluginIterator();
   1.187 -
   1.188 -	for (; iter->MorePlugins(); iter->NextPlugin(), id++)
   1.189 -	{
   1.190 -		IPlugin *pl = iter->GetPlugin();
   1.191 -		if (pl->GetBaseContext() == ctx)
   1.192 -		{
   1.193 -			iter->Release();
   1.194 -			return id;
   1.195 -		}
   1.196 -	}
   1.197 -
   1.198 -	iter->Release();
   1.199 -
   1.200 -	/* If we don't know which plugin this is, it's one being loaded.  Fake its index for now. */
   1.201 -
   1.202 -	return g_PluginSys.GetPluginCount() + 1;
   1.203 -}
   1.204 -