diff options
author | elijah <elijah@riseup.net> | 2012-11-13 22:49:32 -0800 |
---|---|---|
committer | elijah <elijah@riseup.net> | 2012-11-13 22:49:32 -0800 |
commit | 10bd0ba9d66a32cb8e0f7fb322843005b23181b7 (patch) | |
tree | 5768b4fc787b189b5df9035596d7de945342852f /lib/leap_cli/log.rb | |
parent | 622236c3ad6abc4aef200cc2d4fc2a09effd8647 (diff) |
cleaned up logging, and much improved error message when file is not found
Diffstat (limited to 'lib/leap_cli/log.rb')
-rw-r--r-- | lib/leap_cli/log.rb | 81 |
1 files changed, 49 insertions, 32 deletions
diff --git a/lib/leap_cli/log.rb b/lib/leap_cli/log.rb index 0cba54d..1266caf 100644 --- a/lib/leap_cli/log.rb +++ b/lib/leap_cli/log.rb @@ -1,10 +1,10 @@ +require 'paint' + module LeapCli extend self - def log_level @log_level ||= 1 end - def log_level=(value) @log_level = value end @@ -14,38 +14,55 @@ end ## LOGGING ## -def log0(message=nil, &block) - if message - puts message - elsif block - puts yield(block) - end -end +# +# these are log titles typically associated with files +# +FILE_TITLES = [:updated, :created, :removed, :missing, :nochange, :loading] -def log1(message=nil, &block) - if LeapCli.log_level > 0 - if message - puts message - elsif block - puts yield(block) +# +# master logging function. +# +# arguments can be a String, Integer, Symbol, or Hash, in any order. +# +# * String: treated as the message to log. +# * Integer: the log level (0, 1, 2) +# * Symbol: the prefix title to colorize. may be one of +# [:error, :warning, :info, :updated, :created, :removed, :no_change, :missing] +# * Hash: a hash of options. so far, only :indent is supported. +# +def log(*args) + level = args.grep(Integer).first || 1 + title = args.grep(Symbol).first + message = args.grep(String).first + options = args.grep(Hash).first || {:indent => 0} + if message && LeapCli.log_level >= level + print " " * (options[:indent]+1) + if options[:indent] > 0 + print '- ' + else + print '= ' end - end -end - -def log2(message=nil, &block) - if LeapCli.log_level > 1 - if message - puts message - elsif block - puts yield(block) + if title + prefix = case title + when :error then Paint['error', :red, :bold] + when :warning then Paint['warning', :yellow, :bold] + when :info then Paint['info', :cyan, :bold] + when :updated then Paint['updated', :cyan, :bold] + when :created then Paint['created', :green, :bold] + when :removed then Paint['removed', :red, :bold] + when :nochange then Paint['no change', :magenta] + when :loading then Paint['loading', :magenta] + when :missing then Paint['missing', :yellow, :bold] + when :run then Paint['run', :magenta] + when :failed then Paint['FAILED', :red, :bold] + when :ran then Paint['ran', :green, :bold] + else "" + end + print "#{prefix} " + if FILE_TITLES.include?(title) && message =~ /^\// + message = LeapCli::Path.relative_path(message) + end end + puts "#{message}" end end - -def progress(message) - log1(" = " + message) -end - -def progress2(message) - log2(" = " + message) -end |