core/loggingmanager.lua

Thu, 23 Apr 2009 02:55:31 +0100

author
Matthew Wild <mwild1@gmail.com>
date
Thu, 23 Apr 2009 02:55:31 +0100
changeset 1046
6fef969ff307
parent 1031
ec013f93de81
child 1067
21f41b06f1d2
permissions
-rw-r--r--

core.loggingmanager: Reinstating global log() function

1016
73afe3e30e97 core.loggingmanager: A new manager (yay!) to manage log output
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1
73afe3e30e97 core.loggingmanager: A new manager (yay!) to manage log output
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 local format, rep = string.format, string.rep;
73afe3e30e97 core.loggingmanager: A new manager (yay!) to manage log output
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 local pcall = pcall;
73afe3e30e97 core.loggingmanager: A new manager (yay!) to manage log output
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 local debug = debug;
1031
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
5 local tostring, setmetatable, rawset, pairs, ipairs, type =
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
6 tostring, setmetatable, rawset, pairs, ipairs, type;
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
7 local io_open, io_write = io.open, io.write;
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
8 local math_max, rep = math.max, string.rep;
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
9 local os_getenv = os.getenv;
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
10 local getstyle, getstring = require "util.termcolours".getstyle, require "util.termcolours".getstring;
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
11
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
12 local config = require "core.configmanager";
1016
73afe3e30e97 core.loggingmanager: A new manager (yay!) to manage log output
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13
73afe3e30e97 core.loggingmanager: A new manager (yay!) to manage log output
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 local logger = require "util.logger";
73afe3e30e97 core.loggingmanager: A new manager (yay!) to manage log output
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15
1046
6fef969ff307 core.loggingmanager: Reinstating global log() function
Matthew Wild <mwild1@gmail.com>
parents: 1031
diff changeset
16 _G.log = logger.init("general");
6fef969ff307 core.loggingmanager: Reinstating global log() function
Matthew Wild <mwild1@gmail.com>
parents: 1031
diff changeset
17
1031
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
18 module "loggingmanager"
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
19
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
20 -- The log config used if none specified in the config file
1024
1bcc8ca57a7c core.loggingmanager: Add default logging settings (to console) and fill out code for adding sinks which catch all sources
Matthew Wild <mwild1@gmail.com>
parents: 1021
diff changeset
21 local default_logging = { { to = "console" } };
1bcc8ca57a7c core.loggingmanager: Add default logging settings (to console) and fill out code for adding sinks which catch all sources
Matthew Wild <mwild1@gmail.com>
parents: 1021
diff changeset
22
1031
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
23 -- The actual config loggingmanager is using
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
24 local logging_config = config.get("*", "core", "log") or default_logging;
1016
73afe3e30e97 core.loggingmanager: A new manager (yay!) to manage log output
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25
1031
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
26 local apply_sink_rules;
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
27 local log_sink_types = setmetatable({}, { __newindex = function (t, k, v) rawset(t, k, v); apply_sink_rules(k); end; });
1021
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
28 local get_levels;
1024
1bcc8ca57a7c core.loggingmanager: Add default logging settings (to console) and fill out code for adding sinks which catch all sources
Matthew Wild <mwild1@gmail.com>
parents: 1021
diff changeset
29 local logging_levels = { "debug", "info", "warn", "error", "critical" }
1016
73afe3e30e97 core.loggingmanager: A new manager (yay!) to manage log output
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30
1031
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
31 local function add_rule(sink_config)
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
32 local sink_maker = log_sink_types[sink_config.to];
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
33 if sink_maker then
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
34 if sink_config.levels and not sink_config.source then
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
35 -- Create sink
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
36 local sink = sink_maker(sink_config);
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
37
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
38 -- Set sink for all chosen levels
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
39 for level in pairs(get_levels(sink_config.levels)) do
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
40 logger.add_level_sink(level, sink);
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
41 end
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
42 elseif sink_config.source and not sink_config.levels then
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
43 logger.add_name_sink(sink_config.source, sink_maker(sink_config));
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
44 elseif sink_config.source and sink_config.levels then
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
45 local levels = get_levels(sink_config.levels);
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
46 local sink = sink_maker(sink_config);
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
47 logger.add_name_sink(sink_config.source,
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
48 function (name, level, ...)
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
49 if levels[level] then
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
50 return sink(name, level, ...);
1021
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
51 end
1031
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
52 end);
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
53 else
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
54 -- All sources
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
55 -- Create sink
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
56 local sink = sink_maker(sink_config);
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
57
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
58 -- Set sink for all levels
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
59 for _, level in pairs(logging_levels) do
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
60 logger.add_level_sink(level, sink);
1021
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
61 end
1016
73afe3e30e97 core.loggingmanager: A new manager (yay!) to manage log output
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 end
1031
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
63 else
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
64 -- No such sink type
1016
73afe3e30e97 core.loggingmanager: A new manager (yay!) to manage log output
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 end
73afe3e30e97 core.loggingmanager: A new manager (yay!) to manage log output
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 end
73afe3e30e97 core.loggingmanager: A new manager (yay!) to manage log output
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67
1031
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
68 -- Search for all rules using a particular sink type,
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
69 -- and apply them
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
70 function apply_sink_rules(sink_type)
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
71 if type(logging_config) == "table" then
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
72 for _, sink_config in pairs(logging_config) do
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
73 if sink_config.to == sink_type then
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
74 add_rule(sink_config);
1021
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
75 end
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
76 end
1031
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
77 elseif type(logging_config) == "string" and sink_type == "file" then
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
78 -- User specified simply a filename, and the "file" sink type
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
79 -- was just added
1021
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
80 end
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
81 end
1016
73afe3e30e97 core.loggingmanager: A new manager (yay!) to manage log output
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82
1021
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
83
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
84
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
85 --- Helper function to get a set of levels given a "criteria" table
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
86 function get_levels(criteria, set)
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
87 set = set or {};
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
88 if type(criteria) == "string" then
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
89 set[criteria] = true;
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
90 return set;
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
91 end
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
92 local min, max = criteria.min, criteria.max;
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
93 if min or max then
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
94 local in_range;
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
95 for _, level in ipairs(logging_levels) do
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
96 if min == level then
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
97 set[level] = true;
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
98 in_range = true;
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
99 elseif max == level then
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
100 set[level] = true;
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
101 return set;
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
102 elseif in_range then
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
103 set[level] = true;
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
104 end
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
105 end
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
106 end
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
107
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
108 for _, level in ipairs(criteria) do
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
109 set[level] = true;
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
110 end
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
111 return set;
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
112 end
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
113
1031
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
114 --- Definition of built-in logging sinks ---
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
115
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
116 function log_sink_types.nowhere()
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
117 return function () return false; end;
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
118 end
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
119
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
120 -- Column width for "source" (used by stdout and console)
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
121 local sourcewidth = 20;
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
122
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
123 function log_sink_types.stdout()
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
124 return function (name, level, message, ...)
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
125 sourcewidth = math_max(#name+2, sourcewidth);
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
126 local namelen = #name;
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
127 if ... then
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
128 io_write(name, rep(" ", sourcewidth-namelen), level, "\t", format(message, ...), "\n");
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
129 else
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
130 io_write(name, rep(" ", sourcewidth-namelen), level, "\t", message, "\n");
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
131 end
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
132 end
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
133 end
1021
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
134
1031
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
135 do
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
136 local do_pretty_printing = not os_getenv("WINDIR");
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
137
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
138 local logstyles = {};
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
139 if do_pretty_printing then
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
140 logstyles["info"] = getstyle("bold");
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
141 logstyles["warn"] = getstyle("bold", "yellow");
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
142 logstyles["error"] = getstyle("bold", "red");
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
143 end
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
144 function log_sink_types.console(config)
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
145 -- Really if we don't want pretty colours then just use plain stdout
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
146 if not do_pretty_printing then
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
147 return log_sink_types.stdout(config);
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
148 end
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
149
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
150 return function (name, level, message, ...)
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
151 sourcewidth = math_max(#name+2, sourcewidth);
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
152 local namelen = #name;
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
153 if ... then
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
154 io_write(name, rep(" ", sourcewidth-namelen), getstring(logstyles[level], level), "\t", format(message, ...), "\n");
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
155 else
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
156 io_write(name, rep(" ", sourcewidth-namelen), getstring(logstyles[level], level), "\t", message, "\n");
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
157 end
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
158 end
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
159 end
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
160 end
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
161
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
162 function log_sink_types.file(config)
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
163 local log = config.filename;
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
164 local logfile = io_open(log, "a+");
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
165 if not logfile then
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
166 return function () end
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
167 end
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
168
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
169 local write, format, flush = logfile.write, format, logfile.flush;
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
170 return function (name, level, message, ...)
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
171 if ... then
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
172 write(logfile, name, "\t", level, "\t", format(message, ...), "\n");
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
173 else
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
174 write(logfile, name, "\t" , level, "\t", message, "\n");
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
175 end
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
176 flush(logfile);
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
177 end;
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
178 end
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
179
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
180 function register_sink_type(name, sink_maker)
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
181 local old_sink_maker = log_sink_types[name];
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
182 log_sink_types[name] = sink_maker;
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
183 return old_sink_maker;
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
184 end
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
185
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
186 return _M;

mercurial