comparison mod_debug_traceback/mod_debug_traceback.lua @ 4214:5841d54cb6c6

mod_debug_traceback: Descendent of mod_traceback with some additional features
author Matthew Wild <mwild1@gmail.com>
date Fri, 16 Oct 2020 14:23:10 +0100
parents
children 983565f42063
comparison
equal deleted inserted replaced
4213:93a980ac1816 4214:5841d54cb6c6
1 module:set_global();
2
3 local traceback = require "util.debug".traceback;
4 local pposix = require "util.pposix";
5 local os_date = os.date;
6 local render_filename = require "util.interpolation".new("%b{}", function (s) return s; end, {
7 yyyymmdd = function (t)
8 return os_date("%Y%m%d", t);
9 end;
10 hhmmss = function (t)
11 return os_date("%H%M%S", t);
12 end;
13 });
14
15 local count = 0;
16
17 local function get_filename(filename_template)
18 filename_template = filename_template;
19 return render_filename(filename_template, {
20 paths = prosody.paths;
21 pid = pposix.getpid();
22 count = count;
23 time = os.time();
24 });
25 end
26
27 local default_filename_template = "{paths.data}/traceback-{pid}-{count}.log";
28 local filename_template = module:get_option_string("debug_traceback_filename", default_filename_template);
29 local signal_name = module:get_option_string("debug_traceback_signal", "SIGUSR1");
30
31 function dump_traceback()
32 module:log("info", "Received %s, writing traceback", signal_name);
33
34 local tb = traceback();
35 module:fire_event("debug_traceback/triggered", { traceback = tb });
36
37 local f, err = io.open(get_filename(filename_template), "a+");
38 if not f then
39 module:log("error", "Unable to write traceback: %s", err);
40 return;
41 end
42 f:write("-- Traceback generated at ", os.date("%b %d %H:%M:%S"), " --\n");
43 f:write(traceback(), "\n");
44 f:write("-- End of traceback --\n");
45 f:close();
46 count = count + 1;
47 end
48
49 local mod_posix = module:depends("posix");
50 if mod_posix.features and mod_posix.features.signal_events then
51 module:hook("signal/"..signal_name, dump_traceback);
52 else
53 require"util.signal".signal(signal_name, dump_traceback);
54 end