Mercurial > prosody-modules
comparison mod_log_ringbuffer/mod_log_ringbuffer.lua @ 5853:97c9b76867ca
mod_log_ringbuffer: Detach event handlers on logging reload (thanks Menel)
Otherwise the global event handlers accumulate, one added each time
logging is reoladed, and each invocation of the signal or event triggers
one dump of each created ringbuffer.
| author | Kim Alvefur <zash@zash.se> |
|---|---|
| date | Sun, 03 Mar 2024 11:23:40 +0100 |
| parents | 133b23758cf6 |
| children | 8a47791338fc |
comparison
equal
deleted
inserted
replaced
| 5852:133b23758cf6 | 5853:97c9b76867ca |
|---|---|
| 88 end | 88 end |
| 89 end | 89 end |
| 90 return write, dump; | 90 return write, dump; |
| 91 end | 91 end |
| 92 | 92 |
| 93 local event_hooks = {}; | |
| 94 | |
| 93 local function ringbuffer_log_sink_maker(sink_config) | 95 local function ringbuffer_log_sink_maker(sink_config) |
| 94 local write, dump = new_buffer(sink_config); | 96 local write, dump = new_buffer(sink_config); |
| 95 | 97 |
| 96 local timestamps = sink_config.timestamps; | 98 local timestamps = sink_config.timestamps; |
| 97 | 99 |
| 105 dump_buffer(dump, sink_config.filename or get_filename(sink_config.filename_template)); | 107 dump_buffer(dump, sink_config.filename or get_filename(sink_config.filename_template)); |
| 106 end | 108 end |
| 107 | 109 |
| 108 if sink_config.signal then | 110 if sink_config.signal then |
| 109 module:hook_global("signal/"..sink_config.signal, handler); | 111 module:hook_global("signal/"..sink_config.signal, handler); |
| 112 event_hooks[handler] = "signal/"..sink_config.signal; | |
| 110 elseif sink_config.event then | 113 elseif sink_config.event then |
| 111 module:hook_global(sink_config.event, handler); | 114 module:hook_global(sink_config.event, handler); |
| 115 event_hooks[handler] = sink_config.event; | |
| 112 end | 116 end |
| 113 | 117 |
| 114 return function (name, level, message, ...) | 118 return function (name, level, message, ...) |
| 115 local line = format("%s%s\t%s\t%s\n", timestamps and os_date(timestamps) or "", name, level, format(message, ...)); | 119 local line = format("%s%s\t%s\t%s\n", timestamps and os_date(timestamps) or "", name, level, format(message, ...)); |
| 116 write(line); | 120 write(line); |
| 117 end; | 121 end; |
| 118 end | 122 end |
| 119 | 123 |
| 124 module:hook_global("reopen-log-files", function() | |
| 125 for handler, event_name in pairs(event_hooks) do | |
| 126 module:unhook_object_event(prosody.events, event_name, handler); | |
| 127 event_hooks[handler] = nil; | |
| 128 end | |
| 129 end, 1); | |
| 130 | |
| 120 loggingmanager.register_sink_type("ringbuffer", ringbuffer_log_sink_maker); | 131 loggingmanager.register_sink_type("ringbuffer", ringbuffer_log_sink_maker); |
