Class: Pindo::TaskSystem::CustomIO

Inherits:
Object
  • Object
show all
Defined in:
lib/pindo/module/task/output/stdout_redirector.rb

Overview

自定义 IO 对象拦截所有 puts/print 输出,将其路由到输出管理器

Instance Method Summary collapse

Constructor Details

#initialize(task_id, output_manager, stream_type) ⇒ CustomIO

初始化自定义 IO



36
37
38
39
40
# File 'lib/pindo/module/task/output/stdout_redirector.rb', line 36

def initialize(task_id, output_manager, stream_type)
  @task_id = task_id
  @output_manager = output_manager
  @stream_type = stream_type
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object

委托其他方法给 STDOUT



82
83
84
# File 'lib/pindo/module/task/output/stdout_redirector.rb', line 82

def method_missing(method, *args, &block)
  STDOUT.send(method, *args, &block)
end

Instance Method Details

#flushObject

flush 方法(无操作)



77
78
79
# File 'lib/pindo/module/task/output/stdout_redirector.rb', line 77

def flush
  # 不需要处理
end

print 方法



71
72
73
74
# File 'lib/pindo/module/task/output/stdout_redirector.rb', line 71

def print(*args)
  write(args.join)
  nil
end

#puts(*args) ⇒ Object

puts 方法



60
61
62
63
64
65
66
67
# File 'lib/pindo/module/task/output/stdout_redirector.rb', line 60

def puts(*args)
  if args.empty?
    write("\n")
  else
    args.each { |arg| write("#{arg}\n") }
  end
  nil
end

#respond_to_missing?(method, include_private = false) ⇒ Boolean

响应方法检查



90
91
92
# File 'lib/pindo/module/task/output/stdout_redirector.rb', line 90

def respond_to_missing?(method, include_private = false)
  STDOUT.respond_to?(method, include_private) || super
end

#write(message) ⇒ Integer

写入数据



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/pindo/module/task/output/stdout_redirector.rb', line 45

def write(message)
  # 捕获输出,写入日志文件(跳过空行)
  unless message.strip.empty?
    # 根据流类型选择日志级别
    if @stream_type == :stderr
      @output_manager.log_error(@task_id, message.chomp)
    else
      @output_manager.log_detail(@task_id, message.chomp)
    end
  end
  message.length
end