2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
9 local debug
= require
"util.debug";
11 -- Helper functions for debugging
13 local log = require
"util.logger".init("util.debug");
15 local function log_events(events
, name
, logger
)
16 local f
= events
.fire_event
;
18 error("Object does not appear to be a util.events object");
20 logger
= logger
or log;
21 name
= name
or tostring(events
);
22 function events
.fire_event(event
, ...)
23 logger("debug", "%s firing event: %s", name
, event
);
26 events
[events
.fire_event
] = f
;
30 local function revert_log_events(events
)
31 events
.fire_event
, events
[events
.fire_event
] = events
[events
.fire_event
], nil; -- :))
34 local function log_host_events(host
)
35 return log_events(prosody
.hosts
[host
].events
, host
);
38 local function revert_log_host_events(host
)
39 return revert_log_events(prosody
.hosts
[host
].events
);
42 local function show_events(events
, specific_event
)
43 local event_handlers
= events
._handlers
;
44 local events_array
= {};
45 local event_handler_arrays
= {};
46 for event
, priorities
in pairs(events
._event_map
) do
47 local handlers
= event_handlers
[event
];
48 if handlers
and (event
== specific_event
or not specific_event
) then
49 table.insert(events_array
, event
);
50 local handler_strings
= {};
51 for i
, handler
in ipairs(handlers
) do
52 local upvals
= debug
.string_from_var_table(debug
.get_upvalues_table(handler
));
53 handler_strings
[i
] = " "..(priorities
[handler
] or "?")..": "..tostring(handler
)..(upvals
and ("\n "..upvals
) or "");
55 event_handler_arrays
[event
] = handler_strings
;
58 table.sort(events_array
);
60 while i
<= #events_array
do
61 local handlers
= event_handler_arrays
[events_array
[i]]
;
62 for j
=#handlers
, 1, -1 do
63 table.insert(events_array
, i
+1, handlers
[j
]);
65 if i
> 1 then events_array
[i
] = "\n"..events_array
[i
]; end
68 return table.concat(events_array
, "\n");
71 local function get_upvalue(f
, get_name
)
72 local i
, name
, value
= 0;
75 name
, value
= debug
.getupvalue(f
, i
);
76 until name
== get_name
or name
== nil;
81 log_host_events
= log_host_events
;
82 revert_log_host_events
= revert_log_host_events
;
83 log_events
= log_events
;
84 revert_log_events
= revert_log_events
;
85 show_events
= show_events
;
86 get_upvalue
= get_upvalue
;