New logging mechanism (log to files) + select IPC messages to show.

This commit is contained in:
Philippe Pittoli 2024-12-12 20:30:13 +01:00
parent f218ac685f
commit 26de0a4e1d

View File

@ -50,19 +50,20 @@ class Baguette::Configuration
end end
class IPC < Base class IPC < Base
enum MESSAGE
TIMER
CONNECTION
DISCONNECTION
EXTERNAL # extra socket
RX
TX
SWITCH
ERROR
EXCEPTION
end
property ipc_timer : Int32 = 30_000 # 30 seconds. property ipc_timer : Int32 = 30_000 # 30 seconds.
property ipc_messages_to_show : Array(IPC::MESSAGE) = [MESSAGE::ERROR, MESSAGE::EXCEPTION]
property print_ipc_timer : Bool = false
property print_ipc_connection : Bool = false
property print_ipc_disconnection : Bool = false
property print_ipc_extra_socket : Bool = false
property print_ipc_message_received : Bool = false
property print_ipc_message_sent : Bool = false
property print_ipc_switch : Bool = false
property print_ipc_error : Bool = true
property print_ipc_exception : Bool = true
property print_keepalive : Bool = false
property verbosity : Int32 = 4 property verbosity : Int32 = 4
def initialize def initialize
@ -125,49 +126,94 @@ class Baguette::Configuration
end end
end end
# `Baguette::Log` is a library to display information.
# A filename can be given to write logs in a file.
class Baguette::Log class Baguette::Log
# FIXME: Use log files.
# FIXME: def log(), that puts stuff as-is in the logs. # FIXME: def log(), that puts stuff as-is in the logs.
# Name of the file.
class_property log_path : String? = nil
enum LOGTYPE
DEBUG = 5
INFO = 4
TITLE = 3
WARNING = 2
ERROR = 1
end
def self.now() : String
Time.local.to_s("%Y-%m-%d_%H:%M:%S")
end
def self.write_log_fancy(io : IO, logtype : LOGTYPE, text)
case logtype
when .debug?
io.<<(self.now).<<(" :: ".colorize(:cyan)).<<(text.colorize(:cyan)).<<("\n")
when .info?
io.<<(self.now).<<(" :: ".colorize(:blue)).<<(text.colorize(:white)).<<("\n")
when .title?
io.<<(self.now).<<(" |> ".colorize(:blue).bright).<<(text.colorize(:white).bright).<<("\n")
when .warning?
io.<<(self.now).<<(" :: ".colorize(:yellow).bright).<<(text.colorize(:yellow)).<<("\n")
when .error?
io.<<(self.now).<<(" !! ".colorize(:red).bright).<<(text.colorize(:red)).<<("\n")
end
end
def self.write_log_plain(io : IO, logtype : LOGTYPE, text)
case logtype
when .debug?
io.<<(self.now).<<(" (debug) ").<<(text).<<("\n")
when .info?
io.<<(self.now).<<(" (info) ").<<(text).<<("\n")
when .title?
io.<<(self.now).<<(" (title) ").<<(text).<<("\n")
when .warning?
io.<<(self.now).<<(" (warning) ").<<(text).<<("\n")
when .error?
io.<<(self.now).<<(" (error) ").<<(text).<<("\n")
end
end
def self.write_log(logtype : LOGTYPE, text)
if path = @@log_path
File.open path, "a" do |io|
self.write_log_plain io, logtype, text
end
# In case the thing we want to print is important, put it on the screen too.
if logtype <= LOGTYPE::WARNING
self.write_log_fancy STDERR, logtype, text
end
else
if logtype <= LOGTYPE::WARNING
self.write_log_fancy STDERR, logtype, text
else
self.write_log_fancy STDOUT, logtype, text
end
end
end
def self.debug(text) def self.debug(text)
return unless Baguette::Context.verbosity > 3 return unless Baguette::Context.verbosity > 3
STDERR self.write_log LOGTYPE::DEBUG, text
.<<(":: ".colorize(:cyan))
.<<(text.colorize(:cyan))
.<<("\n")
STDERR.flush
end end
def self.info(text) def self.info(text)
return unless Baguette::Context.verbosity > 2 return unless Baguette::Context.verbosity > 2
STDOUT self.write_log LOGTYPE::INFO, text
.<<(":: ".colorize(:blue))
.<<(text.colorize(:white))
.<<("\n")
STDOUT.flush
end end
def self.title(text) def self.title(text)
return unless Baguette::Context.verbosity > 2 return unless Baguette::Context.verbosity > 2
STDOUT self.write_log LOGTYPE::TITLE, text
.<<("|> ".colorize(:blue).bright)
.<<(text.colorize(:white).bright)
.<<("\n")
STDOUT.flush
end end
def self.warning(text) def self.warning(text)
return unless Baguette::Context.verbosity > 1 return unless Baguette::Context.verbosity > 1
STDERR self.write_log LOGTYPE::WARNING, text
.<<(":: ".colorize(:yellow).bright)
.<<(text.colorize(:yellow))
.<<("\n")
STDERR.flush
end end
def self.error(text) def self.error(text)
return unless Baguette::Context.verbosity > 0 return unless Baguette::Context.verbosity > 0
STDERR self.write_log LOGTYPE::ERROR, text
.<<("!! ".colorize(:red).bright)
.<<(text.colorize(:red))
.<<("\n")
STDERR.flush
end end
end end