C0 code coverage information
Generated on Tue Oct 16 11:40:49 -0400 2007 with rcov 0.8.0
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) 2007 Cathal Mc Ginley
2 #
3 # Alexandria is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU General Public License as
5 # published by the Free Software Foundation; either version 2 of the
6 # License, or (at your option) any later version.
7 #
8 # Alexandria is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 # General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public
14 # License along with Alexandria; see the file COPYING. If not,
15 # write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 # Boston, MA 02111-1307, USA.
17
18 require 'logger'
19 require 'forwardable'
20
21 module Alexandria
22
23 # A Logger subclass which accepts a source for log messages
24 # in order to improve legibility of the logs.
25 # The source should usually be +self+, whether that be a Class, Module
26 # or Object. A LoggerWrapper can be used to simplify this procedure.
27 class Logger < ::Logger
28
29 def add(severity, message = nil, source=nil, progname = nil, &block)
30 if source.nil?
31 return super(severity, message, progname, &block)
32 end
33 category = self.class.category(source)
34 if not block_given?
35 return super(severity, progname, category)
36 end
37 category = "#{category} #{progname}" if progname
38 return super(severity, message, category, &block)
39 end
40
41 def debug(source=nil, progname = nil, &block)
42 add(DEBUG, nil, source, progname, &block)
43 end
44
45 def info(source=nil, progname = nil, &block)
46 add(INFO, nil, source, progname, &block)
47 end
48
49 def warn(source=nil, progname = nil, &block)
50 add(WARN, nil, source, progname, &block)
51 end
52
53 def error(source=nil, progname = nil, &block)
54 add(ERROR, nil, source, progname, &block)
55 end
56
57 def fatal(source=nil, progname = nil, &block)
58 add(FATAL, nil, source, progname, &block)
59 end
60
61 private
62
63 def self.category(source)
64 if source.instance_of? Class
65 "[Cls #{source.name}]"
66 elsif source.instance_of? Module
67 "[Mod #{source.name}]"
68 else
69 "<Obj #{source.class.name}>"
70 end
71 end
72
73 end
74
75 # A wrapper around a Logger, which allows code to define the source
76 # once (in the wrapper's initialization) and then call the log methods
77 # whithout needing to specify the source each time.
78 class LogWrapper
79 extend Forwardable
80 def initialize(logger, source)
81 @logger = logger
82 @source = source
83 end
84
85 def_delegators :@logger, :debug?, :info?, :warn?, :error?, :fatal?
86
87 def <<(msg)
88 if msg.respond_to? :backtrace
89 msg.backtrace.each { |line|
90 @logger << " #{line} \n"
91 }
92 else
93 @logger << msg + "\n"
94 end
95 end
96 def debug(progname = nil, &block)
97 @logger.debug(@source, progname, &block)
98 end
99 def info(progname = nil, &block)
100 @logger.info(@source, progname, &block)
101 end
102 def warn(progname = nil, &block)
103 @logger.warn(@source, progname, &block)
104 end
105 def error(progname = nil, &block)
106 @logger.error(@source, progname, &block)
107 end
108 def fatal(progname = nil, &block)
109 @logger.fatal(@source, progname, &block)
110 end
111
112 end
113
114 # A mixin to include a +log+ instance method for objects or a
115 # static +log+ method for classes and modules. In either case, a
116 # +LogWrapper+ is returned which wraps the Alexandria log and
117 # specifies the appropriate source object, class or module.
118 module Logging
119
120 module ClassMethods
121 def log
122 if not @log_wrapper
123 @log_wrapper = LogWrapper.new(Alexandria.log, self)
124 end
125 @log_wrapper
126 end
127 end
128
129 def self.included(base)
130 base.extend(ClassMethods)
131 end
132
133 def log
134 if not @log_wrapper
135 @log_wrapper = LogWrapper.new(Alexandria.log, self)
136 end
137 @log_wrapper
138 end
139 end
140
141 private
142
143 # Creates the Logger for Alexandria
144 def self.create_logger
145 logger = Alexandria::Logger.new(STDERR)
146
147 level = ENV['LOGLEVEL'] ? ENV['LOGLEVEL'].intern : nil
148 if [:FATAL, :ERROR, :WARN, :INFO, :DEBUG].include? level
149 logger.level = Logger.const_get(level)
150 else
151 logger.level = Logger::WARN # default level
152 logger.warn(self, "Unknown LOGLEVEL '#{level}'; using WARN") if level
153 end
154
155 logger
156 end
157
158 @@logger = self.create_logger
159
160 public
161
162 # Returns the Logger for Alexandria
163 def self.log
164 @@logger
165 end
166
167 end
Generated using the rcov code coverage analysis tool for Ruby version 0.8.0.