author | Matthew Wild <mwild1@gmail.com> |
Fri, 30 Jan 2009 17:22:56 +0000 | |
changeset 758 | b1885732e979 |
parent 716 | d61eabc678a6 |
child 759 | 5cccfb5da6cb |
permissions | -rw-r--r-- |
615 | 1 |
-- Prosody IM v0.2 |
519
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
437
diff
changeset
|
2 |
-- Copyright (C) 2008 Matthew Wild |
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
437
diff
changeset
|
3 |
-- Copyright (C) 2008 Waqas Hussain |
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
437
diff
changeset
|
4 |
-- |
758 | 5 |
-- This project is MIT/X11 licensed. Please see the |
6 |
-- COPYING file in the source package for more information. |
|
519
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
437
diff
changeset
|
7 |
-- |
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
437
diff
changeset
|
8 |
|
437
c1a720db2157
Nice enhancement for logging output
Matthew Wild <mwild1@gmail.com>
parents:
360
diff
changeset
|
9 |
local format, rep = string.format, string.rep; |
c1a720db2157
Nice enhancement for logging output
Matthew Wild <mwild1@gmail.com>
parents:
360
diff
changeset
|
10 |
local io_write = io.write; |
582
8eb45a8099c4
Make it possible to set custom output handler for logger
Matthew Wild <mwild1@gmail.com>
parents:
519
diff
changeset
|
11 |
local pcall = pcall; |
30 | 12 |
local debug = debug; |
13 |
local tostring = tostring; |
|
437
c1a720db2157
Nice enhancement for logging output
Matthew Wild <mwild1@gmail.com>
parents:
360
diff
changeset
|
14 |
local math_max = math.max; |
262 | 15 |
|
16 |
local getstyle, getstring = require "util.termcolours".getstyle, require "util.termcolours".getstring; |
|
17 |
local do_pretty_printing = not os.getenv("WINDIR"); |
|
18 |
||
30 | 19 |
module "logger" |
20 |
||
262 | 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 |
||
437
c1a720db2157
Nice enhancement for logging output
Matthew Wild <mwild1@gmail.com>
parents:
360
diff
changeset
|
30 |
local sourcewidth = 20; |
c1a720db2157
Nice enhancement for logging output
Matthew Wild <mwild1@gmail.com>
parents:
360
diff
changeset
|
31 |
|
582
8eb45a8099c4
Make it possible to set custom output handler for logger
Matthew Wild <mwild1@gmail.com>
parents:
519
diff
changeset
|
32 |
local outfunction = nil; |
8eb45a8099c4
Make it possible to set custom output handler for logger
Matthew Wild <mwild1@gmail.com>
parents:
519
diff
changeset
|
33 |
|
30 | 34 |
function init(name) |
147 | 35 |
--name = nil; -- While this line is not commented, will automatically fill in file/line number info |
437
c1a720db2157
Nice enhancement for logging output
Matthew Wild <mwild1@gmail.com>
parents:
360
diff
changeset
|
36 |
sourcewidth = math_max(#name+2, sourcewidth); |
c1a720db2157
Nice enhancement for logging output
Matthew Wild <mwild1@gmail.com>
parents:
360
diff
changeset
|
37 |
local namelen = #name; |
30 | 38 |
return function (level, message, ...) |
39 |
if not name then |
|
53
14ea0fe6ca86
Session destruction fixes, some debugging code while we fix the rest. Also change logger to be more useful.
Matthew Wild <mwild1@gmail.com>
parents:
30
diff
changeset
|
40 |
local inf = debug.getinfo(3, 'Snl'); |
30 | 41 |
level = level .. ","..tostring(inf.short_src):match("[^/]*$")..":"..inf.currentline; |
42 |
end |
|
582
8eb45a8099c4
Make it possible to set custom output handler for logger
Matthew Wild <mwild1@gmail.com>
parents:
519
diff
changeset
|
43 |
|
8eb45a8099c4
Make it possible to set custom output handler for logger
Matthew Wild <mwild1@gmail.com>
parents:
519
diff
changeset
|
44 |
if outfunction then return outfunction(name, level, message, ...); end |
8eb45a8099c4
Make it possible to set custom output handler for logger
Matthew Wild <mwild1@gmail.com>
parents:
519
diff
changeset
|
45 |
|
30 | 46 |
if ... then |
437
c1a720db2157
Nice enhancement for logging output
Matthew Wild <mwild1@gmail.com>
parents:
360
diff
changeset
|
47 |
io_write(name, rep(" ", sourcewidth-namelen), getstring(logstyles[level], level), "\t", format(message, ...), "\n"); |
30 | 48 |
else |
437
c1a720db2157
Nice enhancement for logging output
Matthew Wild <mwild1@gmail.com>
parents:
360
diff
changeset
|
49 |
io_write(name, rep(" ", sourcewidth-namelen), getstring(logstyles[level], level), "\t", message, "\n"); |
30 | 50 |
end |
51 |
end |
|
52 |
end |
|
53 |
||
582
8eb45a8099c4
Make it possible to set custom output handler for logger
Matthew Wild <mwild1@gmail.com>
parents:
519
diff
changeset
|
54 |
function setwriter(f) |
716
d61eabc678a6
util/logger: setwriter now returns the old writer on success
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
55 |
local old_func = outfunction; |
d61eabc678a6
util/logger: setwriter now returns the old writer on success
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
56 |
if not f then outfunction = nil; return true, old_func; end |
582
8eb45a8099c4
Make it possible to set custom output handler for logger
Matthew Wild <mwild1@gmail.com>
parents:
519
diff
changeset
|
57 |
local ok, ret = pcall(f, "logger", "info", "Switched logging output successfully"); |
8eb45a8099c4
Make it possible to set custom output handler for logger
Matthew Wild <mwild1@gmail.com>
parents:
519
diff
changeset
|
58 |
if ok then |
8eb45a8099c4
Make it possible to set custom output handler for logger
Matthew Wild <mwild1@gmail.com>
parents:
519
diff
changeset
|
59 |
outfunction = f; |
716
d61eabc678a6
util/logger: setwriter now returns the old writer on success
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
60 |
ret = old_func; |
582
8eb45a8099c4
Make it possible to set custom output handler for logger
Matthew Wild <mwild1@gmail.com>
parents:
519
diff
changeset
|
61 |
end |
8eb45a8099c4
Make it possible to set custom output handler for logger
Matthew Wild <mwild1@gmail.com>
parents:
519
diff
changeset
|
62 |
return ok, ret; |
8eb45a8099c4
Make it possible to set custom output handler for logger
Matthew Wild <mwild1@gmail.com>
parents:
519
diff
changeset
|
63 |
end |
8eb45a8099c4
Make it possible to set custom output handler for logger
Matthew Wild <mwild1@gmail.com>
parents:
519
diff
changeset
|
64 |
|
360
e918c979ad1a
Remove or comment useless prints, or change them to log()
Matthew Wild <mwild1@gmail.com>
parents:
262
diff
changeset
|
65 |
return _M; |