class Thrifty::Logger::App

Public Class Methods

append(entry) click to toggle source
# File lib/thrifty/logger/app.rb, line 86
def append(entry)
  @@appenders.each{|fn| fn.call(entry) }
end
appenders(*fn) click to toggle source
# File lib/thrifty/logger/app.rb, line 78
def appenders(*fn)
  @@appenders = fn.flatten
end
exception_handlers(*fn) click to toggle source
# File lib/thrifty/logger/app.rb, line 82
def exception_handlers(*fn)
  @@exception_handlers = fn.flatten
end
handle_exception(ex, scope = nil, context = {}) click to toggle source
# File lib/thrifty/logger/app.rb, line 90
def handle_exception(ex, scope = nil, context = {})
  @@exception_handlers.each{|fn| fn.call(ex, scope, context) }
end
new() click to toggle source
# File lib/thrifty/logger/app.rb, line 9
def initialize
  @queue = Queue.new
  @lock  = Mutex.new

  Thrifty::Signals.register_after(method(:stop))
end
reset!() click to toggle source
# File lib/thrifty/logger/app.rb, line 94
def reset!
  @@appenders          = [IoAppender.new]
  @@exception_handlers = [StderrExceptionHanlder.new]
end

Public Instance Methods

append(entry) click to toggle source
# File lib/thrifty/logger/app.rb, line 39
def append(entry)
  @queue.push(entry) if @thread
end
start() click to toggle source
# File lib/thrifty/logger/app.rb, line 16
def start
  @lock.synchronize do
    unless @thread
      @thread = main_loop
    end
  end
end
started?() click to toggle source
# File lib/thrifty/logger/app.rb, line 24
def started?
  !!@thread
end
stop() click to toggle source
# File lib/thrifty/logger/app.rb, line 28
def stop
  @lock.synchronize do
    return unless @thread
    append(new_entry("stopping"))
    append(:shutdown)
    @thread.join
    @thread = nil
    App.append(new_entry("stopped"))
  end
end

Private Instance Methods

main_loop() click to toggle source
# File lib/thrifty/logger/app.rb, line 54
def main_loop ; Thread.new do
  begin
    loop do
      entry = @queue.pop
      if entry == :shutdown
        break
      end
      if entry == :boom
        raise RuntimeError.new("ignore me")
      end
      App.append(entry)
    end
  rescue Exception => ex
    App.handle_exception(ex, self.class)
    retry
  end
end ; end
new_entry(msg) click to toggle source
# File lib/thrifty/logger/app.rb, line 45
def new_entry(msg)
  Entry.new(
    :info,
    Time.now,
    self.class,
    msg
  )
end