<commit_before>module MfDebugger
  extend self

  class Logger
    class << self
      attr_accessor :debug_on
      @debug_on = false
    end
    def self.log(msg, &block)
      if block_given?
        block.call
      end
      STDOUT.puts '*'*5 + msg.to_s
    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(&block)
      old_stdout = STDOUT.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
      STDOUT.reopen(pipe_w)
      yield
    ensure
      STDOUT.reopen(old_stdout)
      pipe_w.close
      reader.join
      return output
    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<commit_msg>[Fix] Close read pipe when capturing output, per @eclubb, <NAME><commit_after>module MfDebugger
  extend self

  class Logger
    class << self
      attr_accessor :debug_on
      @debug_on = false
    end
    def self.log(msg, &block)
      if block_given?
        block.call
      end
      STDOUT.puts '*'*5 + msg.to_s
    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(&block)
      old_stdout = STDOUT.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
      STDOUT.reopen(pipe_w)
      yield
    ensure
      STDOUT.reopen(old_stdout)
      pipe_w.close
      reader.join
      pipe_r.close
      return output
    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
{'fraction_non_alphanumeric': 0.08408163265306122, 'fraction_numerical': 0.00816326530612245, 'mean_word_length': 1.8043478260869565, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 4, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2631547', 'n_tokens_mistral': 850, 'n_tokens_neox': 827, 'n_words': 189}