Sha256: 93ead2bffa5db4e1bcc7d26793e76b7038506c1afa3e13a68fd576f75fba90be

Contents?: true

Size: 1.14 KB

Versions: 1

Compression:

Stored size: 1.14 KB

Contents

require 'logger'
require 'catcher/prefixed_logger'
require 'catcher/logger'

module Catcher

  def self.block(progname = nil)
    begin
      yield
    rescue Exception => e
      log_exception e, progname
    end
  end

  def self.thread(progname = nil)
    Thread.new do
      block progname do
        yield
      end
    end
  end

  def self.thread_loop(progname = nil)
    Thread.new do
      loop do
        block progname do
          yield
        end
      end
    end
  end

  def self.log_exception(e, progname)
    text = progname ? "Exception raised by '#{progname}'\n\t"  : ""
    if e.kind_of? Exception
      text << "[#{e.class}] #{e.message}\n\t#{e.backtrace.join("\n\t")}"
    else
      # This software is UFO ready
      text << "Some not very exceptional object raised o_O #{e.inspect} [#{e.class}]"
    end
    Catcher.logger.error text
  end

  def self.logger
    @logger || setup_logger(STDOUT)
  end

  def self.logger=(logger)
    @logger = logger
  end

  def self.setup_logger(*args)
    @logger = ::Logger.new(*args)
    @logger.formatter = ::Logger::Formatter.new 
    @logger.datetime_format = "%Y-%m-%d %H:%M:%S "
    @logger
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
catcher-1.0.0 lib/catcher.rb