1 -- Variables ending with these names will not
2 -- have their values printed ('password' includes
3 -- 'new_password', etc.)
4 local censored_names = {
10 local optimal_line_length = 65;
12 local termcolours = require "util.termcolours";
13 local getstring = termcolours.getstring;
16 _ = termcolours.getstyle;
18 boundary_padding = _("bright");
19 filename = _("bright", "blue");
20 level_num = _("green");
21 funcname = _("yellow");
22 location = _("yellow");
25 module("debugx", package.seeall);
27 function get_locals_table(level)
28 level = level + 1; -- Skip this function itself
30 for local_num = 1, math.huge do
31 local name, value = debug.getlocal(level, local_num);
32 if not name then break; end
33 table.insert(locals, { name = name, value = value });
38 function get_upvalues_table(func)
41 for upvalue_num = 1, math.huge do
42 local name, value = debug.getupvalue(func, upvalue_num);
43 if not name then break; end
44 table.insert(upvalues, { name = name, value = value });
50 function string_from_var_table(var_table, max_line_len, indent_str)
51 local var_string = {};
53 max_line_len = max_line_len or math.huge;
54 indent_str = "\n"..(indent_str or "");
55 for _, var in ipairs(var_table) do
56 local name, value = var.name, var.value;
57 if name:sub(1,1) ~= "(" then
58 if type(value) == "string" then
59 if censored_names[name:match("%a+$")] then
62 value = ("%q"):format(value);
65 value = tostring(value);
67 if #value > max_line_len then
68 value = value:sub(1, max_line_len-3).."…";
70 local str = ("%s = %s"):format(name, tostring(value));
71 col_pos = col_pos + #str;
72 if col_pos > max_line_len then
73 table.insert(var_string, indent_str);
76 table.insert(var_string, str);
79 if #var_string == 0 then
82 return "{ "..table.concat(var_string, ", "):gsub(indent_str..", ", indent_str).." }";
86 function get_traceback_table(thread, start_level)
88 for level = start_level, math.huge do
91 info = debug.getinfo(thread, level+1);
93 info = debug.getinfo(level+1);
95 if not info then break; end
97 levels[(level-start_level)+1] = {
100 locals = get_locals_table(level+1);
101 upvalues = get_upvalues_table(info.func);
107 function traceback(...)
108 local ok, ret = pcall(_traceback, ...);
110 return "Error in error handling: "..ret;
115 local function build_source_boundary_marker(last_source_desc)
116 local padding = string.rep("-", math.floor(((optimal_line_length - 6) - #last_source_desc)/2));
117 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 "));
120 function _traceback(thread, message, level)
122 -- Lua manual says: debug.traceback ([thread,] [message [, level]])
123 -- I fathom this to mean one of:
127 -- (thread, message, level)
129 if thread == nil then -- Defaults
130 thread, message, level = coroutine.running(), message, level;
131 elseif type(thread) == "string" then
132 thread, message, level = coroutine.running(), thread, message;
133 elseif type(thread) ~= "thread" then
134 return nil; -- debug.traceback() does this
139 message = message and (message.."\n") or "";
141 -- +3 counts for this function, and the pcall() and wrapper above us
142 local levels = get_traceback_table(thread, level+3);
144 local last_source_desc;
147 for nlevel, level in ipairs(levels) do
148 local info = level.info;
150 local func_type = info.namewhat.." ";
151 local source_desc = (info.short_src == "[C]" and "C code") or info.short_src or "Unknown";
152 if func_type == " " then func_type = ""; end;
153 if info.short_src == "[C]" then
154 line = "[ C ] "..func_type.."C function "..getstring(styles.location, (info.name and ("%q"):format(info.name) or "(unknown name)"));
155 elseif info.what == "main" then
156 line = "[Lua] "..getstring(styles.location, info.short_src.." line "..info.currentline);
158 local name = info.name or " ";
160 name = ("%q"):format(name);
162 if func_type == "global " or func_type == "local " then
163 func_type = func_type.."function ";
165 line = "[Lua] "..getstring(styles.location, info.short_src.." line "..info.currentline).." in "..func_type..getstring(styles.funcname, name).." (defined on line "..info.linedefined..")";
167 if source_desc ~= last_source_desc then -- Venturing into a new source, add marker for previous
168 last_source_desc = source_desc;
169 table.insert(lines, "\t "..build_source_boundary_marker(last_source_desc));
172 table.insert(lines, "\t"..(nlevel==0 and ">" or " ")..getstring(styles.level_num, "("..nlevel..") ")..line);
173 local npadding = (" "):rep(#tostring(nlevel));
174 local locals_str = string_from_var_table(level.locals, optimal_line_length, "\t "..npadding);
176 table.insert(lines, "\t "..npadding.."Locals: "..locals_str);
178 local upvalues_str = string_from_var_table(level.upvalues, optimal_line_length, "\t "..npadding);
180 table.insert(lines, "\t "..npadding.."Upvals: "..upvalues_str);
184 -- table.insert(lines, "\t "..build_source_boundary_marker(last_source_desc));
186 return message.."stack traceback:\n"..table.concat(lines, "\n");
190 debug.traceback = traceback;