<commit_before>
module Bixby
  module ThreadDump

    class LoggerIO
      def initialize(logger)
        @logger = logger
      end
      def puts(str="")
        @logger.warn(str)
      end
    end

    class << self

      # Prints a thread dump on ALRM signal
      # kill -ALRM <pid>
      #
      # You can cancel the signal trap by saving the returned Thread and killing it
      #
      # @return [Thread]
      def trap!
        t = Bixby::Signal.trap("SIGALRM") do
          write(LoggerIO.new(Logging.logger[ThreadDump]))
        end
        t[:_name] = "dumper [ignore me]"

        Logging.logger[ThreadDump].info "Trapping SIGALRM: kill -ALRM #{Process.pid}"
        return t
      end

      # Write thread dump to the given IO-like handle (must respond to #puts)
      #
      # @param [IO] io
      def write(io=STDERR)
        out = []
        out << "=== thread dump for #{$0} (pid=#{Process.pid}): #{Time.now} ==="
        out << ""

        Thread.list.each do |thread|

          # compute thread name, filling in some common libraries
          n = thread.inspect
          if thread.key?(:_name) then
            n += " #{thread[:_name]}"<commit_msg>[Bixby::ThreadDump] default to STDERR in case 'logging' gem is not avail<commit_after>
module Bixby
  module ThreadDump

    class LoggerIO
      def initialize(logger)
        @logger = logger
      end
      def puts(str="")
        @logger.warn(str)
      end
    end

    class << self

      # Prints a thread dump on ALRM signal
      # kill -ALRM <pid>
      #
      # You can cancel the signal trap by saving the returned Thread and killing it
      # @param [IO] logger        IO-like destination for thread dumps (responds to #puts)
      #
      # @return [Thread]
      def trap!(logger=nil)

        if logger.nil? then
          logger = STDERR
        elsif Module.const_defined?(:Logging) && logger.kind_of?(Logging::Logger) then
          logger = LoggerIO.new(logger)
        end

        t = Bixby::Signal.trap("SIGALRM") do
          write(logger)
        end
        t[:_name] = "dumper [ignore me]"

        logger.puts("Trapping SIGALRM: kill -ALRM #{Process.pid}")
        return t
      end

      # Write thread dump to the given IO-like handle (must respond to #puts)
      #
      # @param [IO] io
      def write(io=STDERR)
        out = []
        out << "=== thread dump for #{$0} (pid=#{Process.pid}): #{Time.now} ==="
        out << ""

        Thread.list.each do |thread|

          # compute thread name, filling in some common libraries
          n = thread.inspect
          if thread.key?(:_name) then
            n += " #{thread[:_name]}"
{'fraction_non_alphanumeric': 0.1138763197586727, 'fraction_numerical': 0.0007541478129713424, 'mean_word_length': 1.921806167400881, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 5, 'https://': 0, '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': '6702309', 'n_tokens_mistral': 816, 'n_tokens_neox': 795, 'n_words': 249}