Class: MJ::Logging::Progressbar

Inherits:
Logging::Appender
  • Object
show all
Defined in:
lib/mj/logging.rb

Instance Method Summary collapse

Constructor Details

#initialize(title, total = 100, &block) ⇒ Progressbar

Returns a new instance of Progressbar.



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/mj/logging.rb', line 102

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