Class: Pindo::Funlog

Inherits:
Object
  • Object
show all
Defined in:
lib/pindo/base/funlog.rb

Defined Under Namespace

Modules: Mixin

Singleton collapse

Singleton collapse

Instance Method Summary collapse

Class Attribute Details

.instanceObject



125
126
127
# File 'lib/pindo/base/funlog.rb', line 125

def self.instance
  @instance ||= new
end

Class Method Details

.current_output_sinkOutputSink?

获取当前线程的输出接收器

Returns:



145
146
147
# File 'lib/pindo/base/funlog.rb', line 145

def current_output_sink
  Thread.current[:output_sink]
end

.current_task_idString?

获取当前线程的任务 ID

Returns:

  • (String, nil)

    任务 ID 或 nil



151
152
153
# File 'lib/pindo/base/funlog.rb', line 151

def current_task_id
  Thread.current[:task_id]
end

.error(*args) ⇒ Object



194
195
196
# File 'lib/pindo/base/funlog.rb', line 194

def error(*args)
  instance.error(*args)
end

.fancyinfo_error(*args) ⇒ Object



178
179
180
# File 'lib/pindo/base/funlog.rb', line 178

def fancyinfo_error(*args)
  instance.fancyinfo_error(*args)
end

.fancyinfo_start(*args) ⇒ Object

类方法委托,简化调用方式使用 Funlog.info(“消息”) 代替 Funlog.instance.info(“消息”)



166
167
168
# File 'lib/pindo/base/funlog.rb', line 166

def fancyinfo_start(*args)
  instance.fancyinfo_start(*args)
end

.fancyinfo_success(*args) ⇒ Object



174
175
176
# File 'lib/pindo/base/funlog.rb', line 174

def fancyinfo_success(*args)
  instance.fancyinfo_success(*args)
end

.fancyinfo_update(*args) ⇒ Object



170
171
172
# File 'lib/pindo/base/funlog.rb', line 170

def fancyinfo_update(*args)
  instance.fancyinfo_update(*args)
end

.fancyinfo_warning(*args) ⇒ Object



182
183
184
# File 'lib/pindo/base/funlog.rb', line 182

def fancyinfo_warning(*args)
  instance.fancyinfo_warning(*args)
end

.in_task_context?Boolean

检查是否在任务上下文中

Returns:

  • (Boolean)

    是否在任务上下文中



157
158
159
# File 'lib/pindo/base/funlog.rb', line 157

def in_task_context?
  !current_output_sink.nil? && !current_task_id.nil?
end

.info(*args) ⇒ Object



186
187
188
# File 'lib/pindo/base/funlog.rb', line 186

def info(*args)
  instance.info(*args)
end

.reload_instanceConfig

Returns the current config instance creating one if needed.

Returns:

  • (Config)

    the current config instance creating one if needed.



121
122
123
# File 'lib/pindo/base/funlog.rb', line 121

def self.reload_instance
  @instance = new
end

.warning(*args) ⇒ Object



190
191
192
# File 'lib/pindo/base/funlog.rb', line 190

def warning(*args)
  instance.warning(*args)
end

Instance Method Details

#create_spinner(info_key: nil) ⇒ Object



8
9
10
11
12
# File 'lib/pindo/base/funlog.rb', line 8

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)

Parameters:

  • args (Array)

    要输出的消息



102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/pindo/base/funlog.rb', line 102

def error(*args)
   message = args.join(" ")

   # 检查是否在任务上下文中
   if Funlog.in_task_context?
     # 并发模式:路由到输出管理器
     Funlog.current_output_sink.log_error(Funlog.current_task_id, message)
   else
     # 串行模式:保持原有行为
     puts "\e[31m  ✗ #{message}\e[0m"
   end
end

#fancyinfo_error(*args) ⇒ Object



54
55
56
57
58
59
# File 'lib/pindo/base/funlog.rb', line 54

def fancyinfo_error(*args)
   message = args.join(" ")
   spinner_log_handle.update(title:message)
   spinner_log_handle.error
   @spinner_log =nil
end

#fancyinfo_start(*args) ⇒ Object



23
24
25
26
27
28
# File 'lib/pindo/base/funlog.rb', line 23

def fancyinfo_start(*args)
  message = args.join(" ")
  spinner_log_handle.update(title:message)
  spinner_log_handle.auto_spin
  @spinner_log
end

#fancyinfo_success(*args) ⇒ Object



47
48
49
50
51
52
# File 'lib/pindo/base/funlog.rb', line 47

def fancyinfo_success(*args)
   message = args.join(" ")
   spinner_log_handle.update(title:message)
   spinner_log_handle.success
   @spinner_log =nil
end

#fancyinfo_update(*args) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/pindo/base/funlog.rb', line 31

def fancyinfo_update(*args)
   message = args.join(" ")

   # 检查是否在任务上下文中
   if Funlog.in_task_context?
     # 并发模式:不更新终端,只写日志
     Funlog.current_output_sink.log_info(Funlog.current_task_id, message)
   else
     # 串行模式:使用 spinner
     spinner_log_handle.update(title:message)
     spinner_log_handle.spin
   end

   @spinner_log
end

#fancyinfo_warning(*args) ⇒ Object

输出警告信息(使用spinner,黄色警告标记)

Parameters:

  • args (Array)

    要输出的消息



63
64
65
66
67
68
# File 'lib/pindo/base/funlog.rb', line 63

def fancyinfo_warning(*args)
   message = args.join(" ")
   spinner_log_handle.update(title:message)
   spinner_log_handle.stop("\e[33m⚠\e[0m")
   @spinner_log = nil
end

#info(*args) ⇒ Object

输出静态成功信息(不使用spinner,对应 fancyinfo_success)

Parameters:

  • args (Array)

    要输出的消息



72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/pindo/base/funlog.rb', line 72

def info(*args)
   message = args.join(" ")

   # 检查是否在任务上下文中
   if Funlog.in_task_context?
     # 并发模式:路由到输出管理器(只写日志文件)
     Funlog.current_output_sink.log_info(Funlog.current_task_id, message)
   else
     # 串行模式:保持原有行为
     puts "  ✓ #{message}"
   end
end

#spinner_log_handleObject



14
15
16
17
18
19
# File 'lib/pindo/base/funlog.rb', line 14

def spinner_log_handle
  if @spinner_log.nil?
    @spinner_log = create_spinner(info_key:"title")
  end
  @spinner_log
end

#warning(*args) ⇒ Object

输出静态警告信息(不使用spinner)

Parameters:

  • args (Array)

    要输出的消息



87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/pindo/base/funlog.rb', line 87

def warning(*args)
   message = args.join(" ")

   # 检查是否在任务上下文中
   if Funlog.in_task_context?
     # 并发模式:路由到输出管理器
     Funlog.current_output_sink.log_warning(Funlog.current_task_id, message)
   else
     # 串行模式:保持原有行为
     puts "\e[33m  ⚠ #{message}\e[0m"
   end
end