util.sql: Remove built-in engine caching. This is the wrong layer to do this, and...
[prosody.git] / util / debug.lua
index 9b1c4853b2e3eb56b75577a0a047018b62ac654c..91f691e144b388d933b23126cf412f0ff7d8b2a4 100644 (file)
@@ -9,19 +9,37 @@ local censored_names = {
 };
 local optimal_line_length = 65;
 
+local termcolours = require "util.termcolours";
+local getstring = termcolours.getstring;
+local styles;
+do
+       _ = termcolours.getstyle;
+       styles = {
+               boundary_padding = _("bright");
+               filename         = _("bright", "blue");
+               level_num        = _("green");
+               funcname         = _("yellow");
+               location         = _("yellow");
+       };
+end
+module("debugx", package.seeall);
 
-local function get_locals_table(level)
-       level = level + 1; -- Skip this function itself
+function get_locals_table(thread, level)
        local locals = {};
        for local_num = 1, math.huge do
-               local name, value = debug.getlocal(level, local_num);
+               local name, value;
+               if thread then
+                       name, value = debug.getlocal(thread, level, local_num);
+               else
+                       name, value = debug.getlocal(level+1, local_num);
+               end
                if not name then break; end
                table.insert(locals, { name = name, value = value });
        end
        return locals;
 end
 
-local function get_upvalues_table(func)
+function get_upvalues_table(func)
        local upvalues = {};
        if func then
                for upvalue_num = 1, math.huge do
@@ -33,7 +51,7 @@ local function get_upvalues_table(func)
        return upvalues;
 end
 
-local function string_from_var_table(var_table, max_line_len, indent_str)
+function string_from_var_table(var_table, max_line_len, indent_str)
        local var_string = {};
        local col_pos = 0;
        max_line_len = max_line_len or math.huge;
@@ -76,48 +94,59 @@ function get_traceback_table(thread, start_level)
                if thread then
                        info = debug.getinfo(thread, level);
                else
-                       info = debug.getinfo(level);
+                       info = debug.getinfo(level+1);
                end
                if not info then break; end
-               
+
                levels[(level-start_level)+1] = {
                        level = level;
                        info = info;
-                       locals = get_locals_table(level);
+                       locals = get_locals_table(thread, level+(thread and 0 or 1));
                        upvalues = get_upvalues_table(info.func);
                };
-       end     
+       end
        return levels;
 end
 
-function debug.traceback(...)
-       local ok, ret = pcall(debug._traceback, ...);
+function traceback(...)
+       local ok, ret = pcall(_traceback, ...);
        if not ok then
                return "Error in error handling: "..ret;
        end
        return ret;
 end
 
-function debug._traceback(thread, message, level)
-       if type(thread) ~= "thread" then
+local function build_source_boundary_marker(last_source_desc)
+       local padding = string.rep("-", math.floor(((optimal_line_length - 6) - #last_source_desc)/2));
+       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 "));
+end
+
+function _traceback(thread, message, level)
+
+       -- Lua manual says: debug.traceback ([thread,] [message [, level]])
+       -- I fathom this to mean one of:
+       -- ()
+       -- (thread)
+       -- (message, level)
+       -- (thread, message, level)
+
+       if thread == nil then -- Defaults
+               thread, message, level = coroutine.running(), message, level;
+       elseif type(thread) == "string" then
                thread, message, level = coroutine.running(), thread, message;
+       elseif type(thread) ~= "thread" then
+               return nil; -- debug.traceback() does this
        end
-       if level and type(message) ~= "string" then
-               return nil, "invalid message";
-       elseif not level then
-               if type(message) == "number" then
-                       level, message = message, nil;
-               else
-                       level = 2;
-               end
-       end
-       
+
+       level = level or 0;
+
        message = message and (message.."\n") or "";
-       
-       local levels = get_traceback_table(thread, level+2);
-       
+
+       -- +3 counts for this function, and the pcall() and wrapper above us, the +1... I don't know.
+       local levels = get_traceback_table(thread, level+(thread == nil and 4 or 0));
+
        local last_source_desc;
-       
+
        local lines = {};
        for nlevel, level in ipairs(levels) do
                local info = level.info;
@@ -126,9 +155,9 @@ function debug._traceback(thread, message, level)
                local source_desc = (info.short_src == "[C]" and "C code") or info.short_src or "Unknown";
                if func_type == " " then func_type = ""; end;
                if info.short_src == "[C]" then
-                       line = "[ C ] "..func_type.."C function "..(info.name and ("%q"):format(info.name) or "(unknown name)")
+                       line = "[ C ] "..func_type.."C function "..getstring(styles.location, (info.name and ("%q"):format(info.name) or "(unknown name)"));
                elseif info.what == "main" then
-                       line = "[Lua] "..info.short_src.." line "..info.currentline;
+                       line = "[Lua] "..getstring(styles.location, info.short_src.." line "..info.currentline);
                else
                        local name = info.name or " ";
                        if name ~= " " then
@@ -137,21 +166,20 @@ function debug._traceback(thread, message, level)
                        if func_type == "global " or func_type == "local " then
                                func_type = func_type.."function ";
                        end
-                       line = "[Lua] "..info.short_src.." line "..info.currentline.." in "..func_type..name.." defined on line "..info.linedefined;
+                       line = "[Lua] "..getstring(styles.location, info.short_src.." line "..info.currentline).." in "..func_type..getstring(styles.funcname, name).." (defined on line "..info.linedefined..")";
                end
                if source_desc ~= last_source_desc then -- Venturing into a new source, add marker for previous
-                       if last_source_desc then
-                               local padding = string.rep("-", math.floor(((optimal_line_length - 6) - #last_source_desc)/2));
-                               table.insert(lines, "\t ^"..padding.." "..last_source_desc.." "..padding..(#last_source_desc%2==0 and "-^" or "^ "));
-                       end
                        last_source_desc = source_desc;
+                       table.insert(lines, "\t "..build_source_boundary_marker(last_source_desc));
                end
                nlevel = nlevel-1;
-               table.insert(lines, "\t"..(nlevel==0 and ">" or " ").."("..nlevel..") "..line);
+               table.insert(lines, "\t"..(nlevel==0 and ">" or " ")..getstring(styles.level_num, "("..nlevel..") ")..line);
                local npadding = (" "):rep(#tostring(nlevel));
-               local locals_str = string_from_var_table(level.locals, optimal_line_length, "\t            "..npadding);
-               if locals_str then
-                       table.insert(lines, "\t    "..npadding.."Locals: "..locals_str);
+               if level.locals then
+                       local locals_str = string_from_var_table(level.locals, optimal_line_length, "\t            "..npadding);
+                       if locals_str then
+                               table.insert(lines, "\t    "..npadding.."Locals: "..locals_str);
+                       end
                end
                local upvalues_str = string_from_var_table(level.upvalues, optimal_line_length, "\t            "..npadding);
                if upvalues_str then
@@ -159,8 +187,13 @@ function debug._traceback(thread, message, level)
                end
        end
 
-       local padding = string.rep("-", math.floor(((optimal_line_length - 6) - #last_source_desc) / 2));
-       table.insert(lines, "\t ^"..padding.." "..last_source_desc.." "..padding..(#last_source_desc%2==0 and "-^" or "^ "));
+--     table.insert(lines, "\t "..build_source_boundary_marker(last_source_desc));
 
        return message.."stack traceback:\n"..table.concat(lines, "\n");
 end
+
+function use()
+       debug.traceback = traceback;
+end
+
+return _M;