3 * ================================================================
5 * Copyright (C) 2004-2007 AlliedModders LLC. All rights reserved.
6 * ================================================================
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License,
10 * version 3.0, as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * As a special exception, AlliedModders LLC gives you permission to
21 * link the code of this program (as well as its derivative works) to
22 * "Half-Life 2," the "Source Engine," the "SourcePawn JIT," and any
23 * Game MODs that run on software by the Valve Corporation. You must
24 * obey the GNU General Public License in all respects for all other
25 * code used. Additionally, AlliedModders LLC grants this exception
26 * to all derivative works. AlliedModders LLC defines further
27 * exceptions, found in LICENSE.txt (as of this writing, version
28 * JULY-31-2007), or <http://www.sourcemod.net/license.php>.
33 #include "DebugReporter.h"
35 #include "PluginSys.h"
36 #include "sm_stringutil.h"
38 DebugReport g_DbgReporter;
40 /* I'm really lazy. This should probably be exported to ISourcePawnEngine someday,
41 * but we need to make sure the JIT will deal with the version bump.
43 extern const char *GetSourcePawnErrorMessage(int error);
45 void DebugReport::OnSourceModAllInitialized()
47 g_pSourcePawn->SetDebugListener(this);
50 void DebugReport::GenerateError(IPluginContext *ctx, cell_t func_idx, int err, const char *message, ...)
55 va_start(ap, message);
56 UTIL_FormatArgs(buffer, sizeof(buffer), message, ap);
59 const char *plname = g_PluginSys.FindPluginByContext(ctx->GetContext())->GetFilename();
60 const char *error = GetSourcePawnErrorMessage(err);
64 g_Logger.LogError("[SM] Plugin \"%s\" encountered error %d: %s", plname, err, error);
66 g_Logger.LogError("[SM] Plugin \"%s\" encountered unknown error %d", plname, err);
69 g_Logger.LogError("[SM] %s", buffer);
76 sp_public_t *function;
77 if (ctx->GetPublicByIndex(func_idx, &function) == SP_ERROR_NONE)
79 g_Logger.LogError("[SM] Unable to call function \"%s\" due to above errors.", function->name);
85 void DebugReport::OnContextExecuteError(IPluginContext *ctx, IContextTrace *error)
88 const char *plname = g_PluginSys.FindPluginByContext(ctx->GetContext())->GetFilename();
89 int n_err = error->GetErrorCode();
91 if (n_err != SP_ERROR_NATIVE)
93 g_Logger.LogError("[SM] Plugin encountered error %d: %s",
95 error->GetErrorString());
98 if ((lastname=error->GetLastNative(NULL)) != NULL)
101 if ((custerr=error->GetCustomErrorString()) != NULL)
103 g_Logger.LogError("[SM] Native \"%s\" reported: %s", lastname, custerr);
105 g_Logger.LogError("[SM] Native \"%s\" encountered a generic error.", lastname);
109 if (!error->DebugInfoAvailable())
111 g_Logger.LogError("[SM] Debug mode is not enabled for \"%s\"", plname);
112 g_Logger.LogError("[SM] To enable debug mode, edit plugin_settings.cfg, or type: sm plugins debug %d on",
113 _GetPluginIndex(ctx));
117 CallStackInfo stk_info;
119 g_Logger.LogError("[SM] Displaying call stack trace:");
120 while (error->GetTraceInfo(&stk_info))
122 g_Logger.LogError("[SM] [%d] Line %d, %s::%s()",
130 int DebugReport::_GetPluginIndex(IPluginContext *ctx)
133 IPluginIterator *iter = g_PluginSys.GetPluginIterator();
135 for (; iter->MorePlugins(); iter->NextPlugin(), id++)
137 IPlugin *pl = iter->GetPlugin();
138 if (pl->GetBaseContext() == ctx)