diff options
author | Matthew Wild <mwild1@gmail.com> | 2012-04-24 21:59:20 +0100 |
---|---|---|
committer | Matthew Wild <mwild1@gmail.com> | 2012-04-24 21:59:20 +0100 |
commit | 15175bb828ab14cf0f35b221227beeeb46e4dd8f (patch) | |
tree | fe8c013599dd847f7365cb1adab80ab8fe846de5 /util | |
parent | a5efef5aab5624e3a1cb4f1966e719738b859eda (diff) | |
parent | 7cb22f77a78741a1bd085c6b3dc3b31f51c1fa09 (diff) | |
download | prosody-15175bb828ab14cf0f35b221227beeeb46e4dd8f.tar.gz prosody-15175bb828ab14cf0f35b221227beeeb46e4dd8f.zip |
Merge timber->trunk - thanks everyone!
Diffstat (limited to 'util')
-rw-r--r-- | util/debug.lua | 20 | ||||
-rw-r--r-- | util/helpers.lua | 31 | ||||
-rw-r--r-- | util/iterators.lua | 25 | ||||
-rw-r--r-- | util/logger.lua | 31 | ||||
-rw-r--r-- | util/set.lua | 6 |
5 files changed, 63 insertions, 50 deletions
diff --git a/util/debug.lua b/util/debug.lua index 3736dd34..7caf21ce 100644 --- a/util/debug.lua +++ b/util/debug.lua @@ -22,8 +22,9 @@ do 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; @@ -103,8 +104,8 @@ function get_traceback_table(thread, start_level) 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 @@ -116,7 +117,8 @@ local function build_source_boundary_marker(last_source_desc) 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) +function _traceback(thread, message, level) + if type(thread) ~= "thread" then thread, message, level = coroutine.running(), thread, message; end @@ -178,3 +180,9 @@ function debug._traceback(thread, message, level) return message.."stack traceback:\n"..table.concat(lines, "\n"); end + +function use() + debug.traceback = traceback; +end + +return _M; diff --git a/util/helpers.lua b/util/helpers.lua index 11356176..ad23dd79 100644 --- a/util/helpers.lua +++ b/util/helpers.lua @@ -6,6 +6,8 @@ -- COPYING file in the source package for more information. -- +local debug = require "util.debug"; + module("helpers", package.seeall); -- Helper functions for debugging @@ -28,7 +30,34 @@ function log_events(events, name, logger) end function revert_log_events(events) - events.fire_event, events[events.fire_event] = events[events.fire_event], nil; -- :) + events.fire_event, events[events.fire_event] = events[events.fire_event], nil; -- :)) +end + +function show_events(events) + local event_handlers = events._handlers; + local events_array = {}; + local event_handler_arrays = {}; + for event in pairs(events._event_map) do + local handlers = event_handlers[event]; + table.insert(events_array, event); + local handler_strings = {}; + for i, handler in ipairs(handlers) do + local upvals = debug.string_from_var_table(debug.get_upvalues_table(handler)); + handler_strings[i] = " "..i..": "..tostring(handler)..(upvals and ("\n "..upvals) or ""); + end + event_handler_arrays[event] = handler_strings; + end + table.sort(events_array); + local i = 1; + repeat + local handlers = event_handler_arrays[events_array[i]]; + for j=#handlers, 1, -1 do + table.insert(events_array, i+1, handlers[j]); + end + if i > 1 then events_array[i] = "\n"..events_array[i]; end + i = i + #handlers + 1 + until i == #events_array; + return table.concat(events_array, "\n"); end function get_upvalue(f, get_name) diff --git a/util/iterators.lua b/util/iterators.lua index aa0b172b..fb89f4a5 100644 --- a/util/iterators.lua +++ b/util/iterators.lua @@ -8,8 +8,10 @@ --[[ Iterators ]]-- +local it = {}; + -- Reverse an iterator -function reverse(f, s, var) +function it.reverse(f, s, var) local results = {}; -- First call the normal iterator @@ -34,12 +36,12 @@ end local function _keys_it(t, key) return (next(t, key)); end -function keys(t) +function it.keys(t) return _keys_it, t; end -- Iterate only over values in a table -function values(t) +function it.values(t) local key, val; return function (t) key, val = next(t, key); @@ -48,7 +50,7 @@ function values(t) end -- Given an iterator, iterate only over unique items -function unique(f, s, var) +function it.unique(f, s, var) local set = {}; return function () @@ -65,7 +67,7 @@ function unique(f, s, var) end --[[ Return the number of items an iterator returns ]]-- -function count(f, s, var) +function it.count(f, s, var) local x = 0; while true do @@ -79,7 +81,7 @@ function count(f, s, var) end -- Return the first n items an iterator returns -function head(n, f, s, var) +function it.head(n, f, s, var) local c = 0; return function (s, var) if c >= n then @@ -91,7 +93,7 @@ function head(n, f, s, var) end -- Skip the first n items an iterator returns -function skip(n, f, s, var) +function it.skip(n, f, s, var) for i=1,n do var = f(s, var); end @@ -99,7 +101,7 @@ function skip(n, f, s, var) end -- Return the last n items an iterator returns -function tail(n, f, s, var) +function it.tail(n, f, s, var) local results, count = {}, 0; while true do local ret = { f(s, var) }; @@ -121,13 +123,13 @@ function tail(n, f, s, var) end local function _range_iter(max, curr) if curr < max then return curr + 1; end end -function range(x, y) +function it.range(x, y) if not y then x, y = 1, x; end -- Default to 1..x if y not given return _range_iter, y, x-1; end -- Convert the values returned by an iterator to an array -function it2array(f, s, var) +function it.to_array(f, s, var) local t, var = {}; while true do var = f(s, var); @@ -139,7 +141,7 @@ end -- Treat the return of an iterator as key,value pairs, -- and build a table -function it2table(f, s, var) +function it.to_table(f, s, var) local t, var2 = {}; while true do var, var2 = f(s, var); @@ -149,3 +151,4 @@ function it2table(f, s, var) return t; end +return it; diff --git a/util/logger.lua b/util/logger.lua index c3bf3992..4fadb4b9 100644 --- a/util/logger.lua +++ b/util/logger.lua @@ -13,8 +13,7 @@ local ipairs, pairs, setmetatable = ipairs, pairs, setmetatable; module "logger" -local name_sinks, level_sinks = {}, {}; -local name_patterns = {}; +local level_sinks = {}; local make_logger; @@ -46,17 +45,7 @@ function make_logger(source_name, level) level_sinks[level] = level_handlers; end - local source_handlers = name_sinks[source_name]; - local logger = function (message, ...) - if source_handlers then - for i = 1,#source_handlers do - if source_handlers[i](source_name, level, message, ...) == false then - return; - end - end - end - for i = 1,#level_handlers do level_handlers[i](source_name, level, message, ...); end @@ -66,14 +55,12 @@ function make_logger(source_name, level) end function reset() - for k in pairs(name_sinks) do name_sinks[k] = nil; end for level, handler_list in pairs(level_sinks) do -- Clear all handlers for this level for i = 1, #handler_list do handler_list[i] = nil; end end - for k in pairs(name_patterns) do name_patterns[k] = nil; end end function add_level_sink(level, sink_function) @@ -84,22 +71,6 @@ function add_level_sink(level, sink_function) end end -function add_name_sink(name, sink_function, exclusive) - if not name_sinks[name] then - name_sinks[name] = { sink_function }; - else - name_sinks[name][#name_sinks[name] + 1] = sink_function; - end -end - -function add_name_pattern_sink(name_pattern, sink_function, exclusive) - if not name_patterns[name_pattern] then - name_patterns[name_pattern] = { sink_function }; - else - name_patterns[name_pattern][#name_patterns[name_pattern] + 1] = sink_function; - end -end - _M.new = make_logger; return _M; diff --git a/util/set.lua b/util/set.lua index e4cc2dff..050446ec 100644 --- a/util/set.lua +++ b/util/set.lua @@ -82,8 +82,10 @@ function new(list) end function set:add_list(list) - for _, item in ipairs(list) do - items[item] = true; + if list then + for _, item in ipairs(list) do + items[item] = true; + end end end |