New logging mechanism (log to files) + select IPC messages to show.
This commit is contained in:
parent
f218ac685f
commit
26de0a4e1d
@ -50,19 +50,20 @@ class Baguette::Configuration
|
||||
end
|
||||
|
||||
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 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 ipc_messages_to_show : Array(IPC::MESSAGE) = [MESSAGE::ERROR, MESSAGE::EXCEPTION]
|
||||
property verbosity : Int32 = 4
|
||||
|
||||
def initialize
|
||||
@ -125,49 +126,94 @@ class Baguette::Configuration
|
||||
end
|
||||
end
|
||||
|
||||
# `Baguette::Log` is a library to display information.
|
||||
# A filename can be given to write logs in a file.
|
||||
class Baguette::Log
|
||||
# FIXME: Use log files.
|
||||
# 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)
|
||||
return unless Baguette::Context.verbosity > 3
|
||||
STDERR
|
||||
.<<(":: ".colorize(:cyan))
|
||||
.<<(text.colorize(:cyan))
|
||||
.<<("\n")
|
||||
STDERR.flush
|
||||
self.write_log LOGTYPE::DEBUG, text
|
||||
end
|
||||
def self.info(text)
|
||||
return unless Baguette::Context.verbosity > 2
|
||||
STDOUT
|
||||
.<<(":: ".colorize(:blue))
|
||||
.<<(text.colorize(:white))
|
||||
.<<("\n")
|
||||
STDOUT.flush
|
||||
self.write_log LOGTYPE::INFO, text
|
||||
end
|
||||
def self.title(text)
|
||||
return unless Baguette::Context.verbosity > 2
|
||||
STDOUT
|
||||
.<<("|> ".colorize(:blue).bright)
|
||||
.<<(text.colorize(:white).bright)
|
||||
.<<("\n")
|
||||
STDOUT.flush
|
||||
self.write_log LOGTYPE::TITLE, text
|
||||
end
|
||||
def self.warning(text)
|
||||
return unless Baguette::Context.verbosity > 1
|
||||
STDERR
|
||||
.<<(":: ".colorize(:yellow).bright)
|
||||
.<<(text.colorize(:yellow))
|
||||
.<<("\n")
|
||||
STDERR.flush
|
||||
self.write_log LOGTYPE::WARNING, text
|
||||
end
|
||||
def self.error(text)
|
||||
return unless Baguette::Context.verbosity > 0
|
||||
STDERR
|
||||
.<<("!! ".colorize(:red).bright)
|
||||
.<<(text.colorize(:red))
|
||||
.<<("\n")
|
||||
STDERR.flush
|
||||
self.write_log LOGTYPE::ERROR, text
|
||||
end
|
||||
end
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user