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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
|
# module : base/logger
# copyright : PRAGMA Advanced Document Engineering
# version : 2002-2005
# author : Hans Hagen
#
# project : ConTeXt / eXaMpLe
# concept : Hans Hagen
# info : j.hagen@xs4all.nl
# www : www.pragma-ade.com
require 'thread'
# The next calls are valid:
# @log.report('a','b','c', 'd')
# @log.report('a','b',"c #{d}")
# @log.report("a b c #{d}")
# Keep in mind that "whatever #{something}" is two times faster than
# 'whatever ' + something or ['whatever',something].join and that
# when verbosity is not needed the following is much faster too:
# @log.report('a','b','c', 'd') if @log.verbose?
# @log.report('a','b',"c #{d}") if @log.verbose?
# @log.report("a b c #{d}") if @log.verbose?
# The last three cases are equally fast when verbosity is turned off.
# Under consideration: verbose per instance
class Logger
@@length = 0
@@verbose = false
def initialize(tag=nil,length=0,verbose=false)
@tag = tag || ''
@@verbose = @@verbose || verbose
@@length = @tag.length if @tag.length > @@length
@@length = length if length > @@length
end
def report(*str)
begin
case str.length
when 0
print("\n")
return true
when 1
# message = str.first
message = str.first.join(' ')
else
message = [str].flatten.collect{|s| s.to_s}.join(' ').chomp
end
if @tag.empty? then
print("#{message}\n")
else
# try to avoid too many adjustments
@tag = @tag.ljust(@@length) unless @tag.length == @@length
print("#{@tag} | #{message}\n")
end
rescue
end
return true
end
def reportlines(*str)
unless @tag.empty? then
@tag = @tag.ljust(@@length) unless @tag.length == @@length
end
report([str].flatten.collect{|s| s.gsub(/\n/,"\n#{@tag} | ")}.join(' '))
end
def debug(*str)
report(str) if @@verbose
end
def error(*str)
if ! $! || $!.to_s.empty? then
report(str)
else
report(str,$!)
end
end
def verbose
@@verbose = true
end
def silent
@@verbose = false
end
def verbose?
@@verbose
end
# attr_reader :tag
# alias fatal error
# alias info debug
# alias warn debug
# alias debug? :verbose?
end
|