lib/mj/logging.rb in build-tool-0.5.3 vs lib/mj/logging.rb in build-tool-0.5.4
- old
+ new
@@ -36,10 +36,13 @@
end
end # class BasicLayout
+
+
+
# :TODO: Map the Logging Levels to Logger Levels instead of hardcoding them here.
class LoggerAdapter
def initialize( prefix )
@prefix = prefix.upcase
@@ -88,6 +91,37 @@
def unknown( string )
logger.error( "<#{@prefix}> UNKNOWN: #{string}" )
end
end
+
+
+ class Progressbar < ::Logging::Appender
+
+ def initialize( title, total = 100, &block )
+ super( 'Progressbar', :level => :DEBUG )
+ @pbar = nil
+ @oldlogger = nil
+ if ::Logging.appenders['stdout'].level >= ::Logging::level_num(:INFO)
+ # We only do the progressbar thing if there is no verbose output active.
+ begin
+ # Remove the old stdout logger.
+ @oldlogger = ::Logging.appenders[ 'stdout' ]
+ ::Logging.logger[ 'root' ].remove_appenders( 'stdout' )
+ ::Logging.logger[ 'root' ].add_appenders( self )
+ # Add the progressbar logger
+ @pbar = ANSI::Progressbar.new( title, total )
+ yield
+ ensure
+ @pbar.finish unless @pbar.nil?
+ # Reset the logger
+ ::Logging.logger[ 'root' ].remove_appenders( 'Progressbar' )
+ ::Logging.logger[ 'root' ].add_appenders( @oldlogger )
+ end
+ else
+ # If there is verbose output just print the text
+ logger.info( title )
+ yield
+ end
+ end
+ end # class Progressbar
end; end