Class: Narou::StreamingLogger

Inherits:
StringIO
  • Object
show all
Includes:
LoggerModule
Defined in:
lib/web/streaminglogger.rb

Overview

標準出力をフックする

$stdout を介した出力は echo イベントとして全てのクライアントへ送信される

Instance Attribute Summary collapse

Attributes included from LoggerModule

#capturing

Instance Method Summary collapse

Methods included from LoggerModule

#capture, #error, #save, #silence, #silent, #silent=, #warn, #write_base, #write_console

Constructor Details

#initialize(push_server = nil) ⇒ StreamingLogger

Returns a new instance of StreamingLogger.



30
31
32
33
# File 'lib/web/streaminglogger.rb', line 30

def initialize(push_server = nil)
  super()
  @push_server = push_server
end

Instance Attribute Details

#push_serverObject (readonly)

Returns the value of attribute push_server.



28
29
30
# File 'lib/web/streaminglogger.rb', line 28

def push_server
  @push_server
end

Instance Method Details

#copy_instanceObject



39
40
41
42
43
# File 'lib/web/streaminglogger.rb', line 39

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

#push_streaming(str) ⇒ Object



53
54
55
56
57
# File 'lib/web/streaminglogger.rb', line 53

def push_streaming(str)
  unless @is_silent
    @push_server.send_all(echo: str) if @push_server
  end
end

#strip_color(str) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/web/streaminglogger.rb', line 45

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

#tty?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/web/streaminglogger.rb', line 35

def tty?
  false
end

#write(str) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/web/streaminglogger.rb', line 59

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)
end