Software /
code /
prosody
Annotate
util/helpers.lua @ 4299:e2674029b8c9
Merge 0.8->trunk
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Fri, 03 Jun 2011 05:35:22 +0100 |
parent | 2923:b7049746bd29 |
child | 4536:285450536ec0 |
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 |
21051377f11b
util.helpers: New util library to aid with debugging, etc.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
9 module("helpers", package.seeall); |
21051377f11b
util.helpers: New util library to aid with debugging, etc.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
10 |
21051377f11b
util.helpers: New util library to aid with debugging, etc.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
11 -- Helper functions for debugging |
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 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
|
14 |
21051377f11b
util.helpers: New util library to aid with debugging, etc.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
15 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
|
16 local f = events.fire_event; |
21051377f11b
util.helpers: New util library to aid with debugging, etc.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
17 if not f then |
21051377f11b
util.helpers: New util library to aid with debugging, etc.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
18 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
|
19 end |
21051377f11b
util.helpers: New util library to aid with debugging, etc.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
20 logger = logger or log; |
21051377f11b
util.helpers: New util library to aid with debugging, etc.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
21 name = name or tostring(events); |
21051377f11b
util.helpers: New util library to aid with debugging, etc.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
22 function events.fire_event(event, ...) |
21051377f11b
util.helpers: New util library to aid with debugging, etc.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
23 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
|
24 return f(event, ...); |
1531
21051377f11b
util.helpers: New util library to aid with debugging, etc.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
25 end |
21051377f11b
util.helpers: New util library to aid with debugging, etc.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
26 events[events.fire_event] = f; |
21051377f11b
util.helpers: New util library to aid with debugging, etc.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
27 return events; |
21051377f11b
util.helpers: New util library to aid with debugging, etc.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
28 end |
21051377f11b
util.helpers: New util library to aid with debugging, etc.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
29 |
21051377f11b
util.helpers: New util library to aid with debugging, etc.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
30 function revert_log_events(events) |
21051377f11b
util.helpers: New util library to aid with debugging, etc.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
31 events.fire_event, events[events.fire_event] = events[events.fire_event], nil; -- :) |
21051377f11b
util.helpers: New util library to aid with debugging, etc.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
32 end |
21051377f11b
util.helpers: New util library to aid with debugging, etc.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
33 |
1959
f56670ce64de
util.helpers: Add get_upvalue(function, name) helper
Matthew Wild <mwild1@gmail.com>
parents:
1795
diff
changeset
|
34 function get_upvalue(f, get_name) |
f56670ce64de
util.helpers: Add get_upvalue(function, name) helper
Matthew Wild <mwild1@gmail.com>
parents:
1795
diff
changeset
|
35 local i, name, value = 0; |
f56670ce64de
util.helpers: Add get_upvalue(function, name) helper
Matthew Wild <mwild1@gmail.com>
parents:
1795
diff
changeset
|
36 repeat |
f56670ce64de
util.helpers: Add get_upvalue(function, name) helper
Matthew Wild <mwild1@gmail.com>
parents:
1795
diff
changeset
|
37 i = i + 1; |
f56670ce64de
util.helpers: Add get_upvalue(function, name) helper
Matthew Wild <mwild1@gmail.com>
parents:
1795
diff
changeset
|
38 name, value = debug.getupvalue(f, i); |
f56670ce64de
util.helpers: Add get_upvalue(function, name) helper
Matthew Wild <mwild1@gmail.com>
parents:
1795
diff
changeset
|
39 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
|
40 return value; |
f56670ce64de
util.helpers: Add get_upvalue(function, name) helper
Matthew Wild <mwild1@gmail.com>
parents:
1795
diff
changeset
|
41 end |
f56670ce64de
util.helpers: Add get_upvalue(function, name) helper
Matthew Wild <mwild1@gmail.com>
parents:
1795
diff
changeset
|
42 |
1531
21051377f11b
util.helpers: New util library to aid with debugging, etc.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
43 return _M; |