Skip to content

Instantly share code, notes, and snippets.

@Zeex
Last active December 22, 2015 01:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Zeex/6395948 to your computer and use it in GitHub Desktop.
Save Zeex/6395948 to your computer and use it in GitHub Desktop.
CaptureBacktraceAsync() function
// Capturing AMX backtrce printed by CrashDetect to a string.
// Requires fixes2 plugin by Y_Less: http://forum.sa-mp.com/showthread.php?t=375925
#include <a_samp>
#if !defined MAX_PUBLIC_NAME
#define MAX_PUBLIC_NAME 32 // compiler-enforced limit
#endif
#if !defined MAX_BACKTRACE_LENGTH
#define MAX_BACKTRACE_LENGTH 10240
#endif
public FinishCaptureBacktrace();
static stock bool:g_capture = false;
static stock g_callback[MAX_PUBLIC_NAME];
static stock g_backtrace[MAX_BACKTRACE_LENGTH];
static stock strcpy(dst[], const src[], size = sizeof(dst)) {
dst[0] = '\0';
return strcat(dst, src, size);
}
static stock bool:StringBeginsWith(const s[], const what[]) {
return strcmp(s, what, _, strlen(what)) == 0;
}
static stock AppendMessage(buf[], const msg[], size = sizeof(buf)) {
new length = strcat(buf, msg, size);
strcat(buf[length - 1], "\n", size);
}
static stock IsEmptyString(const s[]) {
return s[0] == '\0';
}
static stock PrintBacktrace() {
#emit halt 1 // forced exit
}
stock CaptureBacktraceAsync(const callback[]) {
strcpy(g_callback, callback);
SetTimer("FinishCaptureBacktrace", 1, false);
PrintBacktrace();
}
public FinishCaptureBacktrace() {
if (g_capture) {
if (!IsEmptyString(g_backtrace)) {
CallLocalFunction(g_callback, "s", g_backtrace);
} else {
g_backtrace = "";
}
g_capture = false;
}
}
static stock ProcessServerMessage(const msg[]) {
if (!g_capture && StringBeginsWith(msg, "[debug] AMX backtrace")) {
g_capture = true;
}
if (g_capture && StringBeginsWith(msg, "[debug]")) {
AppendMessage(g_backtrace, msg);
}
}
// ALS v4: http://forum.sa-mp.com/showthread.php?t=441293
public OnServerMessage(const msg[]) {
ProcessServerMessage(msg);
#if defined BT_OnServerMessage
BT_OnServerMessage(msg);
#endif
return 1;
}
#if defined _ALS_OnServerMessage
#undef OnServerMessage
#else
#define _ALS_OnServerMessage
#endif
#define OnPlayerConnect BT_OnServerMessage
#if defined BT_OnServerMessage
forward BT_OnServerMessage(const msg[]);
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment