1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
|
module Librarian
class Logger
librarian_path = Pathname(__FILE__)
librarian_path = librarian_path.dirname until librarian_path.join("lib").directory?
LIBRARIAN_PATH = librarian_path
attr_accessor :environment
private :environment=
def initialize(environment)
self.environment = environment
end
def warn(string = nil, &block)
return unless ui
ui.warn(string || yield)
end
def info(string = nil, &block)
return unless ui
ui.info(string || yield)
end
def debug(string = nil, &block)
return unless ui
if ui.respond_to?(:debug_line_numbers) && ui.debug_line_numbers
loc = caller.find{|l| !(l =~ /in `debug'$/)}
if loc =~ /^(.+):(\d+):in `(.+)'$/
loc = "#{Pathname.new($1).relative_path_from(LIBRARIAN_PATH)}:#{$2}:in `#{$3}'"
end
ui.debug { "[Librarian] #{string || yield} [#{loc}]" }
else
ui.debug { "[Librarian] #{string || yield}" }
end
end
def relative_path_to(path)
environment.project_relative_path_to(path)
end
private
def ui
environment.ui
end
end
end
|