Comparison

util/logger.lua @ 3556:d00fc909296e

util.logger: Remove support for a global writer (setwriter) to try and balance the forces...
author Matthew Wild <mwild1@gmail.com>
date Thu, 04 Nov 2010 08:42:18 +0000
parent 3554:1770f8aaf04a
child 4627:b046cafc81a8
comparison
equal deleted inserted replaced
3555:6938b6153890 3556:d00fc909296e
15 15
16 local name_sinks, level_sinks = {}, {}; 16 local name_sinks, level_sinks = {}, {};
17 local name_patterns = {}; 17 local name_patterns = {};
18 18
19 local make_logger; 19 local make_logger;
20 local outfunction = nil;
21 20
22 function init(name) 21 function init(name)
23 local log_debug = make_logger(name, "debug"); 22 local log_debug = make_logger(name, "debug");
24 local log_info = make_logger(name, "info"); 23 local log_info = make_logger(name, "info");
25 local log_warn = make_logger(name, "warn"); 24 local log_warn = make_logger(name, "warn");
26 local log_error = make_logger(name, "error"); 25 local log_error = make_logger(name, "error");
27 26
28 --name = nil; -- While this line is not commented, will automatically fill in file/line number info 27 --name = nil; -- While this line is not commented, will automatically fill in file/line number info
29 local namelen = #name; 28 local namelen = #name;
30 return function (level, message, ...) 29 return function (level, message, ...)
31 if outfunction then return outfunction(name, level, message, ...); end
32
33 if level == "debug" then 30 if level == "debug" then
34 return log_debug(message, ...); 31 return log_debug(message, ...);
35 elseif level == "info" then 32 elseif level == "info" then
36 return log_info(message, ...); 33 return log_info(message, ...);
37 elseif level == "warn" then 34 elseif level == "warn" then
64 level_handlers[i](source_name, level, message, ...); 61 level_handlers[i](source_name, level, message, ...);
65 end 62 end
66 end 63 end
67 64
68 return logger; 65 return logger;
69 end
70
71 function setwriter(f)
72 local old_func = outfunction;
73 if not f then outfunction = nil; return true, old_func; end
74 local ok, ret = pcall(f, "logger", "info", "Switched logging output successfully");
75 if ok then
76 outfunction = f;
77 ret = old_func;
78 end
79 return ok, ret;
80 end 66 end
81 67
82 function reset() 68 function reset()
83 for k in pairs(name_sinks) do name_sinks[k] = nil; end 69 for k in pairs(name_sinks) do name_sinks[k] = nil; end
84 for level, handler_list in pairs(level_sinks) do 70 for level, handler_list in pairs(level_sinks) do