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 local _ = termcolours.getstyle;
18 boundary_padding = _("bright");
19 filename = _("bright", "blue");
20 level_num = _("green");
21 funcname = _("yellow");
22 location = _("yellow");
26 local function get_locals_table(thread, level)
28 for local_num = 1, math.huge do
31 name, value = debug.getlocal(thread, level, local_num);
33 name, value = debug.getlocal(level+1, local_num);
35 if not name then break; end
36 table.insert(locals, { name = name, value = value });
41 local function get_upvalues_table(func)
44 for upvalue_num = 1, math.huge do
45 local name, value = debug.getupvalue(func, upvalue_num);
46 if not name then break; end
47 table.insert(upvalues, { name = name, value = value });
53 local function string_from_var_table(var_table, max_line_len, indent_str)
54 local var_string = {};
56 max_line_len = max_line_len or math.huge;
57 indent_str = "\n"..(indent_str or "");
58 for _, var in ipairs(var_table) do
59 local name, value = var.name, var.value;
60 if name:sub(1,1) ~= "(" then
61 if type(value) == "string" then
62 if censored_names[name:match("%a+$")] then
65 value = ("%q"):format(value);
68 value = tostring(value);
70 if #value > max_line_len then
71 value = value:sub(1, max_line_len-3).."…";
73 local str = ("%s = %s"):format(name, tostring(value));
74 col_pos = col_pos + #str;
75 if col_pos > max_line_len then
76 table.insert(var_string, indent_str);
79 table.insert(var_string, str);
82 if #var_string == 0 then
85 return "{ "..table.concat(var_string, ", "):gsub(indent_str..", ", indent_str).." }";
89 local function get_traceback_table(thread, start_level)
91 for level = start_level, math.huge do
94 info = debug.getinfo(thread, level);
96 info = debug.getinfo(level+1);
98 if not info then break; end
100 levels[(level-start_level)+1] = {
103 locals = get_locals_table(thread, level+(thread and 0 or 1));
104 upvalues = get_upvalues_table(info.func);
110 local function build_source_boundary_marker(last_source_desc)
111 local padding = string.rep("-", math.floor(((optimal_line_length - 6) - #last_source_desc)/2));
112 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 "));
115 local function _traceback(thread, message, level)
117 -- Lua manual says: debug.traceback ([thread,] [message [, level]])
118 -- I fathom this to mean one of:
122 -- (thread, message, level)
124 if thread == nil then -- Defaults
125 thread, message, level = coroutine.running(), message, level;
126 elseif type(thread) == "string" then
127 thread, message, level = coroutine.running(), thread, message;
128 elseif type(thread) ~= "thread" then
129 return nil; -- debug.traceback() does this
134 message = message and (message.."\n") or "";
136 -- +3 counts for this function, and the pcall() and wrapper above us, the +1... I don't know.
137 local levels = get_traceback_table(thread, level+(thread == nil and 4 or 0));
139 local last_source_desc;
142 for nlevel, level in ipairs(levels) do
143 local info = level.info;
145 local func_type = info.namewhat.." ";
146 local source_desc = (info.short_src == "[C]" and "C code") or info.short_src or "Unknown";
147 if func_type == " " then func_type = ""; end;
148 if info.short_src == "[C]" then
149 line = "[ C ] "..func_type.."C function "..getstring(styles.location, (info.name and ("%q"):format(info.name) or "(unknown name)"));
150 elseif info.what == "main" then
151 line = "[Lua] "..getstring(styles.location, info.short_src.." line "..info.currentline);
153 local name = info.name or " ";
155 name = ("%q"):format(name);
157 if func_type == "global " or func_type == "local " then
158 func_type = func_type.."function ";
160 line = "[Lua] "..getstring(styles.location, info.short_src.." line "..info.currentline).." in "..func_type..getstring(styles.funcname, name).." (defined on line "..info.linedefined..")";
162 if source_desc ~= last_source_desc then -- Venturing into a new source, add marker for previous
163 last_source_desc = source_desc;
164 table.insert(lines, "\t "..build_source_boundary_marker(last_source_desc));
167 table.insert(lines, "\t"..(nlevel==0 and ">" or " ")..getstring(styles.level_num, "("..nlevel..") ")..line);
168 local npadding = (" "):rep(#tostring(nlevel));
170 local locals_str = string_from_var_table(level.locals, optimal_line_length, "\t "..npadding);
172 table.insert(lines, "\t "..npadding.."Locals: "..locals_str);
175 local upvalues_str = string_from_var_table(level.upvalues, optimal_line_length, "\t "..npadding);
177 table.insert(lines, "\t "..npadding.."Upvals: "..upvalues_str);
181 -- table.insert(lines, "\t "..build_source_boundary_marker(last_source_desc));
183 return message.."stack traceback:\n"..table.concat(lines, "\n");
186 local function traceback(...)
187 local ok, ret = pcall(_traceback, ...);
189 return "Error in error handling: "..ret;
195 debug.traceback = traceback;
199 get_locals_table = get_locals_table;
200 get_upvalues_table = get_upvalues_table;
201 string_from_var_table = string_from_var_table;
202 get_traceback_table = get_traceback_table;
203 traceback = traceback;