refactor: Clean up the logging system
This commit is contained in:
parent
8ae9599d0f
commit
0832e4c877
8 changed files with 78 additions and 13 deletions
21
src/cli.cr
21
src/cli.cr
|
@ -2,6 +2,7 @@ require "option_parser"
|
|||
require "digest/sha256"
|
||||
require "colorize"
|
||||
|
||||
require "./log"
|
||||
require "./config"
|
||||
require "./build/cli"
|
||||
require "./build/run"
|
||||
|
@ -14,6 +15,8 @@ require "./write/run"
|
|||
|
||||
module DocMachine
|
||||
class Cli
|
||||
Log = DocMachine::Log.for("cli")
|
||||
|
||||
def initialize
|
||||
end
|
||||
|
||||
|
@ -28,12 +31,18 @@ module DocMachine
|
|||
"Main options:"
|
||||
].join("\n")
|
||||
|
||||
opts.on("-v", "--verbose", "Enable verbosity") do |verbose|
|
||||
config.verbose = true
|
||||
opts.on("-v", "--verbosity LEVEL", "Change verbosity level to LEVEL (0..3)") do |verbose|
|
||||
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
|
||||
|
||||
opts.on("-h", "--help", "Show this help") do
|
||||
Log.info { opts }
|
||||
Log.notice { opts }
|
||||
exit
|
||||
end
|
||||
|
||||
|
@ -47,15 +56,17 @@ module DocMachine
|
|||
end
|
||||
|
||||
parser.parse(args)
|
||||
::Log.setup(config.verbosity, ::Log::IOBackend.new(formatter: BaseFormat))
|
||||
Log.notice { "verbosity level = #{config.verbosity}" }
|
||||
Log.debug { "commands = #{commands}" }
|
||||
|
||||
if commands.size < 1
|
||||
Log.error { parser.to_s }
|
||||
Log.error { "ERROR: no command defined" }
|
||||
Log.error { "No command defined" }
|
||||
end
|
||||
|
||||
commands.each do |command|
|
||||
# Log.info { "== Running #{command}" }
|
||||
Log.debug { "== Running #{command}" }
|
||||
command.call()
|
||||
end
|
||||
end
|
||||
|
|
|
@ -5,7 +5,7 @@ require "./module"
|
|||
module DocMachine
|
||||
class Config
|
||||
|
||||
property verbose : Bool = false
|
||||
property verbosity = ::Log::Severity::Notice
|
||||
|
||||
def initialize
|
||||
end
|
||||
|
|
38
src/log.cr
Normal file
38
src/log.cr
Normal 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}"
|
|
@ -1,10 +1,9 @@
|
|||
|
||||
require "./cli"
|
||||
|
||||
Log.define_formatter BaseFormat, "#{message}"
|
||||
|
||||
require "./log"
|
||||
|
||||
::Log.setup(:notice, Log::IOBackend.new(formatter: BaseFormat))
|
||||
::Log.progname = "(root)"
|
||||
app = DocMachine::Cli.new
|
||||
app.start(ARGV)
|
||||
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
|
||||
require "./config"
|
||||
require "./run"
|
||||
require "./module"
|
||||
|
||||
module DocMachine::Write
|
||||
Log = DocMachine::Log.for("write")
|
||||
|
||||
class Cli
|
||||
Log = DocMachine::Write::Log.for("cli")
|
||||
|
||||
|
@ -18,15 +18,20 @@ module DocMachine::Write
|
|||
end
|
||||
|
||||
commands << ->() : Nil do
|
||||
Log.debug { "before any" }
|
||||
if args.size < 1
|
||||
Log.error { "ERROR: No target given!" }
|
||||
Log.error { "No target given!" }
|
||||
exit 1
|
||||
end
|
||||
config.target_directory = args[0]
|
||||
|
||||
Log.debug { "before new" }
|
||||
app = DocMachine::Write::Run.new(config)
|
||||
Log.debug { "before prepare" }
|
||||
app.prepare
|
||||
Log.debug { "before start" }
|
||||
app.start
|
||||
Log.debug { "before wait" }
|
||||
app.wait
|
||||
end
|
||||
end
|
||||
|
|
6
src/write/module.cr
Normal file
6
src/write/module.cr
Normal file
|
@ -0,0 +1,6 @@
|
|||
|
||||
require "../module"
|
||||
|
||||
module DocMachine::Write
|
||||
Log = DocMachine::Log.for("write")
|
||||
end
|
6
src/write/nodes/module.cr
Normal file
6
src/write/nodes/module.cr
Normal file
|
@ -0,0 +1,6 @@
|
|||
|
||||
require "../module"
|
||||
|
||||
module DocMachine::Write::Nodes
|
||||
Log = DocMachine::Write::Log.for("nodes")
|
||||
end
|
|
@ -18,7 +18,7 @@ module DocMachine::Write
|
|||
# Verify parameters
|
||||
def prepare()
|
||||
if ! File.directory? @config.target_directory
|
||||
Log.error { "ERROR: target must be a directory" }
|
||||
Log.error { "Target must be a directory" }
|
||||
exit 1
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in a new issue