Class: Pindo::Funlog
- Inherits:
-
Object
- Object
- Pindo::Funlog
- Defined in:
- lib/pindo/base/funlog.rb
Defined Under Namespace
Modules: Mixin
Singleton collapse
Singleton collapse
-
.current_output_sink ⇒ OutputSink?
获取当前线程的输出接收器.
-
.current_task_id ⇒ String?
获取当前线程的任务 ID.
- .error(*args) ⇒ Object
- .fancyinfo_error(*args) ⇒ Object
-
.fancyinfo_start(*args) ⇒ Object
类方法委托,简化调用方式 使用 Funlog.info(“消息”) 代替 Funlog.instance.info(“消息”) ============================================.
- .fancyinfo_success(*args) ⇒ Object
- .fancyinfo_update(*args) ⇒ Object
- .fancyinfo_warning(*args) ⇒ Object
-
.in_task_context? ⇒ Boolean
检查是否在任务上下文中.
- .info(*args) ⇒ Object
-
.reload_instance ⇒ Config
The current config instance creating one if needed.
- .warning(*args) ⇒ Object
Class Method Summary collapse
-
.ensure_utf8(str) ⇒ String
确保字符串使用 UTF-8 编码,防止编码错误导致任务中断.
Instance Method Summary collapse
- #create_spinner(info_key: nil) ⇒ Object
-
#error(*args) ⇒ Object
输出静态错误信息(不使用spinner,对应 fancyinfo_error).
- #fancyinfo_error(*args) ⇒ Object
- #fancyinfo_start(*args) ⇒ Object
- #fancyinfo_success(*args) ⇒ Object
- #fancyinfo_update(*args) ⇒ Object
-
#fancyinfo_warning(*args) ⇒ Object
输出警告信息(使用spinner,黄色警告标记).
-
#info(*args) ⇒ Object
输出静态成功信息(不使用spinner,对应 fancyinfo_success).
- #spinner_log_handle ⇒ Object
-
#warning(*args) ⇒ Object
输出静态警告信息(不使用spinner).
Class Attribute Details
.instance ⇒ Object
152 153 154 |
# File 'lib/pindo/base/funlog.rb', line 152 def self.instance @instance ||= new end |
Class Method Details
.current_output_sink ⇒ OutputSink?
获取当前线程的输出接收器
172 173 174 |
# File 'lib/pindo/base/funlog.rb', line 172 def current_output_sink Thread.current[:output_sink] end |
.current_task_id ⇒ String?
获取当前线程的任务 ID
178 179 180 |
# File 'lib/pindo/base/funlog.rb', line 178 def current_task_id Thread.current[:task_id] end |
.ensure_utf8(str) ⇒ String
确保字符串使用 UTF-8 编码,防止编码错误导致任务中断
13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/pindo/base/funlog.rb', line 13 def self.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 |
.error(*args) ⇒ Object
221 222 223 |
# File 'lib/pindo/base/funlog.rb', line 221 def error(*args) instance.error(*args) end |
.fancyinfo_error(*args) ⇒ Object
205 206 207 |
# File 'lib/pindo/base/funlog.rb', line 205 def (*args) instance.(*args) end |
.fancyinfo_start(*args) ⇒ Object
类方法委托,简化调用方式使用 Funlog.info(“消息”) 代替 Funlog.instance.info(“消息”)
193 194 195 |
# File 'lib/pindo/base/funlog.rb', line 193 def (*args) instance.(*args) end |
.fancyinfo_success(*args) ⇒ Object
201 202 203 |
# File 'lib/pindo/base/funlog.rb', line 201 def (*args) instance.(*args) end |
.fancyinfo_update(*args) ⇒ Object
197 198 199 |
# File 'lib/pindo/base/funlog.rb', line 197 def (*args) instance.(*args) end |
.fancyinfo_warning(*args) ⇒ Object
209 210 211 |
# File 'lib/pindo/base/funlog.rb', line 209 def (*args) instance.(*args) end |
.in_task_context? ⇒ Boolean
检查是否在任务上下文中
184 185 186 |
# File 'lib/pindo/base/funlog.rb', line 184 def in_task_context? !current_output_sink.nil? && !current_task_id.nil? end |
.info(*args) ⇒ Object
213 214 215 |
# File 'lib/pindo/base/funlog.rb', line 213 def info(*args) instance.info(*args) end |
.reload_instance ⇒ Config
Returns the current config instance creating one if needed.
148 149 150 |
# File 'lib/pindo/base/funlog.rb', line 148 def self.reload_instance @instance = new end |
.warning(*args) ⇒ Object
217 218 219 |
# File 'lib/pindo/base/funlog.rb', line 217 def warning(*args) instance.warning(*args) end |
Instance Method Details
#create_spinner(info_key: nil) ⇒ Object
26 27 28 29 30 |
# File 'lib/pindo/base/funlog.rb', line 26 def create_spinner(info_key:nil) # spinner = TTY::Spinner.new("[:spinner] :#{info_key}", format: :dots_2, error_mark: "❌", success_mark: "✅") spinner = TTY::Spinner.new("[:spinner] :#{info_key}", format: :dots_2, error_mark: "❌") spinner end |
#error(*args) ⇒ Object
输出静态错误信息(不使用spinner,对应 fancyinfo_error)
129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/pindo/base/funlog.rb', line 129 def error(*args) = Funlog.ensure_utf8(args.join(" ")) # 检查是否在任务上下文中 if Funlog.in_task_context? # 并发模式:路由到输出管理器 Funlog.current_output_sink.log_error(Funlog.current_task_id, ) else # 串行模式:保持原有行为 puts "\e[31m ✗ #{}\e[0m" end end |
#fancyinfo_error(*args) ⇒ Object
81 82 83 84 85 86 |
# File 'lib/pindo/base/funlog.rb', line 81 def (*args) = Funlog.ensure_utf8(args.join(" ")) spinner_log_handle.update(title:) spinner_log_handle.error @spinner_log =nil end |
#fancyinfo_start(*args) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/pindo/base/funlog.rb', line 41 def (*args) = Funlog.ensure_utf8(args.join(" ")) # 检查是否在任务上下文中 if Funlog.in_task_context? # 并发模式:路由到输出管理器(只写日志文件) Funlog.current_output_sink.log_info(Funlog.current_task_id, ) else # 串行模式:使用 spinner spinner_log_handle.update(title:) spinner_log_handle.auto_spin end @spinner_log end |
#fancyinfo_success(*args) ⇒ Object
74 75 76 77 78 79 |
# File 'lib/pindo/base/funlog.rb', line 74 def (*args) = Funlog.ensure_utf8(args.join(" ")) spinner_log_handle.update(title:) spinner_log_handle.success @spinner_log =nil end |
#fancyinfo_update(*args) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/pindo/base/funlog.rb', line 58 def (*args) = Funlog.ensure_utf8(args.join(" ")) # 检查是否在任务上下文中 if Funlog.in_task_context? # 并发模式:不更新终端,只写日志 Funlog.current_output_sink.log_info(Funlog.current_task_id, ) else # 串行模式:使用 spinner spinner_log_handle.update(title:) spinner_log_handle.spin end @spinner_log end |
#fancyinfo_warning(*args) ⇒ Object
输出警告信息(使用spinner,黄色警告标记)
90 91 92 93 94 95 |
# File 'lib/pindo/base/funlog.rb', line 90 def (*args) = Funlog.ensure_utf8(args.join(" ")) spinner_log_handle.update(title:) spinner_log_handle.stop("\e[33m⚠\e[0m") @spinner_log = nil end |
#info(*args) ⇒ Object
输出静态成功信息(不使用spinner,对应 fancyinfo_success)
99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/pindo/base/funlog.rb', line 99 def info(*args) = Funlog.ensure_utf8(args.join(" ")) # 检查是否在任务上下文中 if Funlog.in_task_context? # 并发模式:路由到输出管理器(只写日志文件) Funlog.current_output_sink.log_info(Funlog.current_task_id, ) else # 串行模式:保持原有行为 puts " #{}" end end |
#spinner_log_handle ⇒ Object
32 33 34 35 36 37 |
# File 'lib/pindo/base/funlog.rb', line 32 def spinner_log_handle if @spinner_log.nil? @spinner_log = create_spinner(info_key:"title") end @spinner_log end |
#warning(*args) ⇒ Object
输出静态警告信息(不使用spinner)
114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/pindo/base/funlog.rb', line 114 def warning(*args) = Funlog.ensure_utf8(args.join(" ")) # 检查是否在任务上下文中 if Funlog.in_task_context? # 并发模式:路由到输出管理器 Funlog.current_output_sink.log_warning(Funlog.current_task_id, ) else # 串行模式:保持原有行为 puts "\e[33m ⚠ #{}\e[0m" end end |