Comparison

util/debug.lua @ 4523:d733bde93074

util.debug: Add a bit of colour
author Matthew Wild <mwild1@gmail.com>
date Thu, 22 Mar 2012 16:07:57 +0000
parent 4522:29f75c2af90e
child 4524:816c319a09d8
comparison
equal deleted inserted replaced
4522:29f75c2af90e 4523:d733bde93074
7 pass = true; 7 pass = true;
8 pwd = true; 8 pwd = true;
9 }; 9 };
10 local optimal_line_length = 65; 10 local optimal_line_length = 65;
11 11
12 local termcolours = require "util.termcolours";
13 local getstring = termcolours.getstring;
14 local styles;
15 do
16 _ = termcolours.getstyle;
17 styles = {
18 boundary_padding = _("bright", "white");
19 filename = _("bright", "blue");
20 level_num = _("green");
21 funcname = _("yellow");
22 };
23 end
12 24
13 local function get_locals_table(level) 25 local function get_locals_table(level)
14 level = level + 1; -- Skip this function itself 26 level = level + 1; -- Skip this function itself
15 local locals = {}; 27 local locals = {};
16 for local_num = 1, math.huge do 28 for local_num = 1, math.huge do
96 return "Error in error handling: "..ret; 108 return "Error in error handling: "..ret;
97 end 109 end
98 return ret; 110 return ret;
99 end 111 end
100 112
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, "^"..padding).." "..getstring(styles.filename, last_source_desc).." "..getstring(styles.boundary_padding, padding..(#last_source_desc%2==0 and "-^" or "^ "));
116 end
117
101 function debug._traceback(thread, message, level) 118 function debug._traceback(thread, message, level)
102 if type(thread) ~= "thread" then 119 if type(thread) ~= "thread" then
103 thread, message, level = coroutine.running(), thread, message; 120 thread, message, level = coroutine.running(), thread, message;
104 end 121 end
105 if level and type(message) ~= "string" then 122 if level and type(message) ~= "string" then
135 name = ("%q"):format(name); 152 name = ("%q"):format(name);
136 end 153 end
137 if func_type == "global " or func_type == "local " then 154 if func_type == "global " or func_type == "local " then
138 func_type = func_type.."function "; 155 func_type = func_type.."function ";
139 end 156 end
140 line = "[Lua] "..info.short_src.." line "..info.currentline.." in "..func_type..name.." defined on line "..info.linedefined; 157 line = "[Lua] "..info.short_src.." line "..info.currentline.." in "..func_type..getstring(styles.funcname, name).." defined on line "..info.linedefined;
141 end 158 end
142 if source_desc ~= last_source_desc then -- Venturing into a new source, add marker for previous 159 if source_desc ~= last_source_desc then -- Venturing into a new source, add marker for previous
143 if last_source_desc then 160 if last_source_desc then
144 local padding = string.rep("-", math.floor(((optimal_line_length - 6) - #last_source_desc)/2)); 161 table.insert(lines, "\t "..build_source_boundary_marker(last_source_desc));
145 table.insert(lines, "\t ^"..padding.." "..last_source_desc.." "..padding..(#last_source_desc%2==0 and "-^" or "^ "));
146 end 162 end
147 last_source_desc = source_desc; 163 last_source_desc = source_desc;
148 end 164 end
149 nlevel = nlevel-1; 165 nlevel = nlevel-1;
150 table.insert(lines, "\t"..(nlevel==0 and ">" or " ").."("..nlevel..") "..line); 166 table.insert(lines, "\t"..(nlevel==0 and ">" or " ")..getstring(styles.level_num, "("..nlevel..") ")..line);
151 local npadding = (" "):rep(#tostring(nlevel)); 167 local npadding = (" "):rep(#tostring(nlevel));
152 local locals_str = string_from_var_table(level.locals, optimal_line_length, "\t "..npadding); 168 local locals_str = string_from_var_table(level.locals, optimal_line_length, "\t "..npadding);
153 if locals_str then 169 if locals_str then
154 table.insert(lines, "\t "..npadding.."Locals: "..locals_str); 170 table.insert(lines, "\t "..npadding.."Locals: "..locals_str);
155 end 171 end
157 if upvalues_str then 173 if upvalues_str then
158 table.insert(lines, "\t "..npadding.."Upvals: "..upvalues_str); 174 table.insert(lines, "\t "..npadding.."Upvals: "..upvalues_str);
159 end 175 end
160 end 176 end
161 177
162 local padding = string.rep("-", math.floor(((optimal_line_length - 6) - #last_source_desc) / 2)); 178 table.insert(lines, "\t "..build_source_boundary_marker(last_source_desc));
163 table.insert(lines, "\t ^"..padding.." "..last_source_desc.." "..padding..(#last_source_desc%2==0 and "-^" or "^ "));
164 179
165 return message.."stack traceback:\n"..table.concat(lines, "\n"); 180 return message.."stack traceback:\n"..table.concat(lines, "\n");
166 end 181 end