Software / code / prosody
Annotate
util/logger.lua @ 807:3ab6d7aec53f
Logging format improvement
| author | Waqas Hussain <waqas20@gmail.com> |
|---|---|
| date | Fri, 13 Feb 2009 22:10:29 +0500 |
| parent | 760:90ce865eebd8 |
| child | 883:0112ae30f399 |
| rev | line source |
|---|---|
| 759 | 1 -- Prosody IM v0.3 |
|
760
90ce865eebd8
Update copyright notices for 2009
Matthew Wild <mwild1@gmail.com>
parents:
759
diff
changeset
|
2 -- Copyright (C) 2008-2009 Matthew Wild |
|
90ce865eebd8
Update copyright notices for 2009
Matthew Wild <mwild1@gmail.com>
parents:
759
diff
changeset
|
3 -- Copyright (C) 2008-2009 Waqas Hussain |
|
519
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 local namelen = #name; |
| 30 | 37 return function (level, message, ...) |
|
582
8eb45a8099c4
Make it possible to set custom output handler for logger
Matthew Wild <mwild1@gmail.com>
parents:
519
diff
changeset
|
38 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
|
39 |
|
807
3ab6d7aec53f
Logging format improvement
Waqas Hussain <waqas20@gmail.com>
parents:
760
diff
changeset
|
40 sourcewidth = math_max(#name+2, sourcewidth); |
| 30 | 41 if ... then |
|
437
c1a720db2157
Nice enhancement for logging output
Matthew Wild <mwild1@gmail.com>
parents:
360
diff
changeset
|
42 io_write(name, rep(" ", sourcewidth-namelen), getstring(logstyles[level], level), "\t", format(message, ...), "\n"); |
| 30 | 43 else |
|
437
c1a720db2157
Nice enhancement for logging output
Matthew Wild <mwild1@gmail.com>
parents:
360
diff
changeset
|
44 io_write(name, rep(" ", sourcewidth-namelen), getstring(logstyles[level], level), "\t", message, "\n"); |
| 30 | 45 end |
| 46 end | |
| 47 end | |
| 48 | |
|
582
8eb45a8099c4
Make it possible to set custom output handler for logger
Matthew Wild <mwild1@gmail.com>
parents:
519
diff
changeset
|
49 function setwriter(f) |
|
716
d61eabc678a6
util/logger: setwriter now returns the old writer on success
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
50 local old_func = outfunction; |
|
d61eabc678a6
util/logger: setwriter now returns the old writer on success
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
51 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
|
52 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
|
53 if ok then |
|
8eb45a8099c4
Make it possible to set custom output handler for logger
Matthew Wild <mwild1@gmail.com>
parents:
519
diff
changeset
|
54 outfunction = f; |
|
716
d61eabc678a6
util/logger: setwriter now returns the old writer on success
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
55 ret = old_func; |
|
582
8eb45a8099c4
Make it possible to set custom output handler for logger
Matthew Wild <mwild1@gmail.com>
parents:
519
diff
changeset
|
56 end |
|
8eb45a8099c4
Make it possible to set custom output handler for logger
Matthew Wild <mwild1@gmail.com>
parents:
519
diff
changeset
|
57 return ok, ret; |
|
8eb45a8099c4
Make it possible to set custom output handler for logger
Matthew Wild <mwild1@gmail.com>
parents:
519
diff
changeset
|
58 end |
|
8eb45a8099c4
Make it possible to set custom output handler for logger
Matthew Wild <mwild1@gmail.com>
parents:
519
diff
changeset
|
59 |
|
360
e918c979ad1a
Remove or comment useless prints, or change them to log()
Matthew Wild <mwild1@gmail.com>
parents:
262
diff
changeset
|
60 return _M; |