Sha256: aa193cd923f7c04ef0405bf95ac236e94223d09025ccbd3025899be9aa9b9165

Contents?: true

Size: 1.6 KB

Versions: 25

Compression:

Stored size: 1.6 KB

Contents

# frozen_string_literal: true

#
# Copyright 2013 whiteleaf. All rights reserved.
#

class String
  if $disable_color
    def termcolor
      TermColorLight.strip_tag(self, false)
    end
  else
    require "termcolorlight/html"

    def termcolor
      TermColorLight.to_html(self)
    end
  end
end

#
# 標準出力をフックする
#
# $stdout を介した出力は echo イベントとして全てのクライアントへ送信される
#
module Narou
  class StreamingLogger < StringIO
    include Narou::LoggerModule

    attr_reader :push_server, :target_console

    def initialize(push_server, original_stream = $stdout, target_console: "stdout")
      super()
      @push_server = push_server
      @target_console = target_console
      self.log_postfix = original_stream.log_postfix
      original_stream.string.clear
    end

    def tty?
      false
    end

    def copy_instance
      self.class.new(@push_server).tap do |obj|
        obj.silent = silent?
      end
    end

    def strip_color(str)
      if $disable_color
        str
      else
        str.gsub(%r!</?span.*?>!, "")
      end
    end

    def push_streaming(str, no_history: false)
      return if silent?
      @push_server&.send_all(echo: build_echo(str, no_history))
    end

    def build_echo(str, no_history)
      {
        target_console: target_console,
        body: str,
        no_history: no_history
      }
    end

    def write(str)
      str = str.to_s
      if str.encoding == Encoding::ASCII_8BIT
        str.force_encoding(Encoding::UTF_8)
      end
      super(str)
      push_streaming(str)
      append_log(str)
    end
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
narou-3.9.1 lib/web/streaminglogger.rb
narou-3.9.0 lib/web/streaminglogger.rb
narou-3.8.2 lib/web/streaminglogger.rb
narou-3.8.1 lib/web/streaminglogger.rb
narou-3.8.0 lib/web/streaminglogger.rb
narou-3.7.2 lib/web/streaminglogger.rb
narou-3.7.1 lib/web/streaminglogger.rb
narou-3.7.0 lib/web/streaminglogger.rb
narou-3.6.0 lib/web/streaminglogger.rb
narou-3.5.1 lib/web/streaminglogger.rb
narou-3.5.0.1 lib/web/streaminglogger.rb
narou-3.5.0 lib/web/streaminglogger.rb
narou-3.4.8 lib/web/streaminglogger.rb
narou-3.4.7.1 lib/web/streaminglogger.rb
narou-3.4.7 lib/web/streaminglogger.rb
narou-3.4.6.1 lib/web/streaminglogger.rb
narou-3.4.6 lib/web/streaminglogger.rb
narou-3.4.5 lib/web/streaminglogger.rb
narou-3.4.3 lib/web/streaminglogger.rb
narou-3.4.2 lib/web/streaminglogger.rb