Select messages to show (IPC, DNSMANAGERd).

This commit is contained in:
Philippe Pittoli 2024-12-12 20:29:04 +01:00
parent 566581139a
commit 30d5f1d4a3
4 changed files with 48 additions and 17 deletions

View File

@ -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]

View File

@ -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

View File

@ -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

View File

@ -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