Codebase list facter / dce8f57
Merge branch 'ticket/master/4552' into next Conflicts: bin/facter lib/facter/application.rb Rein Henrichs 13 years ago
5 changed file(s) with 70 addition(s) and 1 deletion(s). Raw diff Collapse all Expand all
3535 # trace::
3636 # Enable backtraces.
3737 #
38 # timing::
39 # Enable timing.
40 #
3841 # = Example
3942 #
4043 # facter kernel
5858 OptionParser.new do |opts|
5959 opts.on("-y", "--yaml") { |v| options[:yaml] = v }
6060 opts.on( "--trace") { |v| options[:trace] = v }
61
6261 opts.on("-d", "--debug") { |v| Facter.debugging(1) }
62 opts.on("-t", "--timing") { |v| Facter.timing(1) }
6363 opts.on("-p", "--puppet") { |v| load_puppet }
6464
6565 opts.on_tail("-v", "--version") do
7474 puts RDoc.usage
7575 ensure
7676 exit
77 rescue LoadError
78 $stderr.puts "No help available unless your RDoc has RDoc.usage"
79 exit(1)
80 rescue => e
81 $stderr.puts "fatal: #{e}"
82 exit(1)
7783 end
7884 end
7985 end.parse!
134134 def value
135135 result = nil
136136 return result if @code == nil and @interpreter == nil
137
138 starttime = Time.now.to_f
139
137140 begin
138141 Timeout.timeout(limit) do
139142 if @code.is_a?(Proc)
155158 return nil
156159 end
157160
161 finishtime = Time.now.to_f
162 ms = (finishtime - starttime) * 1000
163 Facter.show_time "#{self.name}: #{"%.2f" % ms}ms"
164
158165 return nil if result == ""
159166 return result
160167 end
4747 GREEN = ""
4848 RESET = ""
4949 @@debug = 0
50 @@timing = 0
5051
5152 # module methods
5253
7475
7576 def self.debugging?
7677 @@debug != 0
78 end
79
80 # show the timing information
81 def self.show_time(string)
82 puts "#{GREEN}#{string}#{RESET}" if string and Facter.timing?
83 end
84
85 def self.timing?
86 @@timing != 0
7787 end
7888
7989 # Return a fact object by name. If you use this, you still have to call
176186 end
177187 end
178188
189 # Set timing on or off.
190 def self.timing(bit)
191 if bit
192 case bit
193 when TrueClass; @@timing = 1
194 when Fixnum
195 if bit > 0
196 @@timing = 1
197 else
198 @@timing = 0
199 end
200 end
201 else
202 @@timing = 0
203 end
204 end
179205
180206 def self.warn(msg)
181207 if Facter.debugging? and msg and not msg.empty?
134134 Facter.should respond_to(:debugging?)
135135 end
136136
137 it "should have a method to query timing mode" do
138 Facter.should respond_to(:timing?)
139 end
140
141 it "should have a method to show timing information" do
142 Facter.should respond_to(:show_time)
143 end
144
137145 it "should have a method to warn" do
138146 Facter.should respond_to(:warn)
139147 end
203211 end
204212 end
205213
214 describe "when setting timing mode" do
215 it "should have timing enabled using 1" do
216 Facter.timing(1)
217 Facter.should be_timing
218 end
219 it "should have timing enabled using true" do
220 Facter.timing(true)
221 Facter.should be_timing
222 end
223 it "should have timing disabled using 0" do
224 Facter.timing(0)
225 Facter.should_not be_timing
226 end
227 it "should have timing disabled using false" do
228 Facter.timing(false)
229 Facter.should_not be_timing
230 end
231 end
232
206233 describe "when registering directories to search" do
207234 after { Facter.instance_variable_set("@search_path", []) }
208235