Skip to content

Commit

Permalink
Use a real logger. Maintain compatibility with MfDebugger
Browse files Browse the repository at this point in the history
For 5.0, see #113
  • Loading branch information
bf4 committed Apr 27, 2014
1 parent 5973595 commit 4c61fe8
Show file tree
Hide file tree
Showing 12 changed files with 116 additions and 67 deletions.
1 change: 1 addition & 0 deletions DEV.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 1 addition & 5 deletions lib/metric_fu/configuration.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions lib/metric_fu/environment.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/metric_fu/gem_run.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions lib/metric_fu/loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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' }
Expand Down
65 changes: 65 additions & 0 deletions lib/metric_fu/logger.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
require 'logger'
require 'forwardable'
module MetricFu

def self.logger
@logger ||= ::MetricFu::Logger.new($stdout, $stderr)
end

class Logger
extend Forwardable
MfLogger = ::Logger

def initialize(stdout, stderr)
@logger = MfLogger.new(stdout, stderr)
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
61 changes: 13 additions & 48 deletions lib/metric_fu/logging/mf_debugger.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion lib/metric_fu/metrics/stats/stats.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 25 additions & 0 deletions lib/metric_fu/utility.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion spec/metric_fu/formatter/yaml_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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}:"
Expand Down
2 changes: 1 addition & 1 deletion spec/metric_fu/run_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 2 additions & 4 deletions spec/support/usage_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

# puts "SUCCESS!"
# Process.exit! 0
require 'metric_fu/logging/mf_debugger'
require 'open3'

class UsageTest
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 4c61fe8

Please sign in to comment.