]> gitweb.fluxo.info Git - leap/leap_cli.git/commitdiff
improve logging: set exit code on puppet error, better puppet error handling, better...
authorelijah <elijah@riseup.net>
Thu, 28 Feb 2013 07:53:10 +0000 (23:53 -0800)
committerelijah <elijah@riseup.net>
Thu, 28 Feb 2013 07:53:10 +0000 (23:53 -0800)
lib/leap_cli/log.rb
lib/leap_cli/logger.rb

index fb43d7b29404566859bea79612827f3863107f14..4d5e4da50a2f12eebb6eae10f4d4abfd4733e724 100644 (file)
@@ -78,7 +78,7 @@ module LeapCli
       if title
         prefix_options = case title
           when :error     then ['error', :red, :bold]
-          when :warning   then ['warning', :yellow, :bold]
+          when :warning   then ['warning:', :yellow, :bold]
           when :info      then ['info', :cyan, :bold]
           when :updated   then ['updated', :cyan, :bold]
           when :updating  then ['updating', :cyan, :bold]
@@ -126,6 +126,7 @@ module LeapCli
     #
     # Add a raw log entry, without any modifications (other than indent).
     # Content to be logged is yielded by the block.
+    # Block may be either a string or array of strings.
     #
     # if mode == :stdout, output is sent to STDOUT.
     # if mode == :log, output is sent to log file, if present.
@@ -134,16 +135,18 @@ module LeapCli
       # NOTE: print message (using 'print' produces better results than 'puts' when multiple threads are logging)
       if mode == :log
         if LeapCli.log_output_stream
-          message = yield
-          if message
+          messages = [yield].compact.flatten
+          if messages.any?
             timestamp = Time.now.strftime("%b %d %H:%M:%S")
-            LeapCli.log_output_stream.print("#{timestamp} #{message}\n")
+            messages.each do |message|
+              LeapCli.log_output_stream.print("#{timestamp} #{message}\n")
+            end
             LeapCli.log_output_stream.flush
           end
         end
       elsif mode == :stdout
-        message = yield
-        if message
+        messages = [yield].compact.flatten
+        if messages.any?
           indent ||= LeapCli.indent_level
           indent_str = ""
           indent_str += "  " * indent.to_i
@@ -152,7 +155,9 @@ module LeapCli
           else
             indent_str += ' = '
           end
-          STDOUT.print("#{indent_str}#{message}\n")
+          messages.each do |message|
+            STDOUT.print("#{indent_str}#{message}\n")
+          end
         end
       end
     end
index eca7aa6964a35b071a48bdf41c76a63f292080c0..373af35da3550d5cfea7bd309a67552af8bfbff2 100644 (file)
@@ -44,9 +44,9 @@ module LeapCli
     end
 
     def log(level, message, line_prefix=nil, options={})
-      # in some cases, when the message doesn't end with a return, we buffer it and
-      # wait until we encounter the return before we log the message out.
       if message !~ /\n$/ && level <= 2 && line_prefix.is_a?(String)
+        # in some cases, when the message doesn't end with a return, we buffer it and
+        # wait until we encounter the return before we log the message out.
         @message_buffer ||= ""
         @message_buffer += message
         return
@@ -56,26 +56,30 @@ module LeapCli
       end
 
       options[:level] ||= level
-      message.lines.each do |line|
-        [:stdout, :log].each do |mode|
-          LeapCli::log_raw(mode) {
-            formatted_line, formatted_prefix, line_options = apply_formatting(mode, line, line_prefix, options)
-            if line_options[:level] <= self.level && formatted_line && formatted_line.chars.any?
-              if formatted_prefix
-                "[#{formatted_prefix}] #{formatted_line}"
-              else
-                formatted_line
-              end
-            else
-              nil
-            end
-          }
+      [:stdout, :log].each do |mode|
+        LeapCli::log_raw(mode) do
+          message_lines(mode, message, line_prefix, options)
         end
       end
     end
 
     private
 
+    def message_lines(mode, message, line_prefix, options)
+      formatted_message, formatted_prefix, message_options = apply_formatting(mode, message, line_prefix, options)
+      if message_options[:level] <= self.level && formatted_message && formatted_message.chars.any?
+        if formatted_prefix
+          formatted_message.lines.collect { |line|
+            "[#{formatted_prefix}] #{line.sub(/\s+$/, '')}"
+          }
+        else
+          formatted_message.lines.collect {|line| line.sub(/\s+$/, '')}
+        end
+      else
+        nil
+      end
+    end
+
     ##
     ## FORMATTING
     ##
@@ -114,10 +118,15 @@ module LeapCli
       { :match => /^warning: .*is deprecated.*$/,  :level => 2, :color => :yellow, :priority => -10},
       { :match => /^warning: Scope.*$/,            :level => 2, :color => :yellow, :priority => -10},
       { :match => /^notice:/,                      :level => 1, :color => :cyan,   :priority => -20},
-      { :match => /^err:/,                         :level => 0, :color => :red,    :priority => -20},
       { :match => /^warning:/,                     :level => 0, :color => :yellow, :priority => -20},
       { :match => /^Duplicate declaration:/,       :level => 0, :color => :red,    :priority => -20},
       { :match => /Finished catalog run/,          :level => 0, :color => :green,  :priority => -10},
+
+      # PUPPET FATAL ERRORS
+      { :match => /^err:/,                         :level => 0, :color => :red, :priority => -1, :exit => 1},
+      { :match => /^Failed to parse template/,     :level => 0, :color => :red, :priority => -1, :exit => 1},
+      { :match => /^Parameter matches failed:/,    :level => 0, :color => :red, :priority => -1, :exit => 1},
+      { :match => /^Syntax error/,                 :level => 0, :color => :red, :priority => -1, :exit => 1}
     ]
 
     def self.sorted_formatters
@@ -164,6 +173,10 @@ module LeapCli
             message.replace(message + formatter[:append])  unless formatter[:append].nil?
             message.replace(Time.now.strftime('%Y-%m-%d %T') + ' ' + message) if formatter[:timestamp]
 
+            if formatter[:exit]
+              LeapCli::Util.exit_status(formatter[:exit])
+            end
+
             # stop formatting, unless formatter was just for string replacement
             break unless formatter[:replace]
           end