# # File 'logger.rb' created on 23 feb 2008 at 15:46:54. # # See 'dokkit.rb' or +LICENSE+ for licence information. # # (C) 2008 Andrea Fazzi (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