Class: Pindo::TaskSystem::CustomIO
- Inherits:
-
Object
- Object
- Pindo::TaskSystem::CustomIO
- Defined in:
- lib/pindo/module/task/output/stdout_redirector.rb
Overview
自定义 IO 对象拦截所有 puts/print 输出,将其路由到输出管理器
Instance Method Summary collapse
-
#ensure_utf8(str) ⇒ String
确保字符串使用 UTF-8 编码.
-
#flush ⇒ Object
flush 方法(无操作).
-
#initialize(task_id, output_manager, stream_type) ⇒ CustomIO
constructor
初始化自定义 IO.
-
#method_missing(method, *args, &block) ⇒ Object
委托其他方法给 STDOUT.
-
#print(*args) ⇒ Object
print 方法.
-
#puts(*args) ⇒ Object
puts 方法.
-
#respond_to_missing?(method, include_private = false) ⇒ Boolean
响应方法检查.
-
#write(message) ⇒ Integer
写入数据.
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
100 101 102 |
# File 'lib/pindo/module/task/output/stdout_redirector.rb', line 100 def method_missing(method, *args, &block) STDOUT.send(method, *args, &block) end |
Instance Method Details
#ensure_utf8(str) ⇒ String
确保字符串使用 UTF-8 编码
63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/pindo/module/task/output/stdout_redirector.rb', line 63 def ensure_utf8(str) return str if str.nil? return str if str.encoding == Encoding::UTF_8 && str.valid_encoding? # 尝试转换为 UTF-8 begin str.encode(Encoding::UTF_8) rescue Encoding::InvalidByteSequenceError, Encoding::UndefinedConversionError # 如果转换失败,尝试使用替换字符 str.encode(Encoding::UTF_8, invalid: :replace, undef: :replace) end end |
#flush ⇒ Object
flush 方法(无操作)
95 96 97 |
# File 'lib/pindo/module/task/output/stdout_redirector.rb', line 95 def flush # 不需要处理 end |
#print(*args) ⇒ Object
print 方法
89 90 91 92 |
# File 'lib/pindo/module/task/output/stdout_redirector.rb', line 89 def print(*args) write(ensure_utf8(args.join)) nil end |
#puts(*args) ⇒ Object
puts 方法
78 79 80 81 82 83 84 85 |
# File 'lib/pindo/module/task/output/stdout_redirector.rb', line 78 def puts(*args) if args.empty? write("\n") else args.each { |arg| write("#{ensure_utf8(arg.to_s)}\n") } end nil end |
#respond_to_missing?(method, include_private = false) ⇒ Boolean
响应方法检查
108 109 110 |
# File 'lib/pindo/module/task/output/stdout_redirector.rb', line 108 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 57 58 |
# File 'lib/pindo/module/task/output/stdout_redirector.rb', line 45 def write() # 捕获输出,写入日志文件(跳过空行) unless .strip.empty? # 确保消息使用 UTF-8 编码,防止编码错误导致任务中断 = ensure_utf8(.chomp) # 根据流类型选择日志级别 if @stream_type == :stderr @output_manager.log_error(@task_id, ) else @output_manager.log_detail(@task_id, ) end end .length end |