diff --git a/DEV.md b/DEV.md index 65b95a373..56895ff58 100644 --- a/DEV.md +++ b/DEV.md @@ -17,6 +17,7 @@ MetricFu.formatter_dir #=> metric_fu/lib/formatter MetricFu.reporting_require { 'result' } MetricFu.reporting_dir #=> metric_fu/lib/reporting MetricFu.logging_require { 'mf_debugger' } +MetricFu.lib_require { 'logger' } MetricFu.logging_dir #=> metric_fu/lib/logging MetricFu.errors_require { 'analysis_error' } MetricFu.errors_dir #=> metric_fu/lib/errors diff --git a/lib/metric_fu/configuration.rb b/lib/metric_fu/configuration.rb index 81235bbf4..a0a345a80 100644 --- a/lib/metric_fu/configuration.rb +++ b/lib/metric_fu/configuration.rb @@ -1,4 +1,4 @@ -MetricFu.logging_require { 'mf_debugger' } +MetricFu.lib_require { 'logger' } module MetricFu # Even though the below class methods are defined on the MetricFu module @@ -63,10 +63,6 @@ def initialize #:nodoc:# reset end - def mf_debug(msg) - MfDebugger.mf_debug msg - end - # TODO review if these code is functionally duplicated in the # base generator initialize attr_reader :formatters diff --git a/lib/metric_fu/environment.rb b/lib/metric_fu/environment.rb index 3a49f6a7e..0fed51166 100644 --- a/lib/metric_fu/environment.rb +++ b/lib/metric_fu/environment.rb @@ -1,16 +1,16 @@ require 'redcard' require 'rbconfig' -MetricFu.logging_require { 'mf_debugger' } +MetricFu.lib_require { 'logger' } module MetricFu module Environment # TODO: Set log_level here, instead def verbose - MfDebugger::Logger.debug_on + MetricFu.logger.debug_on end def verbose=(toggle) - MfDebugger::Logger.debug_on = toggle + MetricFu.logger.debug_on = toggle end # Perform a simple check to try and guess if we're running diff --git a/lib/metric_fu/gem_run.rb b/lib/metric_fu/gem_run.rb index fa5f8bea2..461db4352 100644 --- a/lib/metric_fu/gem_run.rb +++ b/lib/metric_fu/gem_run.rb @@ -2,7 +2,7 @@ require 'open3' require 'shellwords' require 'metric_fu' -MetricFu.lib_require { 'logging/mf_debugger' } +MetricFu.lib_require { 'logger' } MetricFu.lib_require { 'gem_version' } module MetricFu class GemRun diff --git a/lib/metric_fu/loader.rb b/lib/metric_fu/loader.rb index be8ffd8d2..aeff70710 100644 --- a/lib/metric_fu/loader.rb +++ b/lib/metric_fu/loader.rb @@ -72,9 +72,8 @@ def load_tasks(tasks_relative_path, options={task_name: ''}) end def setup - MetricFu.logging_require { 'mf_debugger' } - Object.send :include, MfDebugger - MfDebugger::Logger.debug_on = !!(ENV['MF_DEBUG'] =~ /true/i) + MetricFu.lib_require { 'logger' } + MetricFu.logger.debug_on = !!(ENV['MF_DEBUG'] =~ /true/i) MetricFu.lib_require { 'configuration' } MetricFu.lib_require { 'metric' } diff --git a/lib/metric_fu/logger.rb b/lib/metric_fu/logger.rb new file mode 100644 index 000000000..ca96b46a4 --- /dev/null +++ b/lib/metric_fu/logger.rb @@ -0,0 +1,65 @@ +require 'logger' +require 'forwardable' +module MetricFu + + def self.logger + @logger ||= ::MetricFu::Logger.new($stdout) + end + + class Logger + extend Forwardable + MfLogger = ::Logger + + def initialize(stdout) + @logger = MfLogger.new(stdout) + self.debug_on = false + self.formatter = ->(severity, time, progname, msg){ "#{msg}\n" } + self.level = 'info' + end + + def debug_on=(bool) + self.level = bool ? 'debug' : 'info' + end + + def debug_on + @logger.level == MfLogger::DEBUG + end + + def_delegators :@logger, :info, :warn, :error, :fatal, :unknown + + LEVELS = { + 'debug' => MfLogger::DEBUG, + 'info' => MfLogger::INFO, + 'warn' => MfLogger::WARN, + 'error' => MfLogger::ERROR, + 'fatal' => MfLogger::FATAL, + 'unknown' => MfLogger::UNKNOWN, + } + + def level=(level) + @logger.level = LEVELS.fetch(level.to_s.downcase) { level } + end + + def formatter=(formatter) + @logger.formatter = formatter + end + + def log(msg) + @logger.info '*'*5 + msg.to_s + end + + def debug(msg) + @logger.debug '*'*5 + msg.to_s + end + + end + +end +# For backward compatibility +def mf_debug(msg,&block) + MetricFu.logger.debug(msg, &block) +end + +def mf_log(msg,&block) + MetricFu.logger.log(msg, &block) +end diff --git a/lib/metric_fu/logging/mf_debugger.rb b/lib/metric_fu/logging/mf_debugger.rb index 3699ee1a5..3a4ba2025 100644 --- a/lib/metric_fu/logging/mf_debugger.rb +++ b/lib/metric_fu/logging/mf_debugger.rb @@ -1,58 +1,23 @@ +warn "MfDebugger if deprecated. Please use MetricFu.logger" +MetricFu.lib_require { 'logger' } module MfDebugger extend self - class Logger - class << self - attr_accessor :debug_on - @debug_on = false + def self.debug_on + warn "MfDebugger if deprecated. Please use MetricFu.logger" + MetricFu.logger.debug_on + end + def self.debug_on=(bool) + warn "MfDebugger if deprecated. Please use MetricFu.logger" + MetricFu.logger.level = bool ? 'debug' : 'info' end def self.log(msg, &block) - if block_given? - block.call - end - STDOUT.puts '*'*5 + msg.to_s + warn "MfDebugger if deprecated. Please use MetricFu.logger" + MetricFu.logger.info msg end def self.debug(msg, &block) - if MfDebugger::Logger.debug_on - if block_given? - log(msg,&block) - else - log(msg) - end - end - end - # From episode 029 of Ruby Tapas by Avdi - # https://rubytapas.dpdcart.com/subscriber/post?id=88 - def self.capture_output(stream=STDOUT, &block) - old_stdout = stream.clone - pipe_r, pipe_w = IO.pipe - pipe_r.sync = true - output = "" - reader = Thread.new do - begin - loop do - output << pipe_r.readpartial(1024) - end - rescue EOFError - end - end - stream.reopen(pipe_w) - yield - ensure - stream.reopen(old_stdout) - pipe_w.close - reader.join - pipe_r.close - return output + warn "MfDebugger if deprecated. Please use MetricFu.logger" + MetricFu.logger.debug msg end end - - def mf_debug(msg,&block) - MfDebugger::Logger.debug(msg, &block) - end - - def mf_log(msg,&block) - MfDebugger::Logger.log(msg, &block) - end - end diff --git a/lib/metric_fu/metrics/stats/stats.rb b/lib/metric_fu/metrics/stats/stats.rb index 1fafb9723..e7f7d6c55 100644 --- a/lib/metric_fu/metrics/stats/stats.rb +++ b/lib/metric_fu/metrics/stats/stats.rb @@ -8,7 +8,7 @@ def self.metric def emit require 'code_metrics/statistics' - @output = MfDebugger::Logger.capture_output do + @output = MetricFu::Utility.capture_output do CodeMetrics::Statistics.new(*dirs).to_s end end diff --git a/lib/metric_fu/utility.rb b/lib/metric_fu/utility.rb index ff7ffbc19..80a16b04f 100644 --- a/lib/metric_fu/utility.rb +++ b/lib/metric_fu/utility.rb @@ -43,5 +43,30 @@ def binread(file) File.binread(file) end + # From episode 029 of Ruby Tapas by Avdi + # https://rubytapas.dpdcart.com/subscriber/post?id=88 + def capture_output(stream=STDOUT, &block) + old_stdout = stream.clone + pipe_r, pipe_w = IO.pipe + pipe_r.sync = true + output = "" + reader = Thread.new do + begin + loop do + output << pipe_r.readpartial(1024) + end + rescue EOFError + end + end + stream.reopen(pipe_w) + yield + ensure + stream.reopen(old_stdout) + pipe_w.close + reader.join + pipe_r.close + return output + end + end end diff --git a/spec/metric_fu/formatter/yaml_spec.rb b/spec/metric_fu/formatter/yaml_spec.rb index bd4447a72..b7f92ae44 100644 --- a/spec/metric_fu/formatter/yaml_spec.rb +++ b/spec/metric_fu/formatter/yaml_spec.rb @@ -53,7 +53,7 @@ end it "creates a report yaml in the custom stream" do - out = MfDebugger::Logger.capture_output { + out = MetricFu::Utility.capture_output { MetricFu::Formatter::YAML.new(output: @output).finish } expect(out).to include ":#{@metric1}:" diff --git a/spec/metric_fu/run_spec.rb b/spec/metric_fu/run_spec.rb index ff5cf803c..060f111a1 100644 --- a/spec/metric_fu/run_spec.rb +++ b/spec/metric_fu/run_spec.rb @@ -191,7 +191,7 @@ def data_directory def metric_fu(options = "--no-open") message = '' - out = MfDebugger::Logger.capture_output { + out = MetricFu::Utility.capture_output { begin argv = Shellwords.shellwords(options) MetricFu::Cli::Client.new.run(argv) diff --git a/spec/support/usage_test.rb b/spec/support/usage_test.rb index 9f9b2612e..263cf5878 100644 --- a/spec/support/usage_test.rb +++ b/spec/support/usage_test.rb @@ -3,7 +3,6 @@ # puts "SUCCESS!" # Process.exit! 0 -require 'metric_fu/logging/mf_debugger' require 'open3' class UsageTest @@ -53,7 +52,6 @@ def in_test_directory end end SnippetRunner = Struct.new(:code, :language) do - include MfDebugger SystemCommandError = Class.new(StandardError) TestResult = Struct.new(:success, :captured_output) @@ -123,9 +121,9 @@ def capture_output(fail_on_empty) exception = nil stderr = :not_set stdout = :not_set - MfDebugger::Logger.capture_output(STDOUT) do + MetricFu::Utility.capture_output(STDOUT) do stdout = - MfDebugger::Logger.capture_output(STDERR) do + MetricFu::Utility.capture_output(STDERR) do begin stderr = yield rescue Exception => e