Sha256: 1ac476e18c7b999c9393ea23dc82aa8ca51e167d3f868b6f0bf8bf087cd90d70

Contents?: true

Size: 1.95 KB

Versions: 1

Compression:

Stored size: 1.95 KB

Contents

#                                                                          
# File 'logger.rb' created on 23 feb 2008 at 15:46:54.                    
#
# See 'dokkit.rb' or +LICENSE+ for licence information.                      
#                                                                          
# (C) 2008 Andrea Fazzi <andrea.fazzi@alca.le.it> (and contributors). 
#                                                                          

#require 'dokkit'

module Dokkit
  module Log
    ERROR = 0
    WARNING = 1
    INFO = 2
    DEBUG = 3
    class LogMessage
      attr_reader :text
      attr_reader :level
      def initialize(text, level)
        @text, @level = text, level
      end
    end
    class Logger
      class << self
        def logger
          @logger ||= Logger.new
        end
        def clear
          @logger = nil
        end
      end
      attr_reader :observers
      attr_reader :last_message
      attr_reader :level
      def initialize
        @observers = []
        @level = INFO
      end
      def level=(new_level)
        new_level = Log::DEBUG if new_level > Log::DEBUG
        new_level = Log::ERROR if new_level < Log::ERROR
        @level = new_level
      end
      def attach(observer)
        (@observers << observer).last
      end
      def detach(observer)
        @observers.delete(observer)
      end
      def notify
        @observers.each { |observer| observer.update }
      end
      def message(message)
        @last_message = message
        notify
        @last_message
      end
      def info(text)
        message({ :text => text, :level => Log::INFO }) if level >= INFO
      end
      def error(text)
        message({ :text => text, :level => ERROR }) if level >= ERROR
      end
      def warn(text)
        message({ :text => text, :level => WARNING }) if level >= WARNING
      end
      def debug(text)
        message({ :text => text, :level => Log::DEBUG }) if level >= DEBUG
      end      
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dokkit-0.3.0 lib/dokkit/logger.rb