diff --git a/configuration-example.yml b/configuration-example.yml index c9fe236..01d826f 100644 --- a/configuration-example.yml +++ b/configuration-example.yml @@ -34,14 +34,8 @@ accepted_domains: #verbosity: 4 # [0-4], “0” being quiet and “4” meaning printing debug values -# IPC-related variables. By default, only print errors and exceptions. -#print_ipc_timer: false -#print_ipc_connection: false -#print_ipc_disconnection: false -#print_ipc_extra_socket: false -#print_ipc_message_received: false -#print_ipc_message_sent: false -#print_ipc_switch: false -#print_ipc_error: true -#print_ipc_exception: true -#print_keepalive: false +# By default, print everything except KEEPALIVE messages. +#messages_to_mask: [KEEPALIVE] + +# IPC-related messages. By default, only print errors and exceptions. +#ipc_messages_to_show: [ERROR, EXCEPTION] diff --git a/src/config.cr b/src/config.cr index af6213b..ff242cd 100644 --- a/src/config.cr +++ b/src/config.cr @@ -16,6 +16,13 @@ class Baguette::Configuration # See tools/write-template-zone-file.cr property template_directory : String = "/etc/dnsmanager/templates" + # Handle messages to display and to mask. + # Messages to mask, to focus on what is important. + property messages_to_mask : Array(DNSMESSAGE) = [DNSMESSAGE::KEEPALIVE] + + # Path to the log file. No log file = just print everything. + property log_file : String? = nil + def initialize end end diff --git a/src/main.cr b/src/main.cr index c611663..96d0716 100644 --- a/src/main.cr +++ b/src/main.cr @@ -112,5 +112,9 @@ unless configuration.pass exit 1 end +if path = configuration.log_file + Baguette::Log.log_path = path +end + service = DNSManager::Service.new configuration service.run diff --git a/src/service.cr b/src/service.cr index be68b23..199db12 100644 --- a/src/service.cr +++ b/src/service.cr @@ -4,6 +4,24 @@ require "baguette-crystal-base" require "./config" require "./exceptions" +class Array(T) + def contains?(value : T) + (self.select { |x| x == value }).size > 0 + end +end + +# WIP: select (dynamically) messages to mask + +module DNSManager + enum MESSAGE + KEEPALIVE + # TODO + end +end + +alias IPCMESSAGE = Baguette::Configuration::IPC::MESSAGE +alias DNSMESSAGE = DNSManager::MESSAGE + class DNSManager::Service < IPC property configuration : Baguette::Configuration::DNSManager getter storage : DNSManager::Storage @@ -11,6 +29,14 @@ class DNSManager::Service < IPC property authd : AuthD::Client + def should_display?(value : DNSMESSAGE) + (@configuration.messages_to_mask.select { |x| x == value }).size == 0 + end + + def should_display?(value : IPCMESSAGE) + @configuration.ipc_messages_to_show.contains? value + end + def initialize(@configuration) super() @storage = DNSManager::Storage.new @configuration.storage_directory, @configuration.recreate_indexes @@ -128,7 +154,7 @@ class DNSManager::Service < IPC if response.is_a? DNSManager::Response::Error Baguette::Log.warning "fd #{"%4d" % event.fd} (#{duration}) #{reqname} >> #{response_name} (#{response.reason})" else - if reqname != "KeepAlive" || @configuration.print_keepalive + if reqname != "KeepAlive" || should_display? DNSMESSAGE::KEEPALIVE Baguette::Log.debug "fd #{"%4d" % event.fd} (#{duration}) #{reqname} >> #{response_name}" end end @@ -141,20 +167,20 @@ class DNSManager::Service < IPC begin case event.type when LibIPC::EventType::Timer - Baguette::Log.debug "Timer." if @configuration.print_ipc_timer + Baguette::Log.debug "Timer." if should_display? IPCMESSAGE::TIMER when LibIPC::EventType::Connection - Baguette::Log.debug "New connection!" if @configuration.print_ipc_connection + Baguette::Log.debug "New connection!" if should_display? IPCMESSAGE::CONNECTION when LibIPC::EventType::Disconnection - Baguette::Log.debug "Disconnection from #{event.fd}." if @configuration.print_ipc_disconnection + Baguette::Log.debug "Disconnection from #{event.fd}." if should_display? IPCMESSAGE::DISCONNECTION @logged_users.delete event.fd when LibIPC::EventType::MessageTx - Baguette::Log.debug "Message sent to #{event.fd}." if @configuration.print_ipc_message_sent + Baguette::Log.debug "Message sent to #{event.fd}." if should_display? IPCMESSAGE::TX when LibIPC::EventType::MessageRx - Baguette::Log.debug "Message received from #{event.fd}." if @configuration.print_ipc_message_received + Baguette::Log.debug "Message received from #{event.fd}." if should_display? IPCMESSAGE::RX handle_request event else