Mercurial > prosody-modules
comparison mod_log_ringbuffer/mod_log_ringbuffer.lua @ 4225:d6fb9f7afaa5
mod_log_ringbuffer: Discard old data when buffer is full
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Tue, 20 Oct 2020 15:34:29 +0100 |
parents | b3dd77f2d0d7 |
children | df2ccb42a241 |
comparison
equal
deleted
inserted
replaced
4224:816c2fa1ca84 | 4225:d6fb9f7afaa5 |
---|---|
55 time = os.time(); | 55 time = os.time(); |
56 }); | 56 }); |
57 end | 57 end |
58 | 58 |
59 local function ringbuffer_log_sink_maker(sink_config) | 59 local function ringbuffer_log_sink_maker(sink_config) |
60 local buffer = rb.new(sink_config.size or 100*1024); | 60 local buffer_size = sink_config.size or 100*1024; |
61 local buffer = rb.new(buffer_size); | |
61 | 62 |
62 local timestamps = sink_config.timestamps; | 63 local timestamps = sink_config.timestamps; |
63 | 64 |
64 if timestamps == true or timestamps == nil then | 65 if timestamps == true or timestamps == nil then |
65 timestamps = default_timestamp; -- Default format | 66 timestamps = default_timestamp; -- Default format |
76 elseif sink_config.event then | 77 elseif sink_config.event then |
77 module:hook_global(sink_config.event, dump); | 78 module:hook_global(sink_config.event, dump); |
78 end | 79 end |
79 | 80 |
80 return function (name, level, message, ...) | 81 return function (name, level, message, ...) |
81 buffer:write(format("%s%s\t%s\t%s\n", timestamps and os_date(timestamps) or "", name, level, format(message, ...))); | 82 local line = format("%s%s\t%s\t%s\n", timestamps and os_date(timestamps) or "", name, level, format(message, ...)); |
83 if not buffer:write(line) then | |
84 if #line > buffer_size then | |
85 buffer:discard(buffer_size); | |
86 buffer:write(line:sub(-buffer_size)); | |
87 else | |
88 buffer:discard(#line); | |
89 buffer:write(line); | |
90 end | |
91 end | |
82 end; | 92 end; |
83 end | 93 end |
84 | 94 |
85 loggingmanager.register_sink_type("ringbuffer", ringbuffer_log_sink_maker); | 95 loggingmanager.register_sink_type("ringbuffer", ringbuffer_log_sink_maker); |