mod_s2s: Log certificate identity validation result
[prosody.git] / util / debug.lua
index d63dce1b7a32f24ab301f56686ad4a00e83479d3..bff0e347a22fa7b58676e280484c9e017bc8f24b 100644 (file)
@@ -15,15 +15,16 @@ local styles;
 do
        _ = termcolours.getstyle;
        styles = {
-               boundary_padding = _("bright", "white");
+               boundary_padding = _("bright");
                filename         = _("bright", "blue");
                level_num        = _("green");
                funcname         = _("yellow");
                location         = _("yellow");
        };
 end
+module("debugx", package.seeall);
 
-local function get_locals_table(level)
+function get_locals_table(level)
        level = level + 1; -- Skip this function itself
        local locals = {};
        for local_num = 1, math.huge do
@@ -34,7 +35,7 @@ local function get_locals_table(level)
        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
@@ -46,7 +47,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;
@@ -87,24 +88,24 @@ function get_traceback_table(thread, start_level)
        for level = start_level, math.huge do
                local info;
                if thread then
-                       info = debug.getinfo(thread, level);
+                       info = debug.getinfo(thread, level+1);
                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(level+1);
                        upvalues = get_upvalues_table(info.func);
                };
        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
@@ -113,26 +114,32 @@ end
 
 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, "^"..padding).." "..getstring(styles.filename, last_source_desc).." "..getstring(styles.boundary_padding, padding..(#last_source_desc%2==0 and "-^" or "^ "));
+       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 debug._traceback(thread, message, level)
-       if type(thread) ~= "thread" then
+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 1;
+
        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
+       local levels = get_traceback_table(thread, level+3);
        
        local last_source_desc;
        
@@ -158,10 +165,8 @@ function debug._traceback(thread, message, level)
                        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
-                               table.insert(lines, "\t "..build_source_boundary_marker(last_source_desc));
-                       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 " ")..getstring(styles.level_num, "("..nlevel..") ")..line);
@@ -176,7 +181,13 @@ function debug._traceback(thread, message, level)
                end
        end
 
-       table.insert(lines, "\t "..build_source_boundary_marker(last_source_desc));
+--     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;