Software /
code /
prosody
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 |
rev | line source |
---|---|
587 | 1 |
2 local pposix = assert(require "util.pposix"); | |
3 | |
4 local config_get = require "core.configmanager".get; | |
5 local logger_set = require "util.logger".setwriter; | |
6 | |
7 module.host = "*"; -- we're a global module | |
8 | |
9 if not config_get("*", "core", "no_daemonize") then | |
10 local function daemonize_server() | |
11 local logwriter; | |
12 | |
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 | 25 local logfile = io.open(logfilename, "a+"); |
26 if logfile then | |
27 local write, format, flush = logfile.write, string.format, logfile.flush; | |
28 logwriter = function (name, level, message, ...) | |
29 if ... then | |
30 write(logfile, name, "\t", level, "\t", format(message, ...), "\n"); | |
31 else | |
32 write(logfile, name, "\t" , level, "\t", message, "\n"); | |
33 end | |
34 flush(logfile); | |
35 end; | |
36 end | |
37 else | |
38 log("debug", "No logging specified, will continue with default"); | |
39 end | |
40 | |
41 local ok, ret = pposix.daemonize(); | |
42 if not ok then | |
43 log("error", "Failed to daemonize: %s", ret); | |
44 elseif ret and ret > 0 then | |
45 log("info", "Daemonized to pid %d", ret); | |
46 os.exit(0); | |
47 else | |
48 if logwriter then | |
49 local ok, ret = logger_set(logwriter); | |
50 if not ok then | |
51 log("error", "Couldn't set new log output: %s", ret); | |
52 end | |
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 | 55 end |
56 end | |
57 module:add_event_hook("server-starting", daemonize_server); | |
58 end |