Software /
code /
prosody
Comparison
core/loggingmanager.lua @ 1016:73afe3e30e97
core.loggingmanager: A new manager (yay!) to manage log output
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Tue, 21 Apr 2009 03:37:49 +0100 |
child | 1021:f9122efeaadd |
comparison
equal
deleted
inserted
replaced
1015:9e31e9397cff | 1016:73afe3e30e97 |
---|---|
1 | |
2 local format, rep = string.format, string.rep; | |
3 local io_write = io.write; | |
4 local pcall = pcall; | |
5 local debug = debug; | |
6 local tostring = tostring; | |
7 local math_max = math.max; | |
8 | |
9 local logger = require "util.logger"; | |
10 | |
11 -- Global log function, because some people are too | |
12 -- lazy to get their own | |
13 _G.log = logger.init("general"); | |
14 | |
15 -- Disable log output, needs to read from config | |
16 -- logger.setwriter(function () end); | |
17 | |
18 local getstyle, getstring = require "util.termcolours".getstyle, require "util.termcolours".getstring; | |
19 local do_pretty_printing = not os.getenv("WINDIR"); | |
20 | |
21 local logstyles = {}; | |
22 | |
23 --TODO: This should be done in config, but we don't have proper config yet | |
24 if do_pretty_printing then | |
25 logstyles["info"] = getstyle("bold"); | |
26 logstyles["warn"] = getstyle("bold", "yellow"); | |
27 logstyles["error"] = getstyle("bold", "red"); | |
28 end | |
29 | |
30 local sourcewidth = 20; | |
31 local math_max, rep = math.max, string.rep; | |
32 local function make_default_log_sink(level) | |
33 return function (name, _level, message, ...) | |
34 sourcewidth = math_max(#name+2, sourcewidth); | |
35 local namelen = #name; | |
36 if ... then | |
37 io_write(name, rep(" ", sourcewidth-namelen), getstring(logstyles[level], level), "\t", format(message, ...), "\n"); | |
38 else | |
39 io_write(name, rep(" ", sourcewidth-namelen), getstring(logstyles[level], level), "\t", message, "\n"); | |
40 end | |
41 end | |
42 end | |
43 | |
44 -- Set default sinks | |
45 logger.add_level_sink("debug", make_default_log_sink("debug")); | |
46 logger.add_level_sink("info", make_default_log_sink("info")); | |
47 logger.add_level_sink("warn", make_default_log_sink("warn")); | |
48 logger.add_level_sink("error", make_default_log_sink("error")); | |
49 |