Skip to content

Instantly share code, notes, and snippets.

@gesslar
Forked from jalbright015/error_handler.c
Last active July 25, 2022 21:23
Show Gist options
  • Save gesslar/587ad0bdbad2c0f752bbd91194ca07d5 to your computer and use it in GitHub Desktop.
Save gesslar/587ad0bdbad2c0f752bbd91194ca07d5 to your computer and use it in GitHub Desktop.
/*
(Date unknown) - Jezu@Astaria
Added error_handler() during porting to FluffOS
July 10, 2022 - Jezu@Astaria
- Refactored error_handler().
- Fixed bug in error_handler() that was incorrectly applying verbose_errors.
- Changed debug class for errors generated by wizards to be ':<wiz name>'. This is so wizzes can selectively enable which wizards they want to receive errors for.
*/
#include <uid.h>
#include <ansi.h>
#define OUTPUT ([ \
"file" : 0,\
"line" : 1,\
"program" : 2,\
"trace" : 3,\
"error" : 4,\
"arguments" : 5,\
"object" : 6,\
"interactive" : 7,\
"context" : 8,\
"function" : 9,\
"locals" : 10\
])
class Error {
string file;
int line;
string program;
mixed trace;
string error;
mixed *args;
object ob;
mixed interactive;
string uid;
string func;
mixed *locals;
}
class Output {
string file;
string line;
string program;
string trace;
string error;
string arguments;
string ob;
string interactive;
string context;
string func;
string locals;
}
class Error err;
class Output out;
nosave int has_error = 0;
nosave string last_error = "";
public string clear_last_error() {
last_error = "";
}
public string get_last_error() {
return last_error;
}
protected void error_handler(mapping map, int flag) {
string str, debug_class, file_lines, border, o;
function f;
mapping enabled_categories;
out = new(class Output);
err = new(class Error);
err->file = (string)map["file"];
err->line = (int)map["line"];
err->ob = (object)map["object"];
err->program = (string)map["program"];
err->trace = (mixed*)map["trace"];
if ( arrayp(err->trace) && mapp(err->trace[<1]) )
{
err->func = err->trace[<1]["function"];
err->args = err->trace[<1]["arguments"];
err->locals = err->trace[<1]["locals"];
}
err->error = (string)map["error"];
err->interactive = this_interactive() || this_player();
debug_class = "debug/";
border = repeat_string("-", 60);
str = sprintf("\n%s\n%s\n\n", border, ctime());
if (flag)
str += "*Error caught\n\n";
f = function(string s, mixed m){
return sprintf("\n%s%14s:%s %s", HIW, s, NOR, identify(m));
};
str += (*f)("Error", err->error[0..-2]);
if ( objectp(err->interactive) )
{
err->uid = getuid(err->interactive);
out->interactive = (*f)("Interactive", err->uid);
}
out->ob = (*f)("Object", err->ob);
out->program = (*f)("Program", err->program);
out->func = (*f)("Function", err->func);
out->arguments = (*f)("Arguments", err->args);
out->file = (*f)("File", err->file);
out->line = (*f)("Line", err->line);
out->trace = sprintf("\n\n\n%sTrace:%s\n\n%s", HIW, NOR,
implode(
map_array(
err->trace,
(: sprintf("Line: %O File: %O Function: %O Object: %O Program: %O",
(int)$1["line"],
(string)$1["file"],
(string)$1["function"],
(object)$1["object"] || "No object",
(string)$1["program"] || "No program") :)
), "\n"));
last_error = str;
if ( objectp(err->interactive) && wizardp(err->interactive) )
{
debug_class += ":"+err->uid;
write_file( sprintf("%sdebug.log", user_path(err->uid)), str);
}
else
{
debug_class += "master";
write_file("/log/debug.log", str);
}
f = function(int offset, int count) {
string lines = read_file( err->file, err->line+offset, count);
string s = "";
if ( !offset )
s = sprintf("%s%d:%s %s", HIR, err->line, NOR, lines);
else
foreach (string line in explode(lines, "\n"))
s += sprintf("%d: %s\n", err->line+offset++, line);
return s;
};
out->context = sprintf("\n\n\n%sContext:%s\n\n%s%s%s",
HIW,
NOR,
(*f)(-3, 3),
(*f)(0, 1),
(*f)(1, 3));
foreach(object wiz in wizards())
{
o = "";
enabled_categories = (mapping)wiz->query("debug_categories") || ([]);
foreach(string cat in (keys(enabled_categories)-({ "context", "trace" })))
{
if ( (int)enabled_categories[cat] )
o += fetch_class_member(out, OUTPUT[cat]);
}
if ( enabled_categories["trace"] )
o += fetch_class_member(out, OUTPUT["trace"]);
if ( enabled_categories["context"] )
o += fetch_class_member(out, OUTPUT["context"]);
message(debug_class, sprintf("%s\n%s\n", str+o, border), wiz);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment