1 -- Variables ending with these names will not
2 -- have their values printed ('password' includes
3 -- 'new_password', etc.)
5 -- luacheck: ignore 122/debug
7 local censored_names = {
13 local optimal_line_length = 65;
15 local termcolours = require "util.termcolours";
16 local getstring = termcolours.getstring;
19 local _ = termcolours.getstyle;
21 boundary_padding = _("bright");
22 filename = _("bright", "blue");
23 level_num = _("green");
24 funcname = _("yellow");
25 location = _("yellow");
29 local function get_locals_table(thread, level)
31 for local_num = 1, math.huge do
34 name, value = debug.getlocal(thread, level, local_num);
36 name, value = debug.getlocal(level+1, local_num);
38 if not name then break; end
39 table.insert(locals, { name = name, value = value });
44 local function get_upvalues_table(func)
47 for upvalue_num = 1, math.huge do
48 local name, value = debug.getupvalue(func, upvalue_num);
49 if not name then break; end
50 table.insert(upvalues, { name = name, value = value });
56 local function string_from_var_table(var_table, max_line_len, indent_str)
57 local var_string = {};
59 max_line_len = max_line_len or math.huge;
60 indent_str = "\n"..(indent_str or "");
61 for _, var in ipairs(var_table) do
62 local name, value = var.name, var.value;
63 if name:sub(1,1) ~= "(" then
64 if type(value) == "string" then
65 if censored_names[name:match("%a+$")] then
68 value = ("%q"):format(value);
71 value = tostring(value);
73 if #value > max_line_len then
74 value = value:sub(1, max_line_len-3).."…";
76 local str = ("%s = %s"):format(name, tostring(value));
77 col_pos = col_pos + #str;
78 if col_pos > max_line_len then
79 table.insert(var_string, indent_str);
82 table.insert(var_string, str);
85 if #var_string == 0 then
88 return "{ "..table.concat(var_string, ", "):gsub(indent_str..", ", indent_str).." }";
92 local function get_traceback_table(thread, start_level)
94 for level = start_level, math.huge do
97 info = debug.getinfo(thread, level);
99 info = debug.getinfo(level+1);
101 if not info then break; end
103 levels[(level-start_level)+1] = {
106 locals = get_locals_table(thread, level+(thread and 0 or 1));
107 upvalues = get_upvalues_table(info.func);
113 local function build_source_boundary_marker(last_source_desc)
114 local padding = string.rep("-", math.floor(((optimal_line_length - 6) - #last_source_desc)/2));
115 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 "));
118 local function _traceback(thread, message, level)
120 -- Lua manual says: debug.traceback ([thread,] [message [, level]])
121 -- I fathom this to mean one of:
125 -- (thread, message, level)
127 if thread == nil then -- Defaults
128 thread, message, level = coroutine.running(), message, level;
129 elseif type(thread) == "string" then
130 thread, message, level = coroutine.running(), thread, message;
131 elseif type(thread) ~= "thread" then
132 return nil; -- debug.traceback() does this
137 message = message and (message.."\n") or "";
139 -- +3 counts for this function, and the pcall() and wrapper above us, the +1... I don't know.
140 local levels = get_traceback_table(thread, level+(thread == nil and 4 or 0));
142 local last_source_desc;
145 for nlevel, level in ipairs(levels) do
146 local info = level.info;
148 local func_type = info.namewhat.." ";
149 local source_desc = (info.short_src == "[C]" and "C code") or info.short_src or "Unknown";
150 if func_type == " " then func_type = ""; end;
151 if info.short_src == "[C]" then
152 line = "[ C ] "..func_type.."C function "..getstring(styles.location, (info.name and ("%q"):format(info.name) or "(unknown name)"));
153 elseif info.what == "main" then
154 line = "[Lua] "..getstring(styles.location, info.short_src.." line "..info.currentline);
156 local name = info.name or " ";
158 name = ("%q"):format(name);
160 if func_type == "global " or func_type == "local " then
161 func_type = func_type.."function ";
163 line = "[Lua] "..getstring(styles.location, info.short_src.." line "..info.currentline).." in "..func_type..getstring(styles.funcname, name).." (defined on line "..info.linedefined..")";
165 if source_desc ~= last_source_desc then -- Venturing into a new source, add marker for previous
166 last_source_desc = source_desc;
167 table.insert(lines, "\t "..build_source_boundary_marker(last_source_desc));
170 table.insert(lines, "\t"..(nlevel==0 and ">" or " ")..getstring(styles.level_num, "("..nlevel..") ")..line);
171 local npadding = (" "):rep(#tostring(nlevel));
173 local locals_str = string_from_var_table(level.locals, optimal_line_length, "\t "..npadding);
175 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");
189 local function traceback(...)
190 local ok, ret = pcall(_traceback, ...);
192 return "Error in error handling: "..ret;
198 debug.traceback = traceback;
202 get_locals_table = get_locals_table;
203 get_upvalues_table = get_upvalues_table;
204 string_from_var_table = string_from_var_table;
205 get_traceback_table = get_traceback_table;
206 traceback = traceback;