Sha256: 6ab6e1bb49af666ae377c0300647ad97d7f4ef6a7f43b6c750948f7e543a1425

Contents?: true

Size: 1.44 KB

Versions: 5

Compression:

Stored size: 1.44 KB

Contents

class Navy::ScopedLogger

  attr_accessor :logger, :scope

  def initialize(logger, scope = nil)
    @logger, @scope = logger, scope
  end

  def add(*args, &block)
    scoped(:add, *args, &block)
  end

  def debug(*args, &block)
    scoped(:debug, *args, &block)
  end

  def info(*args, &block)
    scoped(:info, *args, &block)
  end

  def warn(*args, &block)
    scoped(:warn, *args, &block)
  end

  def error(*args, &block)
    scoped(:error, *args, &block)
  end

  def fatal(*args, &block)
    scoped(:fatal, *args, &block)
  end

  def respond_to?(*args)
    @logger.respond_to?(*args)
  end

  protected

  def method_missing(name, *args, &block)
    @logger.__send__(name, *args, &block)
  end

  def scoped(name, *args, &block)
    start_scope!
    retval = @logger.__send__(name, *args, &block)
    stop_scope!
    return retval
  end

  def start_scope!
    return unless scope
    scope.current_stdout = $stdout.dup
    scope.current_stderr = $stderr.dup
    if scope.stderr_path
      scope.stderr_path = scope.stderr_path
    else
      $stderr.reopen scope.orig_stderr
    end
    if scope.stdout_path
      scope.stdout_path = scope.stdout_path
    else
      $stdout.reopen scope.orig_stdout
    end
  end

  def stop_scope!
    return unless scope
    return unless scope.current_stderr and scope.current_stdout
    $stdout.reopen scope.current_stdout
    $stderr.reopen scope.current_stderr
    scope.current_stderr = scope.current_stdout = nil
  end

end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
navy-1.1.2 lib/navy/scoped_logger.rb
navy-1.1.1 lib/navy/scoped_logger.rb
navy-1.1.0 lib/navy/scoped_logger.rb
navy-1.0.7 lib/navy/scoped_logger.rb
navy-1.0.6 lib/navy/scoped_logger.rb