refactor: Clean up the logging system

This commit is contained in:
Glenn Y. Rolland 2023-04-27 09:42:15 +02:00
parent 8ae9599d0f
commit 0832e4c877
8 changed files with 78 additions and 13 deletions

View file

@ -2,6 +2,7 @@ require "option_parser"
require "digest/sha256" require "digest/sha256"
require "colorize" require "colorize"
require "./log"
require "./config" require "./config"
require "./build/cli" require "./build/cli"
require "./build/run" require "./build/run"
@ -14,6 +15,8 @@ require "./write/run"
module DocMachine module DocMachine
class Cli class Cli
Log = DocMachine::Log.for("cli")
def initialize def initialize
end end
@ -28,12 +31,18 @@ module DocMachine
"Main options:" "Main options:"
].join("\n") ].join("\n")
opts.on("-v", "--verbose", "Enable verbosity") do |verbose| opts.on("-v", "--verbosity LEVEL", "Change verbosity level to LEVEL (0..3)") do |verbose|
config.verbose = true verbose_i = verbose.to_i
verbose_i = 0 if verbose.to_i < 0
verbose_i = 3 if verbose.to_i > 3
config.verbosity = ::Log::Severity.from_value(3 - verbose_i)
rescue ex: ArgumentError
Log.error { "Wrong value for parameter --verbosity" }
exit 1
end end
opts.on("-h", "--help", "Show this help") do opts.on("-h", "--help", "Show this help") do
Log.info { opts } Log.notice { opts }
exit exit
end end
@ -47,15 +56,17 @@ module DocMachine
end end
parser.parse(args) parser.parse(args)
::Log.setup(config.verbosity, ::Log::IOBackend.new(formatter: BaseFormat))
Log.notice { "verbosity level = #{config.verbosity}" }
Log.debug { "commands = #{commands}" } Log.debug { "commands = #{commands}" }
if commands.size < 1 if commands.size < 1
Log.error { parser.to_s } Log.error { parser.to_s }
Log.error { "ERROR: no command defined" } Log.error { "No command defined" }
end end
commands.each do |command| commands.each do |command|
# Log.info { "== Running #{command}" } Log.debug { "== Running #{command}" }
command.call() command.call()
end end
end end

View file

@ -5,7 +5,7 @@ require "./module"
module DocMachine module DocMachine
class Config class Config
property verbose : Bool = false property verbosity = ::Log::Severity::Notice
def initialize def initialize
end end

38
src/log.cr Normal file
View file

@ -0,0 +1,38 @@
require "log"
require "colorize"
struct DebugFormat < Log::StaticFormatter
def run
string @entry.severity.label[0].downcase
string ": "
source
string ": "
message
end
end
struct BaseFormat < Log::StaticFormatter
def run
io = ::IO::Memory.new
color = case @entry.severity
when ::Log::Severity::Error
Colorize.colorize.red.bold
when ::Log::Severity::Warn
Colorize.colorize.red.yellow
when ::Log::Severity::Notice
Colorize.colorize.bold
else
Colorize.colorize
end
color.surround(io) do
io << @entry.message
end
string io.to_s
end
end
# Log.define_formatter BaseFormat, "#{severity.to_s.lstrip}(#{source}): #{message}"

View file

@ -1,10 +1,9 @@
require "./cli" require "./cli"
require "./log"
Log.define_formatter BaseFormat, "#{message}"
::Log.setup(:notice, Log::IOBackend.new(formatter: BaseFormat)) ::Log.setup(:notice, Log::IOBackend.new(formatter: BaseFormat))
::Log.progname = "(root)"
app = DocMachine::Cli.new app = DocMachine::Cli.new
app.start(ARGV) app.start(ARGV)

View file

@ -1,9 +1,9 @@
require "./config" require "./config"
require "./run" require "./run"
require "./module"
module DocMachine::Write module DocMachine::Write
Log = DocMachine::Log.for("write")
class Cli class Cli
Log = DocMachine::Write::Log.for("cli") Log = DocMachine::Write::Log.for("cli")
@ -18,15 +18,20 @@ module DocMachine::Write
end end
commands << ->() : Nil do commands << ->() : Nil do
Log.debug { "before any" }
if args.size < 1 if args.size < 1
Log.error { "ERROR: No target given!" } Log.error { "No target given!" }
exit 1 exit 1
end end
config.target_directory = args[0] config.target_directory = args[0]
Log.debug { "before new" }
app = DocMachine::Write::Run.new(config) app = DocMachine::Write::Run.new(config)
Log.debug { "before prepare" }
app.prepare app.prepare
Log.debug { "before start" }
app.start app.start
Log.debug { "before wait" }
app.wait app.wait
end end
end end

6
src/write/module.cr Normal file
View file

@ -0,0 +1,6 @@
require "../module"
module DocMachine::Write
Log = DocMachine::Log.for("write")
end

View file

@ -0,0 +1,6 @@
require "../module"
module DocMachine::Write::Nodes
Log = DocMachine::Write::Log.for("nodes")
end

View file

@ -18,7 +18,7 @@ module DocMachine::Write
# Verify parameters # Verify parameters
def prepare() def prepare()
if ! File.directory? @config.target_directory if ! File.directory? @config.target_directory
Log.error { "ERROR: target must be a directory" } Log.error { "Target must be a directory" }
exit 1 exit 1
end end