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.



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/mj/logging.rb', line 106

def initialize( title, total = 100, &block )
    super( 'Progressbar', :level => :DEBUG )
    @pbar = nil
    @oldlogger = nil
    if not STDOUT.tty? or
       ::Logging.appenders['stdout'].level != ::Logging::level_num(:INFO)
        # We only do the progressbar thing if there is no verbose output active.
        # And only if there is a terminal listening.
        logger.info( title )
        yield
        return
    end

    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, out = STDOUT )
        yield
    ensure
        @pbar.finish unless @pbar.nil?
        # Reset the logger
        ::Logging.logger[ 'root' ].remove_appenders( 'Progressbar' )
        ::Logging.logger[ 'root' ].add_appenders( @oldlogger )
    end
end