Software /
code /
prosody
Annotate
util/helpers.lua @ 1959:f56670ce64de
util.helpers: Add get_upvalue(function, name) helper
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Fri, 16 Oct 2009 22:03:32 +0100 |
parent | 1795:0e933d6f2c31 |
child | 1964:101a8df23b29 |
rev | line source |
---|---|
1531
21051377f11b
util.helpers: New util library to aid with debugging, etc.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
1 |
21051377f11b
util.helpers: New util library to aid with debugging, etc.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
2 module("helpers", package.seeall); |
21051377f11b
util.helpers: New util library to aid with debugging, etc.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
3 |
21051377f11b
util.helpers: New util library to aid with debugging, etc.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
4 -- Helper functions for debugging |
21051377f11b
util.helpers: New util library to aid with debugging, etc.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
5 |
21051377f11b
util.helpers: New util library to aid with debugging, etc.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
6 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
|
7 |
21051377f11b
util.helpers: New util library to aid with debugging, etc.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
8 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
|
9 local f = events.fire_event; |
21051377f11b
util.helpers: New util library to aid with debugging, etc.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
10 if not f then |
21051377f11b
util.helpers: New util library to aid with debugging, etc.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
11 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
|
12 end |
21051377f11b
util.helpers: New util library to aid with debugging, etc.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
13 logger = logger or log; |
21051377f11b
util.helpers: New util library to aid with debugging, etc.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
14 name = name or tostring(events); |
21051377f11b
util.helpers: New util library to aid with debugging, etc.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
15 function events.fire_event(event, ...) |
21051377f11b
util.helpers: New util library to aid with debugging, etc.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
16 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
|
17 return f(event, ...); |
1531
21051377f11b
util.helpers: New util library to aid with debugging, etc.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
18 end |
21051377f11b
util.helpers: New util library to aid with debugging, etc.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
19 events[events.fire_event] = f; |
21051377f11b
util.helpers: New util library to aid with debugging, etc.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
20 return events; |
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 |
21051377f11b
util.helpers: New util library to aid with debugging, etc.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
23 function revert_log_events(events) |
21051377f11b
util.helpers: New util library to aid with debugging, etc.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
24 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
|
25 end |
21051377f11b
util.helpers: New util library to aid with debugging, etc.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
26 |
1959
f56670ce64de
util.helpers: Add get_upvalue(function, name) helper
Matthew Wild <mwild1@gmail.com>
parents:
1795
diff
changeset
|
27 function get_upvalue(f, get_name) |
f56670ce64de
util.helpers: Add get_upvalue(function, name) helper
Matthew Wild <mwild1@gmail.com>
parents:
1795
diff
changeset
|
28 local i, name, value = 0; |
f56670ce64de
util.helpers: Add get_upvalue(function, name) helper
Matthew Wild <mwild1@gmail.com>
parents:
1795
diff
changeset
|
29 repeat |
f56670ce64de
util.helpers: Add get_upvalue(function, name) helper
Matthew Wild <mwild1@gmail.com>
parents:
1795
diff
changeset
|
30 i = i + 1; |
f56670ce64de
util.helpers: Add get_upvalue(function, name) helper
Matthew Wild <mwild1@gmail.com>
parents:
1795
diff
changeset
|
31 name, value = debug.getupvalue(f, i); |
f56670ce64de
util.helpers: Add get_upvalue(function, name) helper
Matthew Wild <mwild1@gmail.com>
parents:
1795
diff
changeset
|
32 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
|
33 return value; |
f56670ce64de
util.helpers: Add get_upvalue(function, name) helper
Matthew Wild <mwild1@gmail.com>
parents:
1795
diff
changeset
|
34 end |
f56670ce64de
util.helpers: Add get_upvalue(function, name) helper
Matthew Wild <mwild1@gmail.com>
parents:
1795
diff
changeset
|
35 |
1531
21051377f11b
util.helpers: New util library to aid with debugging, etc.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
36 return _M; |