Software /
code /
prosody
Annotate
util/helpers.lua @ 4953:24c2150cc8b0
Merge 0.9->trunk
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Mon, 09 Jul 2012 02:36:09 +0100 |
parent | 4705:447f5a94792d |
child | 5415:9e7aa5b47bdf |
rev | line source |
---|---|
1964
101a8df23b29
util.helpers: Add copyright header
Matthew Wild <mwild1@gmail.com>
parents:
1959
diff
changeset
|
1 -- Prosody IM |
2923
b7049746bd29
Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents:
1964
diff
changeset
|
2 -- Copyright (C) 2008-2010 Matthew Wild |
b7049746bd29
Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents:
1964
diff
changeset
|
3 -- Copyright (C) 2008-2010 Waqas Hussain |
1964
101a8df23b29
util.helpers: Add copyright header
Matthew Wild <mwild1@gmail.com>
parents:
1959
diff
changeset
|
4 -- |
101a8df23b29
util.helpers: Add copyright header
Matthew Wild <mwild1@gmail.com>
parents:
1959
diff
changeset
|
5 -- This project is MIT/X11 licensed. Please see the |
101a8df23b29
util.helpers: Add copyright header
Matthew Wild <mwild1@gmail.com>
parents:
1959
diff
changeset
|
6 -- COPYING file in the source package for more information. |
101a8df23b29
util.helpers: Add copyright header
Matthew Wild <mwild1@gmail.com>
parents:
1959
diff
changeset
|
7 -- |
1531
21051377f11b
util.helpers: New util library to aid with debugging, etc.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
8 |
4681
3299223bbed5
util.helpers: Add show_events(), to show the events and handlers in a util.events object
Matthew Wild <mwild1@gmail.com>
parents:
4536
diff
changeset
|
9 local debug = require "util.debug"; |
3299223bbed5
util.helpers: Add show_events(), to show the events and handlers in a util.events object
Matthew Wild <mwild1@gmail.com>
parents:
4536
diff
changeset
|
10 |
1531
21051377f11b
util.helpers: New util library to aid with debugging, etc.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
11 module("helpers", package.seeall); |
21051377f11b
util.helpers: New util library to aid with debugging, etc.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
12 |
21051377f11b
util.helpers: New util library to aid with debugging, etc.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
13 -- Helper functions for debugging |
21051377f11b
util.helpers: New util library to aid with debugging, etc.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
14 |
21051377f11b
util.helpers: New util library to aid with debugging, etc.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
15 local log = require "util.logger".init("util.debug"); |
21051377f11b
util.helpers: New util library to aid with debugging, etc.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
16 |
21051377f11b
util.helpers: New util library to aid with debugging, etc.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
17 function log_events(events, name, logger) |
21051377f11b
util.helpers: New util library to aid with debugging, etc.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
18 local f = events.fire_event; |
21051377f11b
util.helpers: New util library to aid with debugging, etc.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
19 if not f then |
21051377f11b
util.helpers: New util library to aid with debugging, etc.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
20 error("Object does not appear to be a util.events object"); |
21051377f11b
util.helpers: New util library to aid with debugging, etc.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
21 end |
21051377f11b
util.helpers: New util library to aid with debugging, etc.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
22 logger = logger or log; |
21051377f11b
util.helpers: New util library to aid with debugging, etc.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
23 name = name or tostring(events); |
21051377f11b
util.helpers: New util library to aid with debugging, etc.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
24 function events.fire_event(event, ...) |
21051377f11b
util.helpers: New util library to aid with debugging, etc.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
25 logger("debug", "%s firing event: %s", name, event); |
1795
0e933d6f2c31
util.helpers: It would be a good idea to fire an event when we say we are
Matthew Wild <mwild1@gmail.com>
parents:
1531
diff
changeset
|
26 return f(event, ...); |
1531
21051377f11b
util.helpers: New util library to aid with debugging, etc.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
27 end |
21051377f11b
util.helpers: New util library to aid with debugging, etc.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
28 events[events.fire_event] = f; |
21051377f11b
util.helpers: New util library to aid with debugging, etc.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
29 return events; |
21051377f11b
util.helpers: New util library to aid with debugging, etc.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
30 end |
21051377f11b
util.helpers: New util library to aid with debugging, etc.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
31 |
21051377f11b
util.helpers: New util library to aid with debugging, etc.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
32 function revert_log_events(events) |
4536
285450536ec0
util.helpers: After nearly 'fixing' this code, I conclude it instead only deserves a bigger smile
Matthew Wild <mwild1@gmail.com>
parents:
2923
diff
changeset
|
33 events.fire_event, events[events.fire_event] = events[events.fire_event], nil; -- :)) |
1531
21051377f11b
util.helpers: New util library to aid with debugging, etc.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
34 end |
21051377f11b
util.helpers: New util library to aid with debugging, etc.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
35 |
4705
447f5a94792d
util.helpers: show_events(): Make more robust, and allow filtering results to a specific event
Matthew Wild <mwild1@gmail.com>
parents:
4681
diff
changeset
|
36 function show_events(events, specific_event) |
4681
3299223bbed5
util.helpers: Add show_events(), to show the events and handlers in a util.events object
Matthew Wild <mwild1@gmail.com>
parents:
4536
diff
changeset
|
37 local event_handlers = events._handlers; |
3299223bbed5
util.helpers: Add show_events(), to show the events and handlers in a util.events object
Matthew Wild <mwild1@gmail.com>
parents:
4536
diff
changeset
|
38 local events_array = {}; |
3299223bbed5
util.helpers: Add show_events(), to show the events and handlers in a util.events object
Matthew Wild <mwild1@gmail.com>
parents:
4536
diff
changeset
|
39 local event_handler_arrays = {}; |
3299223bbed5
util.helpers: Add show_events(), to show the events and handlers in a util.events object
Matthew Wild <mwild1@gmail.com>
parents:
4536
diff
changeset
|
40 for event in pairs(events._event_map) do |
3299223bbed5
util.helpers: Add show_events(), to show the events and handlers in a util.events object
Matthew Wild <mwild1@gmail.com>
parents:
4536
diff
changeset
|
41 local handlers = event_handlers[event]; |
4705
447f5a94792d
util.helpers: show_events(): Make more robust, and allow filtering results to a specific event
Matthew Wild <mwild1@gmail.com>
parents:
4681
diff
changeset
|
42 if handlers and (event == specific_event or not specific_event) then |
447f5a94792d
util.helpers: show_events(): Make more robust, and allow filtering results to a specific event
Matthew Wild <mwild1@gmail.com>
parents:
4681
diff
changeset
|
43 table.insert(events_array, event); |
447f5a94792d
util.helpers: show_events(): Make more robust, and allow filtering results to a specific event
Matthew Wild <mwild1@gmail.com>
parents:
4681
diff
changeset
|
44 local handler_strings = {}; |
447f5a94792d
util.helpers: show_events(): Make more robust, and allow filtering results to a specific event
Matthew Wild <mwild1@gmail.com>
parents:
4681
diff
changeset
|
45 for i, handler in ipairs(handlers) do |
447f5a94792d
util.helpers: show_events(): Make more robust, and allow filtering results to a specific event
Matthew Wild <mwild1@gmail.com>
parents:
4681
diff
changeset
|
46 local upvals = debug.string_from_var_table(debug.get_upvalues_table(handler)); |
447f5a94792d
util.helpers: show_events(): Make more robust, and allow filtering results to a specific event
Matthew Wild <mwild1@gmail.com>
parents:
4681
diff
changeset
|
47 handler_strings[i] = " "..i..": "..tostring(handler)..(upvals and ("\n "..upvals) or ""); |
447f5a94792d
util.helpers: show_events(): Make more robust, and allow filtering results to a specific event
Matthew Wild <mwild1@gmail.com>
parents:
4681
diff
changeset
|
48 end |
447f5a94792d
util.helpers: show_events(): Make more robust, and allow filtering results to a specific event
Matthew Wild <mwild1@gmail.com>
parents:
4681
diff
changeset
|
49 event_handler_arrays[event] = handler_strings; |
4681
3299223bbed5
util.helpers: Add show_events(), to show the events and handlers in a util.events object
Matthew Wild <mwild1@gmail.com>
parents:
4536
diff
changeset
|
50 end |
3299223bbed5
util.helpers: Add show_events(), to show the events and handlers in a util.events object
Matthew Wild <mwild1@gmail.com>
parents:
4536
diff
changeset
|
51 end |
3299223bbed5
util.helpers: Add show_events(), to show the events and handlers in a util.events object
Matthew Wild <mwild1@gmail.com>
parents:
4536
diff
changeset
|
52 table.sort(events_array); |
3299223bbed5
util.helpers: Add show_events(), to show the events and handlers in a util.events object
Matthew Wild <mwild1@gmail.com>
parents:
4536
diff
changeset
|
53 local i = 1; |
4705
447f5a94792d
util.helpers: show_events(): Make more robust, and allow filtering results to a specific event
Matthew Wild <mwild1@gmail.com>
parents:
4681
diff
changeset
|
54 while i <= #events_array do |
4681
3299223bbed5
util.helpers: Add show_events(), to show the events and handlers in a util.events object
Matthew Wild <mwild1@gmail.com>
parents:
4536
diff
changeset
|
55 local handlers = event_handler_arrays[events_array[i]]; |
3299223bbed5
util.helpers: Add show_events(), to show the events and handlers in a util.events object
Matthew Wild <mwild1@gmail.com>
parents:
4536
diff
changeset
|
56 for j=#handlers, 1, -1 do |
3299223bbed5
util.helpers: Add show_events(), to show the events and handlers in a util.events object
Matthew Wild <mwild1@gmail.com>
parents:
4536
diff
changeset
|
57 table.insert(events_array, i+1, handlers[j]); |
3299223bbed5
util.helpers: Add show_events(), to show the events and handlers in a util.events object
Matthew Wild <mwild1@gmail.com>
parents:
4536
diff
changeset
|
58 end |
3299223bbed5
util.helpers: Add show_events(), to show the events and handlers in a util.events object
Matthew Wild <mwild1@gmail.com>
parents:
4536
diff
changeset
|
59 if i > 1 then events_array[i] = "\n"..events_array[i]; end |
3299223bbed5
util.helpers: Add show_events(), to show the events and handlers in a util.events object
Matthew Wild <mwild1@gmail.com>
parents:
4536
diff
changeset
|
60 i = i + #handlers + 1 |
4705
447f5a94792d
util.helpers: show_events(): Make more robust, and allow filtering results to a specific event
Matthew Wild <mwild1@gmail.com>
parents:
4681
diff
changeset
|
61 end |
4681
3299223bbed5
util.helpers: Add show_events(), to show the events and handlers in a util.events object
Matthew Wild <mwild1@gmail.com>
parents:
4536
diff
changeset
|
62 return table.concat(events_array, "\n"); |
3299223bbed5
util.helpers: Add show_events(), to show the events and handlers in a util.events object
Matthew Wild <mwild1@gmail.com>
parents:
4536
diff
changeset
|
63 end |
3299223bbed5
util.helpers: Add show_events(), to show the events and handlers in a util.events object
Matthew Wild <mwild1@gmail.com>
parents:
4536
diff
changeset
|
64 |
1959
f56670ce64de
util.helpers: Add get_upvalue(function, name) helper
Matthew Wild <mwild1@gmail.com>
parents:
1795
diff
changeset
|
65 function get_upvalue(f, get_name) |
f56670ce64de
util.helpers: Add get_upvalue(function, name) helper
Matthew Wild <mwild1@gmail.com>
parents:
1795
diff
changeset
|
66 local i, name, value = 0; |
f56670ce64de
util.helpers: Add get_upvalue(function, name) helper
Matthew Wild <mwild1@gmail.com>
parents:
1795
diff
changeset
|
67 repeat |
f56670ce64de
util.helpers: Add get_upvalue(function, name) helper
Matthew Wild <mwild1@gmail.com>
parents:
1795
diff
changeset
|
68 i = i + 1; |
f56670ce64de
util.helpers: Add get_upvalue(function, name) helper
Matthew Wild <mwild1@gmail.com>
parents:
1795
diff
changeset
|
69 name, value = debug.getupvalue(f, i); |
f56670ce64de
util.helpers: Add get_upvalue(function, name) helper
Matthew Wild <mwild1@gmail.com>
parents:
1795
diff
changeset
|
70 until name == get_name or name == nil; |
f56670ce64de
util.helpers: Add get_upvalue(function, name) helper
Matthew Wild <mwild1@gmail.com>
parents:
1795
diff
changeset
|
71 return value; |
f56670ce64de
util.helpers: Add get_upvalue(function, name) helper
Matthew Wild <mwild1@gmail.com>
parents:
1795
diff
changeset
|
72 end |
f56670ce64de
util.helpers: Add get_upvalue(function, name) helper
Matthew Wild <mwild1@gmail.com>
parents:
1795
diff
changeset
|
73 |
1531
21051377f11b
util.helpers: New util library to aid with debugging, etc.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
74 return _M; |