util/logger.lua

Thu, 03 Dec 2020 17:05:27 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Thu, 03 Dec 2020 17:05:27 +0000
changeset 0
550f506de75a
permissions
-rw-r--r--

Initial commit

0
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 -- Prosody IM
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 -- Copyright (C) 2008-2010 Matthew Wild
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 -- Copyright (C) 2008-2010 Waqas Hussain
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 --
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 -- This project is MIT/X11 licensed. Please see the
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 -- COPYING file in the source package for more information.
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 --
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 -- luacheck: ignore 213/level
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 local pairs = pairs;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 local ipairs = ipairs;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 local require = require;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 local _ENV = nil;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 -- luacheck: std none
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 local level_sinks = {};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 local make_logger;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 local function init(name)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 local log_debug = make_logger(name, "debug");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 local log_info = make_logger(name, "info");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 local log_warn = make_logger(name, "warn");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 local log_error = make_logger(name, "error");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 return function (level, message, ...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 if level == "debug" then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 return log_debug(message, ...);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 elseif level == "info" then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 return log_info(message, ...);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 elseif level == "warn" then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 return log_warn(message, ...);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 elseif level == "error" then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 return log_error(message, ...);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 function make_logger(source_name, level)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 local level_handlers = level_sinks[level];
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 if not level_handlers then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 level_handlers = {};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 level_sinks[level] = level_handlers;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 local logger = function (message, ...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 for i = 1,#level_handlers do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 level_handlers[i](source_name, level, message, ...);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 return logger;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 local function reset()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 for level, handler_list in pairs(level_sinks) do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 -- Clear all handlers for this level
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 for i = 1, #handler_list do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 handler_list[i] = nil;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 local function add_level_sink(level, sink_function)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 if not level_sinks[level] then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 level_sinks[level] = { sink_function };
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 level_sinks[level][#level_sinks[level] + 1 ] = sink_function;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 local function add_simple_sink(simple_sink_function, levels)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74 local format = require "util.format".format;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 local function sink_function(name, level, msg, ...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76 return simple_sink_function(name, level, format(msg, ...));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 for _, level in ipairs(levels or {"debug", "info", "warn", "error"}) do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79 add_level_sink(level, sink_function);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
83 return {
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84 init = init;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85 make_logger = make_logger;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86 reset = reset;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87 add_level_sink = add_level_sink;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88 add_simple_sink = add_simple_sink;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
89 new = make_logger;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90 };

mercurial