Annotate

plugins/mod_posix.lua @ 722:63456c9d0522

mod_posix: Support for logging to syslog (log = 'syslog' in config)
author Matthew Wild <mwild1@gmail.com>
date Thu, 15 Jan 2009 20:06:41 +0000
parent 594:6d9b2da9888f
child 723:c1e7d280c174
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
587
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 local pposix = assert(require "util.pposix");
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 local config_get = require "core.configmanager".get;
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 local logger_set = require "util.logger".setwriter;
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 module.host = "*"; -- we're a global module
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 if not config_get("*", "core", "no_daemonize") then
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 local function daemonize_server()
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 local logwriter;
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 local logfilename = config_get("*", "core", "log");
722
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 594
diff changeset
14 if logfilename == "syslog" then
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 594
diff changeset
15 pposix.syslog_open("prosody");
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 594
diff changeset
16 local syslog, format = pposix.syslog_log, string.format;
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 594
diff changeset
17 logwriter = function (name, level, message, ...)
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 594
diff changeset
18 if ... then
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 594
diff changeset
19 syslog(level, format(message, ...));
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 594
diff changeset
20 else
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 594
diff changeset
21 syslog(level, message);
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 594
diff changeset
22 end
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 594
diff changeset
23 end;
63456c9d0522 mod_posix: Support for logging to syslog (log = 'syslog' in config)
Matthew Wild <mwild1@gmail.com>
parents: 594
diff changeset
24 elseif logfilename then
587
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 local logfile = io.open(logfilename, "a+");
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 if logfile then
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 local write, format, flush = logfile.write, string.format, logfile.flush;
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 logwriter = function (name, level, message, ...)
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 if ... then
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 write(logfile, name, "\t", level, "\t", format(message, ...), "\n");
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 else
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 write(logfile, name, "\t" , level, "\t", message, "\n");
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 end
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 flush(logfile);
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 end;
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 end
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 else
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 log("debug", "No logging specified, will continue with default");
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 end
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 local ok, ret = pposix.daemonize();
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 if not ok then
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 log("error", "Failed to daemonize: %s", ret);
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 elseif ret and ret > 0 then
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 log("info", "Daemonized to pid %d", ret);
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 os.exit(0);
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 else
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 if logwriter then
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 local ok, ret = logger_set(logwriter);
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 if not ok then
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 log("error", "Couldn't set new log output: %s", ret);
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 end
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 end
594
6d9b2da9888f Small logging adjustment for mod_posix
Matthew Wild <mwild1@gmail.com>
parents: 587
diff changeset
54 log("info", "Successfully daemonized");
587
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 end
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 end
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 module:add_event_hook("server-starting", daemonize_server);
43f509a1519a Add mod_posix, fixes #5
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 end