Mercurial > prosody-modules
annotate mod_debug_traceback/mod_debug_traceback.lua @ 5963:c61a82f80e57 default tip
mod_pubsub_serverinfo: Reference workaround for issue #1841
author | Guus der Kinderen <guus.der.kinderen@gmail.com> |
---|---|
date | Wed, 11 Sep 2024 14:02:39 +0200 |
parents | 0358fcf14cd6 |
children |
rev | line source |
---|---|
4214
5841d54cb6c6
mod_debug_traceback: Descendent of mod_traceback with some additional features
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
1 module:set_global(); |
5841d54cb6c6
mod_debug_traceback: Descendent of mod_traceback with some additional features
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
2 |
5841d54cb6c6
mod_debug_traceback: Descendent of mod_traceback with some additional features
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
3 local traceback = require "util.debug".traceback; |
5841d54cb6c6
mod_debug_traceback: Descendent of mod_traceback with some additional features
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
4 local pposix = require "util.pposix"; |
5841d54cb6c6
mod_debug_traceback: Descendent of mod_traceback with some additional features
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
5 local os_date = os.date; |
5841d54cb6c6
mod_debug_traceback: Descendent of mod_traceback with some additional features
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
6 local render_filename = require "util.interpolation".new("%b{}", function (s) return s; end, { |
5841d54cb6c6
mod_debug_traceback: Descendent of mod_traceback with some additional features
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
7 yyyymmdd = function (t) |
5841d54cb6c6
mod_debug_traceback: Descendent of mod_traceback with some additional features
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
8 return os_date("%Y%m%d", t); |
5841d54cb6c6
mod_debug_traceback: Descendent of mod_traceback with some additional features
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
9 end; |
5841d54cb6c6
mod_debug_traceback: Descendent of mod_traceback with some additional features
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
10 hhmmss = function (t) |
5841d54cb6c6
mod_debug_traceback: Descendent of mod_traceback with some additional features
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
11 return os_date("%H%M%S", t); |
5841d54cb6c6
mod_debug_traceback: Descendent of mod_traceback with some additional features
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
12 end; |
5841d54cb6c6
mod_debug_traceback: Descendent of mod_traceback with some additional features
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
13 }); |
5841d54cb6c6
mod_debug_traceback: Descendent of mod_traceback with some additional features
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
14 |
5841d54cb6c6
mod_debug_traceback: Descendent of mod_traceback with some additional features
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
15 local count = 0; |
5841d54cb6c6
mod_debug_traceback: Descendent of mod_traceback with some additional features
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
16 |
5841d54cb6c6
mod_debug_traceback: Descendent of mod_traceback with some additional features
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
17 local function get_filename(filename_template) |
5841d54cb6c6
mod_debug_traceback: Descendent of mod_traceback with some additional features
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
18 filename_template = filename_template; |
5841d54cb6c6
mod_debug_traceback: Descendent of mod_traceback with some additional features
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
19 return render_filename(filename_template, { |
5841d54cb6c6
mod_debug_traceback: Descendent of mod_traceback with some additional features
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
20 paths = prosody.paths; |
5841d54cb6c6
mod_debug_traceback: Descendent of mod_traceback with some additional features
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
21 pid = pposix.getpid(); |
5841d54cb6c6
mod_debug_traceback: Descendent of mod_traceback with some additional features
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
22 count = count; |
5841d54cb6c6
mod_debug_traceback: Descendent of mod_traceback with some additional features
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
23 time = os.time(); |
5841d54cb6c6
mod_debug_traceback: Descendent of mod_traceback with some additional features
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
24 }); |
5841d54cb6c6
mod_debug_traceback: Descendent of mod_traceback with some additional features
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
25 end |
5841d54cb6c6
mod_debug_traceback: Descendent of mod_traceback with some additional features
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
26 |
5841d54cb6c6
mod_debug_traceback: Descendent of mod_traceback with some additional features
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
27 local default_filename_template = "{paths.data}/traceback-{pid}-{count}.log"; |
5841d54cb6c6
mod_debug_traceback: Descendent of mod_traceback with some additional features
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
28 local filename_template = module:get_option_string("debug_traceback_filename", default_filename_template); |
5841d54cb6c6
mod_debug_traceback: Descendent of mod_traceback with some additional features
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
29 local signal_name = module:get_option_string("debug_traceback_signal", "SIGUSR1"); |
5841d54cb6c6
mod_debug_traceback: Descendent of mod_traceback with some additional features
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
30 |
5841d54cb6c6
mod_debug_traceback: Descendent of mod_traceback with some additional features
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
31 function dump_traceback() |
5841d54cb6c6
mod_debug_traceback: Descendent of mod_traceback with some additional features
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
32 module:log("info", "Received %s, writing traceback", signal_name); |
5841d54cb6c6
mod_debug_traceback: Descendent of mod_traceback with some additional features
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
33 |
5841d54cb6c6
mod_debug_traceback: Descendent of mod_traceback with some additional features
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
34 local tb = traceback(); |
5841d54cb6c6
mod_debug_traceback: Descendent of mod_traceback with some additional features
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
35 module:fire_event("debug_traceback/triggered", { traceback = tb }); |
5841d54cb6c6
mod_debug_traceback: Descendent of mod_traceback with some additional features
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
36 |
5841d54cb6c6
mod_debug_traceback: Descendent of mod_traceback with some additional features
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
37 local f, err = io.open(get_filename(filename_template), "a+"); |
5841d54cb6c6
mod_debug_traceback: Descendent of mod_traceback with some additional features
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
38 if not f then |
5841d54cb6c6
mod_debug_traceback: Descendent of mod_traceback with some additional features
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
39 module:log("error", "Unable to write traceback: %s", err); |
5841d54cb6c6
mod_debug_traceback: Descendent of mod_traceback with some additional features
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
40 return; |
5841d54cb6c6
mod_debug_traceback: Descendent of mod_traceback with some additional features
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
41 end |
5841d54cb6c6
mod_debug_traceback: Descendent of mod_traceback with some additional features
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
42 f:write("-- Traceback generated at ", os.date("%b %d %H:%M:%S"), " --\n"); |
4217
983565f42063
mod_debug_traceback: Fix to use the cached traceback
Matthew Wild <mwild1@gmail.com>
parents:
4214
diff
changeset
|
43 f:write(tb, "\n"); |
4214
5841d54cb6c6
mod_debug_traceback: Descendent of mod_traceback with some additional features
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
44 f:write("-- End of traceback --\n"); |
5841d54cb6c6
mod_debug_traceback: Descendent of mod_traceback with some additional features
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
45 f:close(); |
5841d54cb6c6
mod_debug_traceback: Descendent of mod_traceback with some additional features
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
46 count = count + 1; |
5841d54cb6c6
mod_debug_traceback: Descendent of mod_traceback with some additional features
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
47 end |
5841d54cb6c6
mod_debug_traceback: Descendent of mod_traceback with some additional features
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
48 |
5850
0358fcf14cd6
mod_debug_traceback: Remove direct POSIX signal handling, require Prosody 0.12+
Kim Alvefur <zash@zash.se>
parents:
4222
diff
changeset
|
49 module:hook("signal/"..signal_name, dump_traceback); |