Class: Narou::StreamingLogger

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

Constant Summary

Constants included from LoggerModule

LoggerModule::LOG_FORMAT_FILENAME, LoggerModule::LOG_FORMAT_TIMESTAMP

Instance Attribute Summary collapse

Attributes included from LoggerModule

#capturing, #format_filename, #format_timestamp, #format_timestamp_disabled, #log_postfix, #logging_enabled, #stream

Instance Method Summary collapse

Methods included from LoggerModule

#append_log, #capture, #create_log_dir, #disable_logging, #dup_with_disabled_logging, #embed_timestamp, #error, included, #init_logs, #log_filename, #log_filepath, #logging?, #save, #silence, #silent=, #silent?, #warn, #write_base, #write_console

Constructor Details

#initialize(push_server, original_stream = $stdout, target_console: "stdout") ⇒ StreamingLogger

Returns a new instance of StreamingLogger.



32
33
34
35
36
37
38
# File 'lib/web/streaminglogger.rb', line 32

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

Instance Attribute Details

#push_serverObject (readonly)

Returns the value of attribute push_server.



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

def push_server
  @push_server
end

#target_consoleObject (readonly)

Returns the value of attribute target_console.



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

def target_console
  @target_console
end

Instance Method Details

#build_echo(str, no_history) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/web/streaminglogger.rb', line 63

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

#copy_instanceObject



44
45
46
47
48
# File 'lib/web/streaminglogger.rb', line 44

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

#push_streaming(str, no_history: false) ⇒ Object



58
59
60
61
# File 'lib/web/streaminglogger.rb', line 58

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

#strip_color(str) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/web/streaminglogger.rb', line 50

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

#tty?Boolean

Returns:

  • (Boolean)


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

def tty?
  false
end

#write(str) ⇒ Object



71
72
73
74
75
76
77
78
79
# File 'lib/web/streaminglogger.rb', line 71

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