Sha256: c7306831394682e25143c09d8d88c3fe87219a9d2c1bf9c56e0befed5a846fb2

Contents?: true

Size: 1.76 KB

Versions: 4

Compression:

Stored size: 1.76 KB

Contents

#!/usr/bin/env ruby -w
# encoding: UTF-8
#
# = Log.rb -- Fit4Ruby - FIT file processing library for Ruby
#
# Copyright (c) 2015 by Chris Schlaeger <cs@taskjuggler.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of version 2 of the GNU General Public License as
# published by the Free Software Foundation.
#

require 'monitor'
require 'logger'
require 'singleton'

module Fit4Ruby

  # This is the Exception type that will be thrown for all unrecoverable
  # errors.
  class Error < StandardError ; end

  # The ILogger class is a singleton that provides a common logging mechanism
  # to all objects. It exposes essentially the same interface as the Logger
  # class, just as a singleton and with some additional methods like 'fatal'
  # and 'cricital'.
  class ILogger < Monitor

    include Singleton

    @@logger = Logger.new(STDOUT)

    # Redirect all log messages to the given IO.
    # @param io [IO] Output file descriptor
    def open(io)
      begin
        @@logger = Logger.new(io)
      rescue => e
        @@logger = Logger.new(STDERR)
        Log.fatal "Cannot open log file: #{e.message}"
      end
    end

    # Pass all calls to unknown methods to the @@logger object.
    def method_missing(method, *args, &block)
      @@logger.send(method, *args, &block)
    end

    # Make it properly introspectable.
    def respond_to?(method, include_private = false)
      @@logger.respond_to?(method)
    end

    # Print an error message via the Logger and raise and Fit4Ruby::Error.
    # code 1.
    def fatal(msg, &block)
      @@logger.error(msg, &block)
      raise Error, msg
    end

  end

  Log = ILogger.instance
  Log.level = Logger::WARN
  Log.formatter = proc do |severity, time, progname, msg|
    msg + "\n"
  end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
fit4ruby-0.0.12 lib/fit4ruby/Log.rb
fit4ruby-0.0.11 lib/fit4ruby/Log.rb
fit4ruby-0.0.10 lib/fit4ruby/Log.rb
fit4ruby-0.0.9 lib/fit4ruby/Log.rb