Annotate

util/debug.lua @ 4525:e00b4ec5fca4

util.debug: Move boundary markers to top of relevant sections of the stack trace (easier to follow)
author Matthew Wild <mwild1@gmail.com>
date Thu, 22 Mar 2012 16:51:39 +0000
parent 4524:816c319a09d8
child 4684:dc70c4ffb66d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4412
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 -- Variables ending with these names will not
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 -- have their values printed ('password' includes
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 -- 'new_password', etc.)
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 local censored_names = {
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 password = true;
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 passwd = true;
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 pass = true;
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 pwd = true;
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 };
4522
29f75c2af90e util.debug: Move optimal line length (default 65) into a variable
Matthew Wild <mwild1@gmail.com>
parents: 4521
diff changeset
10 local optimal_line_length = 65;
29f75c2af90e util.debug: Move optimal line length (default 65) into a variable
Matthew Wild <mwild1@gmail.com>
parents: 4521
diff changeset
11
4523
d733bde93074 util.debug: Add a bit of colour
Matthew Wild <mwild1@gmail.com>
parents: 4522
diff changeset
12 local termcolours = require "util.termcolours";
d733bde93074 util.debug: Add a bit of colour
Matthew Wild <mwild1@gmail.com>
parents: 4522
diff changeset
13 local getstring = termcolours.getstring;
d733bde93074 util.debug: Add a bit of colour
Matthew Wild <mwild1@gmail.com>
parents: 4522
diff changeset
14 local styles;
d733bde93074 util.debug: Add a bit of colour
Matthew Wild <mwild1@gmail.com>
parents: 4522
diff changeset
15 do
d733bde93074 util.debug: Add a bit of colour
Matthew Wild <mwild1@gmail.com>
parents: 4522
diff changeset
16 _ = termcolours.getstyle;
d733bde93074 util.debug: Add a bit of colour
Matthew Wild <mwild1@gmail.com>
parents: 4522
diff changeset
17 styles = {
d733bde93074 util.debug: Add a bit of colour
Matthew Wild <mwild1@gmail.com>
parents: 4522
diff changeset
18 boundary_padding = _("bright", "white");
d733bde93074 util.debug: Add a bit of colour
Matthew Wild <mwild1@gmail.com>
parents: 4522
diff changeset
19 filename = _("bright", "blue");
d733bde93074 util.debug: Add a bit of colour
Matthew Wild <mwild1@gmail.com>
parents: 4522
diff changeset
20 level_num = _("green");
d733bde93074 util.debug: Add a bit of colour
Matthew Wild <mwild1@gmail.com>
parents: 4522
diff changeset
21 funcname = _("yellow");
4524
816c319a09d8 util.debug: Add a touch of colour to source locations
Matthew Wild <mwild1@gmail.com>
parents: 4523
diff changeset
22 location = _("yellow");
4523
d733bde93074 util.debug: Add a bit of colour
Matthew Wild <mwild1@gmail.com>
parents: 4522
diff changeset
23 };
d733bde93074 util.debug: Add a bit of colour
Matthew Wild <mwild1@gmail.com>
parents: 4522
diff changeset
24 end
4412
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 local function get_locals_table(level)
4465
41c4252526bd util.debug: Adjust level within get_locals_table() to account for the additional depth of this function itself
Matthew Wild <mwild1@gmail.com>
parents: 4418
diff changeset
27 level = level + 1; -- Skip this function itself
4412
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 local locals = {};
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 for local_num = 1, math.huge do
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 local name, value = debug.getlocal(level, local_num);
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 if not name then break; end
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 table.insert(locals, { name = name, value = value });
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 end
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 return locals;
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 end
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 local function get_upvalues_table(func)
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 local upvalues = {};
4418
70b5e533325d util.debug: Fix potential traceback
Matthew Wild <mwild1@gmail.com>
parents: 4412
diff changeset
39 if func then
70b5e533325d util.debug: Fix potential traceback
Matthew Wild <mwild1@gmail.com>
parents: 4412
diff changeset
40 for upvalue_num = 1, math.huge do
70b5e533325d util.debug: Fix potential traceback
Matthew Wild <mwild1@gmail.com>
parents: 4412
diff changeset
41 local name, value = debug.getupvalue(func, upvalue_num);
70b5e533325d util.debug: Fix potential traceback
Matthew Wild <mwild1@gmail.com>
parents: 4412
diff changeset
42 if not name then break; end
70b5e533325d util.debug: Fix potential traceback
Matthew Wild <mwild1@gmail.com>
parents: 4412
diff changeset
43 table.insert(upvalues, { name = name, value = value });
70b5e533325d util.debug: Fix potential traceback
Matthew Wild <mwild1@gmail.com>
parents: 4412
diff changeset
44 end
4412
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 end
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 return upvalues;
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 end
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 local function string_from_var_table(var_table, max_line_len, indent_str)
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 local var_string = {};
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 local col_pos = 0;
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 max_line_len = max_line_len or math.huge;
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 indent_str = "\n"..(indent_str or "");
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 for _, var in ipairs(var_table) do
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 local name, value = var.name, var.value;
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 if name:sub(1,1) ~= "(" then
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 if type(value) == "string" then
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 if censored_names[name:match("%a+$")] then
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 value = "<hidden>";
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 else
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 value = ("%q"):format(value);
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 end
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 else
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 value = tostring(value);
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 end
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 if #value > max_line_len then
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 value = value:sub(1, max_line_len-3).."…";
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 end
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 local str = ("%s = %s"):format(name, tostring(value));
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 col_pos = col_pos + #str;
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71 if col_pos > max_line_len then
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 table.insert(var_string, indent_str);
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 col_pos = 0;
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74 end
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 table.insert(var_string, str);
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76 end
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 end
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 if #var_string == 0 then
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79 return nil;
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80 else
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81 return "{ "..table.concat(var_string, ", "):gsub(indent_str..", ", indent_str).." }";
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82 end
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
83 end
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85 function get_traceback_table(thread, start_level)
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86 local levels = {};
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87 for level = start_level, math.huge do
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88 local info;
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
89 if thread then
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90 info = debug.getinfo(thread, level);
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
91 else
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
92 info = debug.getinfo(level);
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
93 end
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
94 if not info then break; end
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
95
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
96 levels[(level-start_level)+1] = {
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
97 level = level;
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
98 info = info;
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
99 locals = get_locals_table(level);
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
100 upvalues = get_upvalues_table(info.func);
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
101 };
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
102 end
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
103 return levels;
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
104 end
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
105
4520
2d85ac26799c util.debug: Add catch for an "error in error handling"
Matthew Wild <mwild1@gmail.com>
parents: 4519
diff changeset
106 function debug.traceback(...)
2d85ac26799c util.debug: Add catch for an "error in error handling"
Matthew Wild <mwild1@gmail.com>
parents: 4519
diff changeset
107 local ok, ret = pcall(debug._traceback, ...);
2d85ac26799c util.debug: Add catch for an "error in error handling"
Matthew Wild <mwild1@gmail.com>
parents: 4519
diff changeset
108 if not ok then
2d85ac26799c util.debug: Add catch for an "error in error handling"
Matthew Wild <mwild1@gmail.com>
parents: 4519
diff changeset
109 return "Error in error handling: "..ret;
2d85ac26799c util.debug: Add catch for an "error in error handling"
Matthew Wild <mwild1@gmail.com>
parents: 4519
diff changeset
110 end
2d85ac26799c util.debug: Add catch for an "error in error handling"
Matthew Wild <mwild1@gmail.com>
parents: 4519
diff changeset
111 return ret;
2d85ac26799c util.debug: Add catch for an "error in error handling"
Matthew Wild <mwild1@gmail.com>
parents: 4519
diff changeset
112 end
2d85ac26799c util.debug: Add catch for an "error in error handling"
Matthew Wild <mwild1@gmail.com>
parents: 4519
diff changeset
113
4523
d733bde93074 util.debug: Add a bit of colour
Matthew Wild <mwild1@gmail.com>
parents: 4522
diff changeset
114 local function build_source_boundary_marker(last_source_desc)
d733bde93074 util.debug: Add a bit of colour
Matthew Wild <mwild1@gmail.com>
parents: 4522
diff changeset
115 local padding = string.rep("-", math.floor(((optimal_line_length - 6) - #last_source_desc)/2));
4525
e00b4ec5fca4 util.debug: Move boundary markers to top of relevant sections of the stack trace (easier to follow)
Matthew Wild <mwild1@gmail.com>
parents: 4524
diff changeset
116 return getstring(styles.boundary_padding, "v"..padding).." "..getstring(styles.filename, last_source_desc).." "..getstring(styles.boundary_padding, padding..(#last_source_desc%2==0 and "-v" or "v "));
4523
d733bde93074 util.debug: Add a bit of colour
Matthew Wild <mwild1@gmail.com>
parents: 4522
diff changeset
117 end
d733bde93074 util.debug: Add a bit of colour
Matthew Wild <mwild1@gmail.com>
parents: 4522
diff changeset
118
4520
2d85ac26799c util.debug: Add catch for an "error in error handling"
Matthew Wild <mwild1@gmail.com>
parents: 4519
diff changeset
119 function debug._traceback(thread, message, level)
4412
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
120 if type(thread) ~= "thread" then
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
121 thread, message, level = coroutine.running(), thread, message;
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
122 end
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
123 if level and type(message) ~= "string" then
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
124 return nil, "invalid message";
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
125 elseif not level then
4519
510f4ba7080f util.debug: Attempt to get debug.traceback() parameter handling correct (again)
Matthew Wild <mwild1@gmail.com>
parents: 4465
diff changeset
126 if type(message) == "number" then
510f4ba7080f util.debug: Attempt to get debug.traceback() parameter handling correct (again)
Matthew Wild <mwild1@gmail.com>
parents: 4465
diff changeset
127 level, message = message, nil;
510f4ba7080f util.debug: Attempt to get debug.traceback() parameter handling correct (again)
Matthew Wild <mwild1@gmail.com>
parents: 4465
diff changeset
128 else
510f4ba7080f util.debug: Attempt to get debug.traceback() parameter handling correct (again)
Matthew Wild <mwild1@gmail.com>
parents: 4465
diff changeset
129 level = 2;
510f4ba7080f util.debug: Attempt to get debug.traceback() parameter handling correct (again)
Matthew Wild <mwild1@gmail.com>
parents: 4465
diff changeset
130 end
4412
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
131 end
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
132
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
133 message = message and (message.."\n") or "";
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
134
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
135 local levels = get_traceback_table(thread, level+2);
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
136
4521
4c7495f7f543 util.debug: Add markers in the output when crossing source file boundaries
Matthew Wild <mwild1@gmail.com>
parents: 4520
diff changeset
137 local last_source_desc;
4c7495f7f543 util.debug: Add markers in the output when crossing source file boundaries
Matthew Wild <mwild1@gmail.com>
parents: 4520
diff changeset
138
4412
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
139 local lines = {};
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
140 for nlevel, level in ipairs(levels) do
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
141 local info = level.info;
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
142 local line = "...";
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
143 local func_type = info.namewhat.." ";
4521
4c7495f7f543 util.debug: Add markers in the output when crossing source file boundaries
Matthew Wild <mwild1@gmail.com>
parents: 4520
diff changeset
144 local source_desc = (info.short_src == "[C]" and "C code") or info.short_src or "Unknown";
4412
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
145 if func_type == " " then func_type = ""; end;
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
146 if info.short_src == "[C]" then
4524
816c319a09d8 util.debug: Add a touch of colour to source locations
Matthew Wild <mwild1@gmail.com>
parents: 4523
diff changeset
147 line = "[ C ] "..func_type.."C function "..getstring(styles.location, (info.name and ("%q"):format(info.name) or "(unknown name)"));
4412
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
148 elseif info.what == "main" then
4524
816c319a09d8 util.debug: Add a touch of colour to source locations
Matthew Wild <mwild1@gmail.com>
parents: 4523
diff changeset
149 line = "[Lua] "..getstring(styles.location, info.short_src.." line "..info.currentline);
4412
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
150 else
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
151 local name = info.name or " ";
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
152 if name ~= " " then
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
153 name = ("%q"):format(name);
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
154 end
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
155 if func_type == "global " or func_type == "local " then
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
156 func_type = func_type.."function ";
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
157 end
4524
816c319a09d8 util.debug: Add a touch of colour to source locations
Matthew Wild <mwild1@gmail.com>
parents: 4523
diff changeset
158 line = "[Lua] "..getstring(styles.location, info.short_src.." line "..info.currentline).." in "..func_type..getstring(styles.funcname, name).." (defined on line "..info.linedefined..")";
4412
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
159 end
4521
4c7495f7f543 util.debug: Add markers in the output when crossing source file boundaries
Matthew Wild <mwild1@gmail.com>
parents: 4520
diff changeset
160 if source_desc ~= last_source_desc then -- Venturing into a new source, add marker for previous
4c7495f7f543 util.debug: Add markers in the output when crossing source file boundaries
Matthew Wild <mwild1@gmail.com>
parents: 4520
diff changeset
161 last_source_desc = source_desc;
4525
e00b4ec5fca4 util.debug: Move boundary markers to top of relevant sections of the stack trace (easier to follow)
Matthew Wild <mwild1@gmail.com>
parents: 4524
diff changeset
162 table.insert(lines, "\t "..build_source_boundary_marker(last_source_desc));
4521
4c7495f7f543 util.debug: Add markers in the output when crossing source file boundaries
Matthew Wild <mwild1@gmail.com>
parents: 4520
diff changeset
163 end
4412
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
164 nlevel = nlevel-1;
4523
d733bde93074 util.debug: Add a bit of colour
Matthew Wild <mwild1@gmail.com>
parents: 4522
diff changeset
165 table.insert(lines, "\t"..(nlevel==0 and ">" or " ")..getstring(styles.level_num, "("..nlevel..") ")..line);
4412
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
166 local npadding = (" "):rep(#tostring(nlevel));
4522
29f75c2af90e util.debug: Move optimal line length (default 65) into a variable
Matthew Wild <mwild1@gmail.com>
parents: 4521
diff changeset
167 local locals_str = string_from_var_table(level.locals, optimal_line_length, "\t "..npadding);
4412
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
168 if locals_str then
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
169 table.insert(lines, "\t "..npadding.."Locals: "..locals_str);
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
170 end
4522
29f75c2af90e util.debug: Move optimal line length (default 65) into a variable
Matthew Wild <mwild1@gmail.com>
parents: 4521
diff changeset
171 local upvalues_str = string_from_var_table(level.upvalues, optimal_line_length, "\t "..npadding);
4412
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
172 if upvalues_str then
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
173 table.insert(lines, "\t "..npadding.."Upvals: "..upvalues_str);
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
174 end
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
175 end
4521
4c7495f7f543 util.debug: Add markers in the output when crossing source file boundaries
Matthew Wild <mwild1@gmail.com>
parents: 4520
diff changeset
176
4525
e00b4ec5fca4 util.debug: Move boundary markers to top of relevant sections of the stack trace (easier to follow)
Matthew Wild <mwild1@gmail.com>
parents: 4524
diff changeset
177 -- table.insert(lines, "\t "..build_source_boundary_marker(last_source_desc));
4521
4c7495f7f543 util.debug: Add markers in the output when crossing source file boundaries
Matthew Wild <mwild1@gmail.com>
parents: 4520
diff changeset
178
4412
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
179 return message.."stack traceback:\n"..table.concat(lines, "\n");
5d7d9a60bc7f util.debug: Experimental new library for producing more extensive debug tracebacks
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
180 end