Annotate

tests/util/logger.lua @ 2091:6fd686a45806

net.server_event: Initial commit of server_event.lua. Don't get too excited, it's not used at all yet, and is still incomplete :)
author Matthew Wild <mwild1@gmail.com>
date Fri, 20 Nov 2009 22:58:56 +0000
parent 1522:569d58d21612
child 2923:b7049746bd29
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1522
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 272
diff changeset
1 -- Prosody IM
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 272
diff changeset
2 -- Copyright (C) 2008-2009 Matthew Wild
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 272
diff changeset
3 -- Copyright (C) 2008-2009 Waqas Hussain
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 272
diff changeset
4 --
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 272
diff changeset
5 -- This project is MIT/X11 licensed. Please see the
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 272
diff changeset
6 -- COPYING file in the source package for more information.
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 272
diff changeset
7 --
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 272
diff changeset
8
272
c0769fb9af64 Add new logger for tests to use
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 local format = string.format;
c0769fb9af64 Add new logger for tests to use
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 local print = print;
c0769fb9af64 Add new logger for tests to use
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 local debug = debug;
c0769fb9af64 Add new logger for tests to use
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 local tostring = tostring;
c0769fb9af64 Add new logger for tests to use
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13
c0769fb9af64 Add new logger for tests to use
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 local getstyle, getstring = require "util.termcolours".getstyle, require "util.termcolours".getstring;
c0769fb9af64 Add new logger for tests to use
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 local do_pretty_printing = not os.getenv("WINDIR");
c0769fb9af64 Add new logger for tests to use
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16
c0769fb9af64 Add new logger for tests to use
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 module "logger"
c0769fb9af64 Add new logger for tests to use
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18
c0769fb9af64 Add new logger for tests to use
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 local logstyles = {};
c0769fb9af64 Add new logger for tests to use
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20
c0769fb9af64 Add new logger for tests to use
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 --TODO: This should be done in config, but we don't have proper config yet
c0769fb9af64 Add new logger for tests to use
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 if do_pretty_printing then
c0769fb9af64 Add new logger for tests to use
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 logstyles["info"] = getstyle("bold");
c0769fb9af64 Add new logger for tests to use
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 logstyles["warn"] = getstyle("bold", "yellow");
c0769fb9af64 Add new logger for tests to use
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 logstyles["error"] = getstyle("bold", "red");
c0769fb9af64 Add new logger for tests to use
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 end
c0769fb9af64 Add new logger for tests to use
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27
c0769fb9af64 Add new logger for tests to use
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 function init(name)
c0769fb9af64 Add new logger for tests to use
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 --name = nil; -- While this line is not commented, will automatically fill in file/line number info
c0769fb9af64 Add new logger for tests to use
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 return function (level, message, ...)
c0769fb9af64 Add new logger for tests to use
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 if level == "debug" or level == "info" then return; end
c0769fb9af64 Add new logger for tests to use
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 if not name then
c0769fb9af64 Add new logger for tests to use
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 local inf = debug.getinfo(3, 'Snl');
c0769fb9af64 Add new logger for tests to use
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 level = level .. ","..tostring(inf.short_src):match("[^/]*$")..":"..inf.currentline;
c0769fb9af64 Add new logger for tests to use
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 end
c0769fb9af64 Add new logger for tests to use
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 if ... then
c0769fb9af64 Add new logger for tests to use
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 print(name, getstring(logstyles[level], level), format(message, ...));
c0769fb9af64 Add new logger for tests to use
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 else
c0769fb9af64 Add new logger for tests to use
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 print(name, getstring(logstyles[level], level), message);
c0769fb9af64 Add new logger for tests to use
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 end
c0769fb9af64 Add new logger for tests to use
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 end
c0769fb9af64 Add new logger for tests to use
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 end
c0769fb9af64 Add new logger for tests to use
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43
c0769fb9af64 Add new logger for tests to use
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 return _M;