Annotate

mod_log_ringbuffer/mod_log_ringbuffer.lua @ 6301:fa45ae704b79

mod_cloud_notify: Update Readme diff --git a/mod_cloud_notify/README.md b/mod_cloud_notify/README.md --- a/mod_cloud_notify/README.md +++ b/mod_cloud_notify/README.md @@ -1,109 +1,106 @@ ---- -labels: -- 'Stage-Beta' -summary: 'XEP-0357: Cloud push notifications' ---- +# Introduction -Introduction -============ +This module enables support for sending "push notifications" to clients +that need it, typically those running on certain mobile devices. -This module enables support for sending "push notifications" to clients that -need it, typically those running on certain mobile devices. +As well as this module, your client must support push notifications (the +apps that need it generally do, of course) and the app developer's push +gateway must be reachable from your Prosody server (this happens over a +normal XMPP server-to-server 's2s' connection). -As well as this module, your client must support push notifications (the apps -that need it generally do, of course) and the app developer's push gateway -must be reachable from your Prosody server (this happens over a normal XMPP -server-to-server 's2s' connection). - -Details -======= +# Details Some platforms, notably Apple's iOS and many versions of Android, impose -limits that prevent applications from running or accessing the network in the -background. This makes it difficult or impossible for an XMPP application to -remain reliably connected to a server to receive messages. - -In order for messaging and other apps to receive notifications, the OS vendors -run proprietary servers that their OS maintains a permanent connection to in -the background. Then they provide APIs to application developers that allow -sending notifications to specific devices via those servers. +limits that prevent applications from running or accessing the network +in the background. This makes it difficult or impossible for an XMPP +application to remain reliably connected to a server to receive +messages. -When you connect to your server with an app that requires push notifications, -it will use this module to set up a "push registration". When you receive -a message but your device is not connected to the server, this module will -generate a notification and send it to the push gateway operated by your -application's developers). Their gateway will then connect to your device's -OS vendor and ask them to forward the notification to your device. When your -device receives the notification, it will display it or wake up the app so it -can connect to XMPP and receive any pending messages. +In order for messaging and other apps to receive notifications, the OS +vendors run proprietary servers that their OS maintains a permanent +connection to in the background. Then they provide APIs to application +developers that allow sending notifications to specific devices via +those servers. -This protocol is described for developers in [XEP-0357: Push Notifications]. +When you connect to your server with an app that requires push +notifications, it will use this module to set up a "push registration". +When you receive a message but your device is not connected to the +server, this module will generate a notification and send it to the push +gateway operated by your application's developers). Their gateway will +then connect to your device's OS vendor and ask them to forward the +notification to your device. When your device receives the notification, +it will display it or wake up the app so it can connect to XMPP and +receive any pending messages. -For this module to work reliably, you must have [mod_smacks], [mod_mam] and -[mod_carbons] also enabled on your server. +This protocol is described for developers in \[XEP-0357: Push +Notifications\]. + +For this module to work reliably, you must have \[mod_smacks\], +\[mod_mam\] and \[mod_carbons\] also enabled on your server. -Some clients, notably Siskin and Snikket iOS need some additional extensions -that are not currently defined in a standard XEP. To support these clients, -see [mod_cloud_notify_extensions]. +Some clients, notably Siskin and Snikket iOS need some additional +extensions that are not currently defined in a standard XEP. To support +these clients, see \[mod_cloud_notify_extensions\]. -Configuration -============= +# Configuration - Option Default Description - ------------------------------------ ----------------- ------------------------------------------------------------------------------------------------------------------- - `push_notification_important_body` `New Message!` The body text to use when the stanza is important (see above), no message body is sent if this is empty - `push_max_errors` `16` How much persistent push errors are tolerated before notifications for the identifier in question are disabled - `push_max_devices` `5` The number of allowed devices per user (the oldest devices are automatically removed if this threshold is reached) - `push_max_hibernation_timeout` `259200` (72h) Number of seconds to extend the smacks timeout if no push was triggered yet (default: 72 hours) - `push_notification_with_body` (\*) `false` Whether or not to send the real message body to remote pubsub node. Without end-to-end encryption, enabling this may expose your message contents to your client developers and OS vendor. Not recommended. - `push_notification_with_sender` (\*) `false` Whether or not to send the real message sender to remote pubsub node. Enabling this may expose your contacts to your client developers and OS vendor. Not recommended. + Option Default Description + -------------------------------------- ---------------- ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + `push_notification_important_body` `New Message!` The body text to use when the stanza is important (see above), no message body is sent if this is empty + `push_max_errors` `16` How much persistent push errors are tolerated before notifications for the identifier in question are disabled + `push_max_devices` `5` The number of allowed devices per user (the oldest devices are automatically removed if this threshold is reached) + `push_max_hibernation_timeout` `259200` (72h) Number of seconds to extend the smacks timeout if no push was triggered yet (default: 72 hours) + `push_notification_with_body` (\*) `false` Whether or not to send the real message body to remote pubsub node. Without end-to-end encryption, enabling this may expose your message contents to your client developers and OS vendor. Not recommended. + `push_notification_with_sender` (\*) `false` Whether or not to send the real message sender to remote pubsub node. Enabling this may expose your contacts to your client developers and OS vendor. Not recommended. -(\*) There are privacy implications for enabling these options. +(\*) There are privacy implications for enabling these options.[^1] -Internal design notes -===================== +# Internal design notes -App servers are notified about offline messages, messages stored by [mod_mam] -or messages waiting in the smacks queue. -The business rules outlined [here](//mail.jabber.org/pipermail/standards/2016-February/030925.html) are all honored[^2]. +App servers are notified about offline messages, messages stored by +\[mod_mam\] or messages waiting in the smacks queue. The business rules +outlined +[here](//mail.jabber.org/pipermail/standards/2016-February/030925.html) +are all honored[^2]. -To cooperate with [mod_smacks] this module consumes some events: -`smacks-ack-delayed`, `smacks-hibernation-start` and `smacks-hibernation-end`. -These events allow this module to send out notifications for messages received -while the session is hibernated by [mod_smacks] or even when smacks -acknowledgements for messages are delayed by a certain amount of seconds -configurable with the [mod_smacks] setting `smacks_max_ack_delay`. +To cooperate with \[mod_smacks\] this module consumes some events: +`smacks-ack-delayed`, `smacks-hibernation-start` and +`smacks-hibernation-end`. These events allow this module to send out +notifications for messages received while the session is hibernated by +\[mod_smacks\] or even when smacks acknowledgements for messages are +delayed by a certain amount of seconds configurable with the +\[mod_smacks\] setting `smacks_max_ack_delay`. -The `smacks_max_ack_delay` setting allows to send out notifications to clients -which aren't already in smacks hibernation state (because the read timeout or -connection close didn't already happen) but also aren't responding to acknowledgement -request in a timely manner. This setting thus allows conversations to be smoother -under such circumstances. +The `smacks_max_ack_delay` setting allows to send out notifications to +clients which aren't already in smacks hibernation state (because the +read timeout or connection close didn't already happen) but also aren't +responding to acknowledgement request in a timely manner. This setting +thus allows conversations to be smoother under such circumstances. -The new event `cloud-notify-ping` can be used by any module to send out a cloud -notification to either all registered endpoints for the given user or only the endpoints -given in the event data. +The new event `cloud-notify-ping` can be used by any module to send out +a cloud notification to either all registered endpoints for the given +user or only the endpoints given in the event data. -The config setting `push_notification_important_body` can be used to specify an alternative -body text to send to the remote pubsub node if the stanza is encrypted or has a body. -This way the real contents of the message aren't revealed to the push appserver but it -can still see that the push is important. -This is used by Chatsecure on iOS to send out high priority pushes in those cases for example. +The config setting `push_notification_important_body` can be used to +specify an alternative body text to send to the remote pubsub node if +the stanza is encrypted or has a body. This way the real contents of the +message aren't revealed to the push appserver but it can still see that +the push is important. This is used by Chatsecure on iOS to send out +high priority pushes in those cases for example. -Compatibility -============= - -**Note:** This module should be used with Lua 5.2 and higher. Using it with -Lua 5.1 may cause push notifications to not be sent to some clients. +# Compatibility ------- ----------------------------------------------------------------------------- - trunk Works - 0.12 Works - 0.11 Works - 0.10 Works - 0.9 Support dropped, use last supported version [675726ab06d3](//hg.prosody.im/prosody-modules/raw-file/675726ab06d3/mod_cloud_notify/mod_cloud_notify.lua) ------- ----------------------------------------------------------------------------- +**Note:** This module should be used with Lua 5.2 and higher. Using it +with Lua 5.1 may cause push notifications to not be sent to some +clients. + ------- ----------------------------------------------------------------- + trunk Works as of 25-06-13 + 13 Works + 0.12 Works + ------- ----------------------------------------------------------------- -[^1]: The service which is expected to forward notifications to something like Google Cloud Messaging or Apple Notification Service -[^2]: [business_rules.markdown](//hg.prosody.im/prosody-modules/file/tip/mod_cloud_notify/business_rules.markdown) +[^1]: The service which is expected to forward notifications to + something like Google Cloud Messaging or Apple Notification Service + +[^2]: [business_rules.md](//hg.prosody.im/prosody-modules/file/tip/mod_cloud_notify/business_rules.md)
author Menel <menel@snikket.de>
date Fri, 13 Jun 2025 10:36:52 +0200
parent 5877:97c9b76867ca
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4205
481c4d75e77d mod_log_ringbuffer: New module to send logs to an in-memory ringbuffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 module:set_global();
481c4d75e77d mod_log_ringbuffer: New module to send logs to an in-memory ringbuffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2
481c4d75e77d mod_log_ringbuffer: New module to send logs to an in-memory ringbuffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 local loggingmanager = require "core.loggingmanager";
481c4d75e77d mod_log_ringbuffer: New module to send logs to an in-memory ringbuffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 local format = require "util.format".format;
481c4d75e77d mod_log_ringbuffer: New module to send logs to an in-memory ringbuffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 local pposix = require "util.pposix";
481c4d75e77d mod_log_ringbuffer: New module to send logs to an in-memory ringbuffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 local rb = require "util.ringbuffer";
4226
df2ccb42a241 mod_log_ringbuffer: Add 'lines' option (actually an alternative ringbuffer implementation)
Matthew Wild <mwild1@gmail.com>
parents: 4225
diff changeset
7 local queue = require "util.queue";
4205
481c4d75e77d mod_log_ringbuffer: New module to send logs to an in-memory ringbuffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8
481c4d75e77d mod_log_ringbuffer: New module to send logs to an in-memory ringbuffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 local default_timestamp = "%b %d %H:%M:%S ";
481c4d75e77d mod_log_ringbuffer: New module to send logs to an in-memory ringbuffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 local max_chunk_size = module:get_option_number("log_ringbuffer_chunk_size", 16384);
481c4d75e77d mod_log_ringbuffer: New module to send logs to an in-memory ringbuffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11
481c4d75e77d mod_log_ringbuffer: New module to send logs to an in-memory ringbuffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 local os_date = os.date;
481c4d75e77d mod_log_ringbuffer: New module to send logs to an in-memory ringbuffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13
4215
86f8ece24029 mod_log_ringbuffer: Update default filename to include data path
Matthew Wild <mwild1@gmail.com>
parents: 4205
diff changeset
14 local default_filename_template = "{paths.data}/ringbuffer-logs-{pid}-{count}.log";
4205
481c4d75e77d mod_log_ringbuffer: New module to send logs to an in-memory ringbuffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 local render_filename = require "util.interpolation".new("%b{}", function (s) return s; end, {
481c4d75e77d mod_log_ringbuffer: New module to send logs to an in-memory ringbuffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 yyyymmdd = function (t)
481c4d75e77d mod_log_ringbuffer: New module to send logs to an in-memory ringbuffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 return os_date("%Y%m%d", t);
481c4d75e77d mod_log_ringbuffer: New module to send logs to an in-memory ringbuffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 end;
481c4d75e77d mod_log_ringbuffer: New module to send logs to an in-memory ringbuffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 hhmmss = function (t)
481c4d75e77d mod_log_ringbuffer: New module to send logs to an in-memory ringbuffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 return os_date("%H%M%S", t);
481c4d75e77d mod_log_ringbuffer: New module to send logs to an in-memory ringbuffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 end;
481c4d75e77d mod_log_ringbuffer: New module to send logs to an in-memory ringbuffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 });
481c4d75e77d mod_log_ringbuffer: New module to send logs to an in-memory ringbuffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23
481c4d75e77d mod_log_ringbuffer: New module to send logs to an in-memory ringbuffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 local dump_count = 0;
481c4d75e77d mod_log_ringbuffer: New module to send logs to an in-memory ringbuffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25
4226
df2ccb42a241 mod_log_ringbuffer: Add 'lines' option (actually an alternative ringbuffer implementation)
Matthew Wild <mwild1@gmail.com>
parents: 4225
diff changeset
26 local function dump_buffer(dump, filename)
4205
481c4d75e77d mod_log_ringbuffer: New module to send logs to an in-memory ringbuffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 dump_count = dump_count + 1;
481c4d75e77d mod_log_ringbuffer: New module to send logs to an in-memory ringbuffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 local f, err = io.open(filename, "a+");
481c4d75e77d mod_log_ringbuffer: New module to send logs to an in-memory ringbuffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 if not f then
481c4d75e77d mod_log_ringbuffer: New module to send logs to an in-memory ringbuffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 module:log("error", "Unable to open output file: %s", err);
481c4d75e77d mod_log_ringbuffer: New module to send logs to an in-memory ringbuffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 return;
481c4d75e77d mod_log_ringbuffer: New module to send logs to an in-memory ringbuffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 end
4226
df2ccb42a241 mod_log_ringbuffer: Add 'lines' option (actually an alternative ringbuffer implementation)
Matthew Wild <mwild1@gmail.com>
parents: 4225
diff changeset
33 f:write(("-- Dumping log buffer at %s --\n"):format(os_date(default_timestamp)));
df2ccb42a241 mod_log_ringbuffer: Add 'lines' option (actually an alternative ringbuffer implementation)
Matthew Wild <mwild1@gmail.com>
parents: 4225
diff changeset
34 dump(f);
4205
481c4d75e77d mod_log_ringbuffer: New module to send logs to an in-memory ringbuffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 f:write("-- End of dump --\n\n");
481c4d75e77d mod_log_ringbuffer: New module to send logs to an in-memory ringbuffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 f:close();
481c4d75e77d mod_log_ringbuffer: New module to send logs to an in-memory ringbuffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 end
481c4d75e77d mod_log_ringbuffer: New module to send logs to an in-memory ringbuffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38
481c4d75e77d mod_log_ringbuffer: New module to send logs to an in-memory ringbuffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 local function get_filename(filename_template)
481c4d75e77d mod_log_ringbuffer: New module to send logs to an in-memory ringbuffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 filename_template = filename_template or default_filename_template;
481c4d75e77d mod_log_ringbuffer: New module to send logs to an in-memory ringbuffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 return render_filename(filename_template, {
481c4d75e77d mod_log_ringbuffer: New module to send logs to an in-memory ringbuffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 paths = prosody.paths;
481c4d75e77d mod_log_ringbuffer: New module to send logs to an in-memory ringbuffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 pid = pposix.getpid();
481c4d75e77d mod_log_ringbuffer: New module to send logs to an in-memory ringbuffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 count = dump_count;
481c4d75e77d mod_log_ringbuffer: New module to send logs to an in-memory ringbuffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 time = os.time();
481c4d75e77d mod_log_ringbuffer: New module to send logs to an in-memory ringbuffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 });
481c4d75e77d mod_log_ringbuffer: New module to send logs to an in-memory ringbuffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 end
481c4d75e77d mod_log_ringbuffer: New module to send logs to an in-memory ringbuffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48
4226
df2ccb42a241 mod_log_ringbuffer: Add 'lines' option (actually an alternative ringbuffer implementation)
Matthew Wild <mwild1@gmail.com>
parents: 4225
diff changeset
49 local function new_buffer(config)
df2ccb42a241 mod_log_ringbuffer: Add 'lines' option (actually an alternative ringbuffer implementation)
Matthew Wild <mwild1@gmail.com>
parents: 4225
diff changeset
50 local write, dump;
df2ccb42a241 mod_log_ringbuffer: Add 'lines' option (actually an alternative ringbuffer implementation)
Matthew Wild <mwild1@gmail.com>
parents: 4225
diff changeset
51
df2ccb42a241 mod_log_ringbuffer: Add 'lines' option (actually an alternative ringbuffer implementation)
Matthew Wild <mwild1@gmail.com>
parents: 4225
diff changeset
52 if config.lines then
df2ccb42a241 mod_log_ringbuffer: Add 'lines' option (actually an alternative ringbuffer implementation)
Matthew Wild <mwild1@gmail.com>
parents: 4225
diff changeset
53 local buffer = queue.new(config.lines, true);
df2ccb42a241 mod_log_ringbuffer: Add 'lines' option (actually an alternative ringbuffer implementation)
Matthew Wild <mwild1@gmail.com>
parents: 4225
diff changeset
54 function write(line)
df2ccb42a241 mod_log_ringbuffer: Add 'lines' option (actually an alternative ringbuffer implementation)
Matthew Wild <mwild1@gmail.com>
parents: 4225
diff changeset
55 buffer:push(line);
df2ccb42a241 mod_log_ringbuffer: Add 'lines' option (actually an alternative ringbuffer implementation)
Matthew Wild <mwild1@gmail.com>
parents: 4225
diff changeset
56 end
df2ccb42a241 mod_log_ringbuffer: Add 'lines' option (actually an alternative ringbuffer implementation)
Matthew Wild <mwild1@gmail.com>
parents: 4225
diff changeset
57 function dump(f)
df2ccb42a241 mod_log_ringbuffer: Add 'lines' option (actually an alternative ringbuffer implementation)
Matthew Wild <mwild1@gmail.com>
parents: 4225
diff changeset
58 -- COMPAT w/0.11 - update to use :consume()
df2ccb42a241 mod_log_ringbuffer: Add 'lines' option (actually an alternative ringbuffer implementation)
Matthew Wild <mwild1@gmail.com>
parents: 4225
diff changeset
59 for line in buffer.pop, buffer do
df2ccb42a241 mod_log_ringbuffer: Add 'lines' option (actually an alternative ringbuffer implementation)
Matthew Wild <mwild1@gmail.com>
parents: 4225
diff changeset
60 f:write(line);
df2ccb42a241 mod_log_ringbuffer: Add 'lines' option (actually an alternative ringbuffer implementation)
Matthew Wild <mwild1@gmail.com>
parents: 4225
diff changeset
61 end
df2ccb42a241 mod_log_ringbuffer: Add 'lines' option (actually an alternative ringbuffer implementation)
Matthew Wild <mwild1@gmail.com>
parents: 4225
diff changeset
62 end
df2ccb42a241 mod_log_ringbuffer: Add 'lines' option (actually an alternative ringbuffer implementation)
Matthew Wild <mwild1@gmail.com>
parents: 4225
diff changeset
63 else
df2ccb42a241 mod_log_ringbuffer: Add 'lines' option (actually an alternative ringbuffer implementation)
Matthew Wild <mwild1@gmail.com>
parents: 4225
diff changeset
64 local buffer_size = config.size or 100*1024;
df2ccb42a241 mod_log_ringbuffer: Add 'lines' option (actually an alternative ringbuffer implementation)
Matthew Wild <mwild1@gmail.com>
parents: 4225
diff changeset
65 local buffer = rb.new(buffer_size);
df2ccb42a241 mod_log_ringbuffer: Add 'lines' option (actually an alternative ringbuffer implementation)
Matthew Wild <mwild1@gmail.com>
parents: 4225
diff changeset
66 function write(line)
df2ccb42a241 mod_log_ringbuffer: Add 'lines' option (actually an alternative ringbuffer implementation)
Matthew Wild <mwild1@gmail.com>
parents: 4225
diff changeset
67 if not buffer:write(line) then
df2ccb42a241 mod_log_ringbuffer: Add 'lines' option (actually an alternative ringbuffer implementation)
Matthew Wild <mwild1@gmail.com>
parents: 4225
diff changeset
68 if #line > buffer_size then
df2ccb42a241 mod_log_ringbuffer: Add 'lines' option (actually an alternative ringbuffer implementation)
Matthew Wild <mwild1@gmail.com>
parents: 4225
diff changeset
69 buffer:discard(buffer_size);
df2ccb42a241 mod_log_ringbuffer: Add 'lines' option (actually an alternative ringbuffer implementation)
Matthew Wild <mwild1@gmail.com>
parents: 4225
diff changeset
70 buffer:write(line:sub(-buffer_size));
df2ccb42a241 mod_log_ringbuffer: Add 'lines' option (actually an alternative ringbuffer implementation)
Matthew Wild <mwild1@gmail.com>
parents: 4225
diff changeset
71 else
df2ccb42a241 mod_log_ringbuffer: Add 'lines' option (actually an alternative ringbuffer implementation)
Matthew Wild <mwild1@gmail.com>
parents: 4225
diff changeset
72 buffer:discard(#line);
df2ccb42a241 mod_log_ringbuffer: Add 'lines' option (actually an alternative ringbuffer implementation)
Matthew Wild <mwild1@gmail.com>
parents: 4225
diff changeset
73 buffer:write(line);
df2ccb42a241 mod_log_ringbuffer: Add 'lines' option (actually an alternative ringbuffer implementation)
Matthew Wild <mwild1@gmail.com>
parents: 4225
diff changeset
74 end
df2ccb42a241 mod_log_ringbuffer: Add 'lines' option (actually an alternative ringbuffer implementation)
Matthew Wild <mwild1@gmail.com>
parents: 4225
diff changeset
75 end
df2ccb42a241 mod_log_ringbuffer: Add 'lines' option (actually an alternative ringbuffer implementation)
Matthew Wild <mwild1@gmail.com>
parents: 4225
diff changeset
76 end
df2ccb42a241 mod_log_ringbuffer: Add 'lines' option (actually an alternative ringbuffer implementation)
Matthew Wild <mwild1@gmail.com>
parents: 4225
diff changeset
77 function dump(f)
df2ccb42a241 mod_log_ringbuffer: Add 'lines' option (actually an alternative ringbuffer implementation)
Matthew Wild <mwild1@gmail.com>
parents: 4225
diff changeset
78 local bytes_remaining = buffer:length();
df2ccb42a241 mod_log_ringbuffer: Add 'lines' option (actually an alternative ringbuffer implementation)
Matthew Wild <mwild1@gmail.com>
parents: 4225
diff changeset
79 while bytes_remaining > 0 do
df2ccb42a241 mod_log_ringbuffer: Add 'lines' option (actually an alternative ringbuffer implementation)
Matthew Wild <mwild1@gmail.com>
parents: 4225
diff changeset
80 local chunk_size = math.min(bytes_remaining, max_chunk_size);
df2ccb42a241 mod_log_ringbuffer: Add 'lines' option (actually an alternative ringbuffer implementation)
Matthew Wild <mwild1@gmail.com>
parents: 4225
diff changeset
81 local chunk = buffer:read(chunk_size);
df2ccb42a241 mod_log_ringbuffer: Add 'lines' option (actually an alternative ringbuffer implementation)
Matthew Wild <mwild1@gmail.com>
parents: 4225
diff changeset
82 if not chunk then
df2ccb42a241 mod_log_ringbuffer: Add 'lines' option (actually an alternative ringbuffer implementation)
Matthew Wild <mwild1@gmail.com>
parents: 4225
diff changeset
83 return;
df2ccb42a241 mod_log_ringbuffer: Add 'lines' option (actually an alternative ringbuffer implementation)
Matthew Wild <mwild1@gmail.com>
parents: 4225
diff changeset
84 end
df2ccb42a241 mod_log_ringbuffer: Add 'lines' option (actually an alternative ringbuffer implementation)
Matthew Wild <mwild1@gmail.com>
parents: 4225
diff changeset
85 f:write(chunk);
df2ccb42a241 mod_log_ringbuffer: Add 'lines' option (actually an alternative ringbuffer implementation)
Matthew Wild <mwild1@gmail.com>
parents: 4225
diff changeset
86 bytes_remaining = bytes_remaining - chunk_size;
df2ccb42a241 mod_log_ringbuffer: Add 'lines' option (actually an alternative ringbuffer implementation)
Matthew Wild <mwild1@gmail.com>
parents: 4225
diff changeset
87 end
df2ccb42a241 mod_log_ringbuffer: Add 'lines' option (actually an alternative ringbuffer implementation)
Matthew Wild <mwild1@gmail.com>
parents: 4225
diff changeset
88 end
df2ccb42a241 mod_log_ringbuffer: Add 'lines' option (actually an alternative ringbuffer implementation)
Matthew Wild <mwild1@gmail.com>
parents: 4225
diff changeset
89 end
df2ccb42a241 mod_log_ringbuffer: Add 'lines' option (actually an alternative ringbuffer implementation)
Matthew Wild <mwild1@gmail.com>
parents: 4225
diff changeset
90 return write, dump;
df2ccb42a241 mod_log_ringbuffer: Add 'lines' option (actually an alternative ringbuffer implementation)
Matthew Wild <mwild1@gmail.com>
parents: 4225
diff changeset
91 end
df2ccb42a241 mod_log_ringbuffer: Add 'lines' option (actually an alternative ringbuffer implementation)
Matthew Wild <mwild1@gmail.com>
parents: 4225
diff changeset
92
5877
97c9b76867ca mod_log_ringbuffer: Detach event handlers on logging reload (thanks Menel)
Kim Alvefur <zash@zash.se>
parents: 5876
diff changeset
93 local event_hooks = {};
97c9b76867ca mod_log_ringbuffer: Detach event handlers on logging reload (thanks Menel)
Kim Alvefur <zash@zash.se>
parents: 5876
diff changeset
94
4205
481c4d75e77d mod_log_ringbuffer: New module to send logs to an in-memory ringbuffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
95 local function ringbuffer_log_sink_maker(sink_config)
4226
df2ccb42a241 mod_log_ringbuffer: Add 'lines' option (actually an alternative ringbuffer implementation)
Matthew Wild <mwild1@gmail.com>
parents: 4225
diff changeset
96 local write, dump = new_buffer(sink_config);
4205
481c4d75e77d mod_log_ringbuffer: New module to send logs to an in-memory ringbuffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
97
481c4d75e77d mod_log_ringbuffer: New module to send logs to an in-memory ringbuffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
98 local timestamps = sink_config.timestamps;
481c4d75e77d mod_log_ringbuffer: New module to send logs to an in-memory ringbuffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
99
481c4d75e77d mod_log_ringbuffer: New module to send logs to an in-memory ringbuffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
100 if timestamps == true or timestamps == nil then
481c4d75e77d mod_log_ringbuffer: New module to send logs to an in-memory ringbuffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
101 timestamps = default_timestamp; -- Default format
481c4d75e77d mod_log_ringbuffer: New module to send logs to an in-memory ringbuffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
102 elseif timestamps then
481c4d75e77d mod_log_ringbuffer: New module to send logs to an in-memory ringbuffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
103 timestamps = timestamps .. " ";
481c4d75e77d mod_log_ringbuffer: New module to send logs to an in-memory ringbuffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
104 end
481c4d75e77d mod_log_ringbuffer: New module to send logs to an in-memory ringbuffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
105
4226
df2ccb42a241 mod_log_ringbuffer: Add 'lines' option (actually an alternative ringbuffer implementation)
Matthew Wild <mwild1@gmail.com>
parents: 4225
diff changeset
106 local function handler()
df2ccb42a241 mod_log_ringbuffer: Add 'lines' option (actually an alternative ringbuffer implementation)
Matthew Wild <mwild1@gmail.com>
parents: 4225
diff changeset
107 dump_buffer(dump, sink_config.filename or get_filename(sink_config.filename_template));
4205
481c4d75e77d mod_log_ringbuffer: New module to send logs to an in-memory ringbuffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
108 end
481c4d75e77d mod_log_ringbuffer: New module to send logs to an in-memory ringbuffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
109
481c4d75e77d mod_log_ringbuffer: New module to send logs to an in-memory ringbuffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
110 if sink_config.signal then
5876
133b23758cf6 mod_log_ringbuffer: Hook POSIX signals via event only
Kim Alvefur <zash@zash.se>
parents: 4226
diff changeset
111 module:hook_global("signal/"..sink_config.signal, handler);
5877
97c9b76867ca mod_log_ringbuffer: Detach event handlers on logging reload (thanks Menel)
Kim Alvefur <zash@zash.se>
parents: 5876
diff changeset
112 event_hooks[handler] = "signal/"..sink_config.signal;
4205
481c4d75e77d mod_log_ringbuffer: New module to send logs to an in-memory ringbuffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
113 elseif sink_config.event then
4226
df2ccb42a241 mod_log_ringbuffer: Add 'lines' option (actually an alternative ringbuffer implementation)
Matthew Wild <mwild1@gmail.com>
parents: 4225
diff changeset
114 module:hook_global(sink_config.event, handler);
5877
97c9b76867ca mod_log_ringbuffer: Detach event handlers on logging reload (thanks Menel)
Kim Alvefur <zash@zash.se>
parents: 5876
diff changeset
115 event_hooks[handler] = sink_config.event;
4205
481c4d75e77d mod_log_ringbuffer: New module to send logs to an in-memory ringbuffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
116 end
481c4d75e77d mod_log_ringbuffer: New module to send logs to an in-memory ringbuffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
117
481c4d75e77d mod_log_ringbuffer: New module to send logs to an in-memory ringbuffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
118 return function (name, level, message, ...)
4225
d6fb9f7afaa5 mod_log_ringbuffer: Discard old data when buffer is full
Matthew Wild <mwild1@gmail.com>
parents: 4219
diff changeset
119 local line = format("%s%s\t%s\t%s\n", timestamps and os_date(timestamps) or "", name, level, format(message, ...));
4226
df2ccb42a241 mod_log_ringbuffer: Add 'lines' option (actually an alternative ringbuffer implementation)
Matthew Wild <mwild1@gmail.com>
parents: 4225
diff changeset
120 write(line);
4205
481c4d75e77d mod_log_ringbuffer: New module to send logs to an in-memory ringbuffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
121 end;
481c4d75e77d mod_log_ringbuffer: New module to send logs to an in-memory ringbuffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
122 end
481c4d75e77d mod_log_ringbuffer: New module to send logs to an in-memory ringbuffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
123
5877
97c9b76867ca mod_log_ringbuffer: Detach event handlers on logging reload (thanks Menel)
Kim Alvefur <zash@zash.se>
parents: 5876
diff changeset
124 module:hook_global("reopen-log-files", function()
97c9b76867ca mod_log_ringbuffer: Detach event handlers on logging reload (thanks Menel)
Kim Alvefur <zash@zash.se>
parents: 5876
diff changeset
125 for handler, event_name in pairs(event_hooks) do
97c9b76867ca mod_log_ringbuffer: Detach event handlers on logging reload (thanks Menel)
Kim Alvefur <zash@zash.se>
parents: 5876
diff changeset
126 module:unhook_object_event(prosody.events, event_name, handler);
97c9b76867ca mod_log_ringbuffer: Detach event handlers on logging reload (thanks Menel)
Kim Alvefur <zash@zash.se>
parents: 5876
diff changeset
127 event_hooks[handler] = nil;
97c9b76867ca mod_log_ringbuffer: Detach event handlers on logging reload (thanks Menel)
Kim Alvefur <zash@zash.se>
parents: 5876
diff changeset
128 end
97c9b76867ca mod_log_ringbuffer: Detach event handlers on logging reload (thanks Menel)
Kim Alvefur <zash@zash.se>
parents: 5876
diff changeset
129 end, 1);
97c9b76867ca mod_log_ringbuffer: Detach event handlers on logging reload (thanks Menel)
Kim Alvefur <zash@zash.se>
parents: 5876
diff changeset
130
4205
481c4d75e77d mod_log_ringbuffer: New module to send logs to an in-memory ringbuffer
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
131 loggingmanager.register_sink_type("ringbuffer", ringbuffer_log_sink_maker);