Comparison

init.lua @ 44:37396504de5f

verse: Multiple changes to allow controlling logging for both verse and streams
author Matthew Wild <mwild1@gmail.com>
date Wed, 06 Jan 2010 17:42:37 +0000
parent 42:6006e6bb1c28
child 45:50a2e4fb0a16
comparison
equal deleted inserted replaced
43:a33036b7e5ab 44:37396504de5f
12 stream.__index = stream; 12 stream.__index = stream;
13 stream_mt = stream; 13 stream_mt = stream;
14 14
15 verse.plugins = {}; 15 verse.plugins = {};
16 16
17 function verse.new(base) 17 function verse.new(logger, base)
18 local t = base or {}; 18 local t = setmetatable(base or {}, stream);
19 t.id = tostring(t):match("%x*$"); 19 t.id = tostring(t):match("%x*$");
20 t.logger = logger.init(t.id); 20 t:set_logger(logger, true);
21 t.events = events.new(); 21 t.events = events.new();
22 return setmetatable(t, stream); 22 return t;
23 end 23 end
24 24
25 verse.add_task = require "util.timer".add_task; 25 verse.add_task = require "util.timer".add_task;
26 26
27 function verse.loop() 27 function verse.loop()
28 return server.loop(); 28 return server.loop();
29 end
30
31 verse.logger = logger.init;
32
33 function verse.set_logger(logger)
34 server.setlogger(logger);
29 end 35 end
30 36
31 function stream:connect(connect_host, connect_port) 37 function stream:connect(connect_host, connect_port)
32 connect_host = connect_host or "localhost"; 38 connect_host = connect_host or "localhost";
33 connect_port = tonumber(connect_port) or 5222; 39 connect_port = tonumber(connect_port) or 5222;
53 self.send = function (_, data) return w(conn, t(data)); end 59 self.send = function (_, data) return w(conn, t(data)); end
54 end 60 end
55 61
56 -- Logging functions 62 -- Logging functions
57 function stream:debug(...) 63 function stream:debug(...)
58 return self.logger("debug", ...); 64 if self.logger and self.log.debug then
65 return self.logger("debug", ...);
66 end
59 end 67 end
60 68
61 function stream:warn(...) 69 function stream:warn(...)
62 return self.logger("warn", ...); 70 if self.logger and self.log.warn then
71 return self.logger("warn", ...);
72 end
63 end 73 end
64 74
65 function stream:error(...) 75 function stream:error(...)
66 return self.logger("error", ...); 76 if self.logger and self.log.error then
77 return self.logger("error", ...);
78 end
79 end
80
81 function stream:set_logger(logger, levels)
82 local old_logger = self.logger;
83 if logger then
84 self.logger = logger;
85 end
86 if levels then
87 if levels == true then
88 levels = { "debug", "info", "warn", "error" };
89 end
90 self.log = {};
91 for _, level in ipairs(levels) do
92 self.log[level] = true;
93 end
94 end
95 return old_logger;
67 end 96 end
68 97
69 -- Event handling 98 -- Event handling
70 function stream:event(name, ...) 99 function stream:event(name, ...)
71 self:debug("Firing event: "..tostring(name)); 100 self:debug("Firing event: "..tostring(name));