
https://svn.lrde.epita.fr/svn/lrdetools/trunk Index: ChangeLog from Nicolas Pouillard <ertai@lrde.epita.fr> Clean up the logger, and add colors. * vcs/lib/vcs/app.rb: Remove this ugly LOG constant and now use Vcs.logger. * vcs/lib/vcs/vcs.rb: Add a subclass of Logger in Vcs, and make it support the color. * vcs/lib/vcs/mail.rb, * vcs/lib/vcs/message.rb, * vcs/lib/vcs/changelog.rb, * vcs/lib/vcs/opt_parse.rb, * vcs/lib/vcs/common_commit.rb, * vcs/lib/vcs/script.rb, * vcs/lib/vcs/environment.rb, * vcs/lib/vcs/news.rb: Replace LOG by logger. * vcs/NEWS: . NEWS | 2 + lib/vcs/app.rb | 25 ++++----------- lib/vcs/changelog.rb | 8 ++-- lib/vcs/common_commit.rb | 8 ++-- lib/vcs/environment.rb | 4 +- lib/vcs/mail.rb | 6 +-- lib/vcs/message.rb | 2 - lib/vcs/news.rb | 12 +++---- lib/vcs/opt_parse.rb | 2 - lib/vcs/script.rb | 4 +- lib/vcs/vcs.rb | 77 +++++++++++++++++++++++++++++++++++++++-------- 11 files changed, 96 insertions(+), 54 deletions(-) Index: vcs/lib/vcs/script.rb --- vcs/lib/vcs/script.rb (revision 241) +++ vcs/lib/vcs/script.rb (working copy) @@ -11,8 +11,8 @@ begin eval(code) rescue Exception => ex - LOG.error { 'Vcs#script: during the client execution' } - LOG.error { ex.long_pp } + logger.error { 'Vcs#script: during the client execution' } + logger.error { ex.long_pp } end end Index: vcs/lib/vcs/app.rb --- vcs/lib/vcs/app.rb (revision 241) +++ vcs/lib/vcs/app.rb (working copy) @@ -4,19 +4,6 @@ # Revision:: $Id$ require 'pathname' -require 'logger' - -LOG = Logger.new(STDERR) -if severity = ENV['VCS_SEVERITY'] - LOG.level = Logger.const_get(severity.upcase) -else - LOG.level = Logger::INFO -end -def LOG.format_message(severity, timestamp, msg, progname) - progname += ': ' unless progname.nil? or progname.empty? - "vcs: #{progname}#{severity.downcase}: #{msg}\n" -end - # vcs: The version control system wrapper. # @@ -35,14 +22,15 @@ cattr_accessor :dir cattr_accessor :extension_dirs cattr_accessor :path + cattr_accessor :logger + self.logger = Vcs.logger class << self def requirements Pathname.glob("{#{vcs_dir},#{extension_dirs.join(',')}}/*.rb") do |file| next if file.to_s =~ /\/(app|opt_parse|vcs)\.rb$/ - LOG.debug { file.basename.to_s } - require file.to_s + logger.debug { file.basename.to_s } if require file.to_s end grab_all_vcs() end @@ -105,8 +93,9 @@ def run vcs = nil begin - vcs_extensions_setup() user_configuration_setup() + Vcs.logger.color = Vcs.color? { STDERR.tty? } + vcs_extensions_setup() @@parser.parse! ARGV if Vcs.default.nil? if Vcs.default.nil? STDERR.puts @@parser @@ -131,10 +120,10 @@ raise ex rescue Exception => ex vcs.call_handlers unless vcs.nil? - LOG.debug { raise ex } + logger.debug { raise ex } err = ex.to_s.sub(/\.$/, '') err = ex.inspect if err.empty? - LOG.error err + logger.error err end end Index: vcs/lib/vcs/news.rb --- vcs/lib/vcs/news.rb (revision 241) +++ vcs/lib/vcs/news.rb (working copy) @@ -47,9 +47,9 @@ def check_line ( anIO, aRegex ) line = anIO.readline.chomp! - LOG.debug('news') { "Server: #{line}" } + logger.debug('news') { "Server: #{line}" } unless line =~ aRegex - LOG.error('news') { "Bad answer: #{line}" } + logger.error('news') { "Bad answer: #{line}" } @news_status = 'Error.' end end @@ -68,11 +68,11 @@ opt = YAML::chop_header(file) server, port = opt[:server].split(/:/) port ||= 119 - LOG.info('news') { "Nntp Server: #{server}:#{port}" } + logger.info('news') { "Nntp Server: #{server}:#{port}" } unless @h.agree("Post a news, with this subject: #{opt[:subject]}\n" + " to #{opt[:groups].join(', ')}\n from #{opt[:from]}\n" + 'Are you sure? (y/n)', true) - LOG.error('news') { 'Aborting' } + logger.error('news') { 'Aborting' } exit end require 'socket' @@ -99,7 +99,7 @@ def news_failed if defined? NEWS and NEWS.exist? - LOG.info "#{NEWS}: Contains the generated news" + + logger.info "#{NEWS}: Contains the generated news" + "(generated from #{@@message})" end end @@ -107,7 +107,7 @@ def news_conf_checker %w[ NNTPSERVER ].each do |var| if ENV[var].nil? or ENV[var].empty? - LOG.error "environment variable `#{var}' not set" + logger.error "environment variable `#{var}' not set" end end end Index: vcs/lib/vcs/mail.rb --- vcs/lib/vcs/mail.rb (revision 241) +++ vcs/lib/vcs/mail.rb (working copy) @@ -29,7 +29,7 @@ def mail_failed if defined? MAIL and MAIL.exist? - LOG.info "#{MAIL}: Contains the generated mail " + + logger.info "#{MAIL}: Contains the generated mail " + "(generated from #{Message})" end end @@ -37,10 +37,10 @@ def mail_conf_checker if Vcs.user_conf.sign unless `gpg --version` =~ /^gpg \(GnuPG\)/ - LOG.error 'mail: gunpg is required' + logger.error 'mail: gunpg is required' end unless File.exist?("#{ENV['HOME']}/.gnupg/secring.gpg") - LOG.error 'no private key: in your ~/.gnupg' + logger.error 'no private key: in your ~/.gnupg' end end end Index: vcs/lib/vcs/common_commit.rb --- vcs/lib/vcs/common_commit.rb (revision 241) +++ vcs/lib/vcs/common_commit.rb (working copy) @@ -51,14 +51,14 @@ block[iform['subject']] if block_given? - LOG.info 'Deleting junk files...' + logger.info 'Deleting junk files...' TMP_CL.delete if TMP_CL.exist? destdir = '+commited'.to_path/iform['revision'].to_s destdir.mkpath unless destdir.directory? [LogEntry, Form, IForm, Message, MAIL, NEWS].each do |path| next unless path.exist? dest = destdir/path - LOG.info "Moving `#{path}' to `#{dest}'..." + logger.info "Moving `#{path}' to `#{dest}'..." path.rename(dest) end @@ -66,8 +66,8 @@ protected :common_commit! def commit_failed ( ex=nil ) - LOG.error "Aborting #{ex}" - LOG.info 'You can rerun the same command to resume the commit' + logger.error "Aborting #{ex}" + logger.info 'You can rerun the same command to resume the commit' raise 'Commit failed' end Index: vcs/lib/vcs/changelog.rb --- vcs/lib/vcs/changelog.rb (revision 241) +++ vcs/lib/vcs/changelog.rb (working copy) @@ -78,15 +78,15 @@ error_handling :concat_changelog_failed unless TMP_CL.exist? - LOG.info "Backup your `#{CL}' to `#{TMP_CL}' ..." + logger.info "Backup your `#{CL}' to `#{TMP_CL}' ..." CL.rename(TMP_CL) end CL.open('w') do |file| - LOG.info "#{CL}: Writing your new entry ..." + logger.info "#{CL}: Writing your new entry ..." with(file).mk_changelog_entry!(*args) file.puts - LOG.info "#{CL}: Writing the others ..." + logger.info "#{CL}: Writing the others ..." file.print TMP_CL.read end end @@ -94,7 +94,7 @@ def concat_changelog_failed if TMP_CL.exist? - LOG.info "Restoring `#{CL}' from `#{TMP_CL}' ..." + logger.info "Restoring `#{CL}' from `#{TMP_CL}' ..." TMP_CL.rename(CL) end end Index: vcs/NEWS --- vcs/NEWS (revision 241) +++ vcs/NEWS (working copy) @@ -35,6 +35,8 @@ - Status: the status output is now colored depending of the meaning of the status. The color activation can be choose in your configuration file (auto, never, always). + - Logger: messages displayed by the Vcs logger are new colored. + cyan (info), yellow (warnings), red (errors), blinking red (fatal). * ,messages is now +commited: You can rename your ,messages and keep just one directory for commited meta information. Index: vcs/lib/vcs/environment.rb --- vcs/lib/vcs/environment.rb (revision 241) +++ vcs/lib/vcs/environment.rb (working copy) @@ -36,12 +36,12 @@ if var = ENV[name] var elsif block.nil? - LOG.error "Need #{name} in the environement" + logger.error "Need #{name} in the environement" @@env_status = false "!!! #{name} not set !!!" else default = block[] - LOG.warn "Need #{name} in the environement (default: #{default})" + logger.warn "Need #{name} in the environement (default: #{default})" ENV[name] = default default end Index: vcs/lib/vcs/vcs.rb --- vcs/lib/vcs/vcs.rb (revision 241) +++ vcs/lib/vcs/vcs.rb (working copy) @@ -44,6 +44,53 @@ cattr_accessor :default cattr_accessor :user_conf cattr_accessor :output_io_methods + cattr_accessor :logger + + + class Logger < ::Logger + + attr_accessor :color + + def initialize ( *a, &b ) + super + @high_line = HighLine.new + @color = false + end + + def format_message(severity, timestamp, msg, progname) + progname += ': ' unless progname.nil? or progname.empty? + "#{stylize('[')}#{stylize('vcs')}#{stylize(']')} " + + "#{progname}#{stylize(severity)}#{stylize(':')} #{msg}\n" + end + + @@style = + { + :vcs => [:cyan], + :debug => [:magenta], + :info => [:green], + :warn => [:yellow], + :error => [:red], + :fatal => [:red, :blink], + :'[' => [:blue], + :']' => [:blue], + :':' => [:red], + } + + def stylize ( aString ) + aString = aString.downcase + if @color + @high_line.color(aString, *@@style[aString.to_sym]) + else + aString + end + end + + end # class Logger + + self.logger = Logger.new(STDERR) + ENV['VCS_SEVERITY'] ||= 'info' + logger.level = Logger.const_get(ENV['VCS_SEVERITY'].upcase) + logger.color = STDERR.tty? class Failure < Exception end @@ -83,14 +130,14 @@ @runner.subscribe_hook(:failure) do |data| if data.output == STDOUT - LOG.debug { raise data.to_yaml } + logger.debug { raise data.to_yaml } exit((data.status)? data.status.exitstatus : 1) else raise data.to_yaml end end @runner.subscribe_hook(:display_command) do |cmd| - LOG.debug { "running: #{cmd.to_sh}" } + logger.debug { "running: #{cmd.to_sh}" } end @runner.subscribe_hook(:before_exec) do STDOUT.flush @@ -202,7 +249,7 @@ if name =~ /^(.*)_$/ run!($1, *args) else - LOG.warn { "unknown method #{orig}" } + logger.warn { "unknown method #{orig}" } run!(name, *args) end end @@ -237,7 +284,7 @@ loc = block.source_location # FIXME verify that this type of cache is working return @@cache[loc].dup if @@cache.has_key? loc if path.exist? - LOG.info "#{path} already exists" + logger.info "#{path} already exists" return path.read end unless path.nil? @@ -245,14 +292,14 @@ raise ArgumentError, "need a description for #{path}" end error_handling do - LOG.info "#{path}: Contains your #{description}" if path.exist? + logger.info "#{path}: Contains your #{description}" if path.exist? end end begin - LOG.info "Creating a new `#{path}' file ..." + logger.info "Creating a new `#{path}' file ..." path.open('w') { |f| result = with(f, &block) } rescue Exception => ex - LOG.error "Removing `#{path}' ..." + logger.error "Removing `#{path}' ..." path.unlink raise ex end @@ -294,12 +341,7 @@ end def color? - case Vcs.user_conf.color - when :never then return false - when :auto then return output.tty? - when :always then return true - else raise ArgumentError, "Bad value for Vcs.color: #{Vcs.color}" - end + Vcs.color? { output.tty? } end def color ( aString, *someStyles ) @@ -392,6 +434,15 @@ return :unrecognize end + def color? ( &auto_block ) + case color = Vcs.user_conf.color + when :never then return false + when :auto then return (auto_block.nil?)? false : auto_block[] + when :always then return true + else raise ArgumentError, "Bad value for `color' (#{color})" + end + end + def merge_user_conf ( conf ) conf = YAML.load(conf.read) if conf.is_a? Pathname conf.each do |k, v| Index: vcs/lib/vcs/opt_parse.rb --- vcs/lib/vcs/opt_parse.rb (revision 241) +++ vcs/lib/vcs/opt_parse.rb (working copy) @@ -61,7 +61,7 @@ end o.on('-d', '--debug LEVEL', 'Set debug level') do |severity| - LOG.level = Logger.const_get(severity.upcase) + logger.level = Logger.const_get(severity.upcase) end o.on_tail('-h', '--help', 'Show this message') do |test| Index: vcs/lib/vcs/message.rb --- vcs/lib/vcs/message.rb (revision 241) +++ vcs/lib/vcs/message.rb (working copy) @@ -6,7 +6,7 @@ class Vcs def print_body ( file, options ) - LOG.info "Creating a new `#{file}' file" + logger.info "Creating a new `#{file}' file" file.open('w') do |f| #FIXME add --- f.puts options.to_yaml