Comparison

util/debug.lua @ 4684:dc70c4ffb66d

Merge timber->trunk - thanks everyone!
author Matthew Wild <mwild1@gmail.com>
date Tue, 24 Apr 2012 21:59:20 +0100
parent 4525:e00b4ec5fca4
parent 4680:8834f220ab91
child 4693:7ef4faa056fb
comparison
equal deleted inserted replaced
4529:12621337471f 4684:dc70c4ffb66d
20 level_num = _("green"); 20 level_num = _("green");
21 funcname = _("yellow"); 21 funcname = _("yellow");
22 location = _("yellow"); 22 location = _("yellow");
23 }; 23 };
24 end 24 end
25 module("debugx", package.seeall);
25 26
26 local function get_locals_table(level) 27 function get_locals_table(level)
27 level = level + 1; -- Skip this function itself 28 level = level + 1; -- Skip this function itself
28 local locals = {}; 29 local locals = {};
29 for local_num = 1, math.huge do 30 for local_num = 1, math.huge do
30 local name, value = debug.getlocal(level, local_num); 31 local name, value = debug.getlocal(level, local_num);
31 if not name then break; end 32 if not name then break; end
32 table.insert(locals, { name = name, value = value }); 33 table.insert(locals, { name = name, value = value });
33 end 34 end
34 return locals; 35 return locals;
35 end 36 end
36 37
37 local function get_upvalues_table(func) 38 function get_upvalues_table(func)
38 local upvalues = {}; 39 local upvalues = {};
39 if func then 40 if func then
40 for upvalue_num = 1, math.huge do 41 for upvalue_num = 1, math.huge do
41 local name, value = debug.getupvalue(func, upvalue_num); 42 local name, value = debug.getupvalue(func, upvalue_num);
42 if not name then break; end 43 if not name then break; end
44 end 45 end
45 end 46 end
46 return upvalues; 47 return upvalues;
47 end 48 end
48 49
49 local function string_from_var_table(var_table, max_line_len, indent_str) 50 function string_from_var_table(var_table, max_line_len, indent_str)
50 local var_string = {}; 51 local var_string = {};
51 local col_pos = 0; 52 local col_pos = 0;
52 max_line_len = max_line_len or math.huge; 53 max_line_len = max_line_len or math.huge;
53 indent_str = "\n"..(indent_str or ""); 54 indent_str = "\n"..(indent_str or "");
54 for _, var in ipairs(var_table) do 55 for _, var in ipairs(var_table) do
101 }; 102 };
102 end 103 end
103 return levels; 104 return levels;
104 end 105 end
105 106
106 function debug.traceback(...) 107 function traceback(...)
107 local ok, ret = pcall(debug._traceback, ...); 108 local ok, ret = pcall(_traceback, ...);
108 if not ok then 109 if not ok then
109 return "Error in error handling: "..ret; 110 return "Error in error handling: "..ret;
110 end 111 end
111 return ret; 112 return ret;
112 end 113 end
114 local function build_source_boundary_marker(last_source_desc) 115 local function build_source_boundary_marker(last_source_desc)
115 local padding = string.rep("-", math.floor(((optimal_line_length - 6) - #last_source_desc)/2)); 116 local padding = string.rep("-", math.floor(((optimal_line_length - 6) - #last_source_desc)/2));
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 ")); 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 "));
117 end 118 end
118 119
119 function debug._traceback(thread, message, level) 120 function _traceback(thread, message, level)
121
120 if type(thread) ~= "thread" then 122 if type(thread) ~= "thread" then
121 thread, message, level = coroutine.running(), thread, message; 123 thread, message, level = coroutine.running(), thread, message;
122 end 124 end
123 if level and type(message) ~= "string" then 125 if level and type(message) ~= "string" then
124 return nil, "invalid message"; 126 return nil, "invalid message";
176 178
177 -- table.insert(lines, "\t "..build_source_boundary_marker(last_source_desc)); 179 -- table.insert(lines, "\t "..build_source_boundary_marker(last_source_desc));
178 180
179 return message.."stack traceback:\n"..table.concat(lines, "\n"); 181 return message.."stack traceback:\n"..table.concat(lines, "\n");
180 end 182 end
183
184 function use()
185 debug.traceback = traceback;
186 end
187
188 return _M;