Software / code / prosody
Comparison
util/debug.lua @ 4680:8834f220ab91
util.debug: Turn into a real-ish module ('debugx'), and require you call use() to override debug.traceback()
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Tue, 24 Apr 2012 18:53:50 +0100 |
| parent | 4465:41c4252526bd |
| child | 4684:dc70c4ffb66d |
comparison
equal
deleted
inserted
replaced
| 4679:5b52b5eaa03d | 4680:8834f220ab91 |
|---|---|
| 6 passwd = true; | 6 passwd = true; |
| 7 pass = true; | 7 pass = true; |
| 8 pwd = true; | 8 pwd = true; |
| 9 }; | 9 }; |
| 10 | 10 |
| 11 local function get_locals_table(level) | 11 module("debugx", package.seeall); |
| 12 | |
| 13 function get_locals_table(level) | |
| 12 level = level + 1; -- Skip this function itself | 14 level = level + 1; -- Skip this function itself |
| 13 local locals = {}; | 15 local locals = {}; |
| 14 for local_num = 1, math.huge do | 16 for local_num = 1, math.huge do |
| 15 local name, value = debug.getlocal(level, local_num); | 17 local name, value = debug.getlocal(level, local_num); |
| 16 if not name then break; end | 18 if not name then break; end |
| 17 table.insert(locals, { name = name, value = value }); | 19 table.insert(locals, { name = name, value = value }); |
| 18 end | 20 end |
| 19 return locals; | 21 return locals; |
| 20 end | 22 end |
| 21 | 23 |
| 22 local function get_upvalues_table(func) | 24 function get_upvalues_table(func) |
| 23 local upvalues = {}; | 25 local upvalues = {}; |
| 24 if func then | 26 if func then |
| 25 for upvalue_num = 1, math.huge do | 27 for upvalue_num = 1, math.huge do |
| 26 local name, value = debug.getupvalue(func, upvalue_num); | 28 local name, value = debug.getupvalue(func, upvalue_num); |
| 27 if not name then break; end | 29 if not name then break; end |
| 29 end | 31 end |
| 30 end | 32 end |
| 31 return upvalues; | 33 return upvalues; |
| 32 end | 34 end |
| 33 | 35 |
| 34 local function string_from_var_table(var_table, max_line_len, indent_str) | 36 function string_from_var_table(var_table, max_line_len, indent_str) |
| 35 local var_string = {}; | 37 local var_string = {}; |
| 36 local col_pos = 0; | 38 local col_pos = 0; |
| 37 max_line_len = max_line_len or math.huge; | 39 max_line_len = max_line_len or math.huge; |
| 38 indent_str = "\n"..(indent_str or ""); | 40 indent_str = "\n"..(indent_str or ""); |
| 39 for _, var in ipairs(var_table) do | 41 for _, var in ipairs(var_table) do |
| 86 }; | 88 }; |
| 87 end | 89 end |
| 88 return levels; | 90 return levels; |
| 89 end | 91 end |
| 90 | 92 |
| 91 function debug.traceback(thread, message, level) | 93 function traceback(thread, message, level) |
| 92 if type(thread) ~= "thread" then | 94 if type(thread) ~= "thread" then |
| 93 thread, message, level = coroutine.running(), thread, message; | 95 thread, message, level = coroutine.running(), thread, message; |
| 94 end | 96 end |
| 95 if level and type(message) ~= "string" then | 97 if level and type(message) ~= "string" then |
| 96 return nil, "invalid message"; | 98 return nil, "invalid message"; |
| 134 table.insert(lines, "\t "..npadding.."Upvals: "..upvalues_str); | 136 table.insert(lines, "\t "..npadding.."Upvals: "..upvalues_str); |
| 135 end | 137 end |
| 136 end | 138 end |
| 137 return message.."stack traceback:\n"..table.concat(lines, "\n"); | 139 return message.."stack traceback:\n"..table.concat(lines, "\n"); |
| 138 end | 140 end |
| 141 | |
| 142 function use() | |
| 143 debug.traceback = traceback; | |
| 144 end | |
| 145 | |
| 146 return _M; |