Add log level.

This commit is contained in:
Danila Fedorin 2018-08-03 16:36:27 -07:00
parent 4de89d98a1
commit 1150e2b3ef
2 changed files with 29 additions and 12 deletions

View File

@ -12,7 +12,7 @@ module Chalk
def initialize(@config : Ui::Config) def initialize(@config : Ui::Config)
@logger = Logger.new STDOUT @logger = Logger.new STDOUT
@logger.debug("Initialized compiler") @logger.debug("Initialized compiler")
@logger.level = Logger::DEBUG @logger.level = @config.loglevel
end end
# Reads a file an extracts instances of # Reads a file an extracts instances of

View File

@ -22,10 +22,15 @@ module Chalk
getter mode : OutputMode getter mode : OutputMode
# Sets the mode in which the compiler should operate. # Sets the mode in which the compiler should operate.
setter mode : OutputMode setter mode : OutputMode
# Gets the log level
getter loglevel : Logger::Severity
# Sets the log level
setter loglevel : Logger::Severity
# Creates a new configuration. # Creates a new configuration.
def initialize(@file = "", def initialize(@file = "",
@mode = OutputMode::Tree) @mode = OutputMode::Tree,
@loglevel = Logger::Severity::DEBUG)
end end
# Reads a configuration from the command line options. # Reads a configuration from the command line options.
@ -34,20 +39,32 @@ module Chalk
OptionParser.parse! do |parser| OptionParser.parse! do |parser|
parser.banner = "Usage: chalk [arguments]" parser.banner = "Usage: chalk [arguments]"
parser.on("-m", "--mode=MODE", "Set the mode of the compiler.") do |mode| parser.on("-m", "--mode=MODE", "Set the mode of the compiler.") do |mode|
case mode.downcase hash = {
when "tree", "t" "tree" => OutputMode::Tree,
config.mode = OutputMode::Tree "t" => OutputMode::Tree,
when "intermediate", "i" "intermediate" => OutputMode::Intermediate,
config.mode = OutputMode::Intermediate "i" => OutputMode::Intermediate,
when "binary", "b" "binary" => OutputMode::Binary,
config.mode = OutputMode::Binary "b" => OutputMode::Binary
else }
puts "Invalid mode type. Using default." puts "Invalid mode type. Using default." if !hash.has_key?(mode)
end config.mode = hash[mode]? || OutputMode::Tree
end end
parser.on("-f", "--file=FILE", "Set the input file to compile.") do |file| parser.on("-f", "--file=FILE", "Set the input file to compile.") do |file|
config.file = file config.file = file
end end
parser.on("-l", "--log=LOG", "Set the log level of the compiler.") do |log|
hash = {
"debug" => Logger::Severity::DEBUG,
"fatal" => Logger::Severity::FATAL,
"error" => Logger::Severity::ERROR,
"info" => Logger::Severity::INFO,
"unknown" => Logger::Severity::UNKNOWN,
"warn" => Logger::Severity::WARN
}
puts "Invalid log level. Using default." if !hash.has_key?(log)
config.loglevel = hash[log]? || Logger::Severity::DEBUG
end
parser.on("-h", "--help", "Show this message.") { puts parser } parser.on("-h", "--help", "Show this message.") { puts parser }
end end
return config return config