Comparison

util/logger.lua @ 6777:5de6b93d0190

util.*: Remove use of module() function, make all module functions local and return them in a table at the end
author Kim Alvefur <zash@zash.se>
date Sat, 21 Feb 2015 10:36:37 +0100
parent 5776:bd0ff8ae98a8
child 7130:2b48ce0bfe7d
comparison
equal deleted inserted replaced
6774:3965662ae091 6777:5de6b93d0190
9 local pcall = pcall; 9 local pcall = pcall;
10 10
11 local find = string.find; 11 local find = string.find;
12 local ipairs, pairs, setmetatable = ipairs, pairs, setmetatable; 12 local ipairs, pairs, setmetatable = ipairs, pairs, setmetatable;
13 13
14 module "logger" 14 local _ENV = nil;
15 15
16 local level_sinks = {}; 16 local level_sinks = {};
17 17
18 local make_logger; 18 local make_logger;
19 19
20 function init(name) 20 local function init(name)
21 local log_debug = make_logger(name, "debug"); 21 local log_debug = make_logger(name, "debug");
22 local log_info = make_logger(name, "info"); 22 local log_info = make_logger(name, "info");
23 local log_warn = make_logger(name, "warn"); 23 local log_warn = make_logger(name, "warn");
24 local log_error = make_logger(name, "error"); 24 local log_error = make_logger(name, "error");
25 25
50 end 50 end
51 51
52 return logger; 52 return logger;
53 end 53 end
54 54
55 function reset() 55 local function reset()
56 for level, handler_list in pairs(level_sinks) do 56 for level, handler_list in pairs(level_sinks) do
57 -- Clear all handlers for this level 57 -- Clear all handlers for this level
58 for i = 1, #handler_list do 58 for i = 1, #handler_list do
59 handler_list[i] = nil; 59 handler_list[i] = nil;
60 end 60 end
61 end 61 end
62 end 62 end
63 63
64 function add_level_sink(level, sink_function) 64 local function add_level_sink(level, sink_function)
65 if not level_sinks[level] then 65 if not level_sinks[level] then
66 level_sinks[level] = { sink_function }; 66 level_sinks[level] = { sink_function };
67 else 67 else
68 level_sinks[level][#level_sinks[level] + 1 ] = sink_function; 68 level_sinks[level][#level_sinks[level] + 1 ] = sink_function;
69 end 69 end
70 end 70 end
71 71
72 _M.new = make_logger; 72 return {
73 73 init = init;
74 return _M; 74 make_logger = make_logger;
75 reset = reset;
76 add_level_sink = add_level_sink;
77 new = make_logger;
78 };