C0 code coverage information
Generated on Sat Feb 02 17:44:27 +0100 2008 with rcov 0.8.1.2
Code reported as executed by Ruby looks like this...
and this: this line is also marked as covered.
Lines considered as run by rcov, but not reported by Ruby, look like this,
and this: these lines were inferred by rcov (using simple heuristics).
Finally, here's a line marked as not executed.
1 # Copyright (c) 2008 Michael Fellinger m.fellinger@gmail.com
2 # All files in this distribution are subject to the terms of the Ruby license.
3
4 module Ramaze
5
6 # A minimal logger for Ramaze, supports files, CLI, colors and some
7 # customization.
8
9 class Informer
10 include Informing
11
12 attr_accessor :out, :colorize, :log_levels
13
14 # Should Ramaze try to use colors?
15 trait :colorize => true
16
17 # parameter for Time.now.strftime
18 trait :timestamp => "%Y-%m-%d %H:%M:%S"
19
20 # This is how the final output is arranged.
21 trait :format => "[%time] %prefix %text"
22
23 # Which tag should be in what color
24 COLORS = {
25 :dev => :blue,
26 :debug => :yellow,
27 :info => :green,
28 :warn => :red,
29 :error => :red,
30 }
31
32 # Create a new instance of Informer.
33 # You can spcify
34 #
35 # Examples:
36 # Informer.new #=> logs to stdout with all levels being
37 # shown.
38 # Informer.new($stderr) #=> same, but to stderr
39 # Informer.new("foo.log") #=> same, but logs to the file foo.log
40 # (or creates it if it doesn't exist yet)
41 # Informer.new($stdout, [:info]) #=> show only #info messages to stdout.
42
43 def initialize(out = $stdout, log_levels = [:debug, :error, :info, :warn])
44 @colorize = false
45
46 @out =
47 case out
48 when STDOUT, :stdout, 'stdout'
49 $stdout
50 when STDERR, :stderr, 'stderr'
51 $stderr
52 when IO
53 out
54 else
55 if out.respond_to?(:puts)
56 out
57 else
58 File.open(out.to_s, 'ab+')
59 end
60 end
61
62 if @out.respond_to?(:tty?) and class_trait[:colorize]
63 @colorize = @out.tty?
64 end
65
66 @log_levels = log_levels
67 end
68
69 # Close the file we log to if it isn't closed already.
70
71 def shutdown
72 if @out.respond_to?(:close)
73 Inform.debug("close, #{@out.inspect}")
74 @out.close
75 end
76 end
77
78 # Integration to Informing.
79
80 def inform tag, *messages
81 return if closed? || !@log_levels.include?(tag)
82 messages.flatten!
83
84 prefix = tag.to_s.upcase.ljust(5)
85
86 if @colorize
87 color = COLORS[tag] ||= :white
88 prefix.replace prefix.send(color)
89 end
90
91 messages.each do |message|
92 @out.puts(log_interpolate(prefix, message))
93 end
94
95 @out.flush if @out.respond_to?(:flush)
96 end
97
98 # Takes the prefix (tag), text and timestamp and applies it to
99 # the :format trait.
100
101 def log_interpolate prefix, text, time = timestamp
102 message = class_trait[:format].dup
103
104 vars = { '%time' => time, '%prefix' => prefix, '%text' => text }
105 vars.each{|from, to| message.gsub!(from, to) }
106
107 message
108 end
109
110 # This uses Global.inform_timestamp or a date in the format of
111 # %Y-%m-%d %H:%M:%S
112 # # => "2007-01-19 21:09:32"
113
114 def timestamp
115 mask = class_trait[:timestamp]
116 Time.now.strftime(mask || "%Y-%m-%d %H:%M:%S")
117 end
118
119 # is @out closed?
120
121 def closed?
122 @out.respond_to?(:closed?) and @out.closed?
123 end
124 end
125 end
Generated using the rcov code coverage analysis tool for Ruby version 0.8.1.2.