Skip to content

Commit 4835076

Browse files
committed
improve debug logging
1 parent f4bb57a commit 4835076

File tree

4 files changed

+81
-4
lines changed

4 files changed

+81
-4
lines changed

lib/libvirt_async.rb

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,9 @@ def logger
3535

3636
module_function :logger
3737

38-
def build_logger(io, formatter: nil, progname: nil, level: nil, datetime_format: nil)
38+
def build_logger(io, formatter: LogFormatter.new, progname: nil, level: :info, datetime_format: nil)
39+
formatter&.datetime_format = datetime_format unless datetime_format.nil?
3940
logger = Logger.new(io, formatter: formatter, progname: progname, level: level)
40-
logger.level = level unless level.nil?
41-
logger.formatter = formatter || LogFormatter.new
42-
logger.formatter.datetime_format = datetime_format unless datetime_format.nil?
4341
logger
4442
end
4543

@@ -50,4 +48,30 @@ def use_logger!(io = STDOUT, options = {})
5048
end
5149

5250
module_function :use_logger!
51+
52+
def start_debug_logging!(timeout = 2)
53+
LibvirtAsync.logger.debug { "scheduling debug logging!" }
54+
@debug_task = Util.create_task do
55+
LibvirtAsync.logger.debug { "starting debug logging!" }
56+
begin
57+
while true do
58+
raise Error, 'implementations not registered' if @implementations.nil?
59+
@implementations.print_debug_info
60+
Async::Task.current.reactor.sleep timeout
61+
end
62+
rescue Error => e
63+
LibvirtAsync.logger.debug { "stopping debug logging! #{e.message}" }
64+
end
65+
end
66+
Async::Task.current.reactor << @debug_task.fiber
67+
end
68+
69+
module_function :start_debug_logging!
70+
71+
def stop_debug_logging!
72+
@debug_task&.stop(true)
73+
@debug_task = nil
74+
end
75+
76+
module_function :stop_debug_logging!
5377
end

lib/libvirt_async/handle.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ def close
1414
def readiness
1515
monitor&.readiness
1616
end
17+
18+
def to_s
19+
"#<#{self.class}:0x#{object_id.to_s(16)} readable=#{@readable&.object_id&.to_s(16)} writable=#{@writable&.object_id&.to_s(16)} alive=#{@monitor && !@monitor.closed?}>"
20+
end
21+
22+
def inspect
23+
to_s
24+
end
1725
end
1826

1927
include WithDbg
@@ -94,6 +102,14 @@ def unregister
94102
@monitor = nil
95103
end
96104

105+
def to_s
106+
"#<#{self.class}:0x#{object_id.to_s(16)} handle_id=#{handle_id} fd=#{fd} events=#{events} monitor=#{monitor}>"
107+
end
108+
109+
def inspect
110+
to_s
111+
end
112+
97113
private
98114

99115
def dispatch(events)

lib/libvirt_async/implementations.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,27 @@ def stop
2929
default_variables
3030
end
3131

32+
def print_debug_info
33+
str = [
34+
"#{self.class}:0x#{object_id.to_s(16)}",
35+
"handles = [",
36+
@handles.map(&:to_s).join("\n"),
37+
"]",
38+
"timers = [",
39+
@timers.map(&:to_s).join("\n"),
40+
"]"
41+
].join("\n")
42+
LibvirtAsync.logger&.debug { str }
43+
end
44+
45+
def to_s
46+
"#<#{self.class}:0x#{object_id.to_s(16)} handles=#{@handles} timers=#{@timers}>"
47+
end
48+
49+
def inspect
50+
to_s
51+
end
52+
3253
private
3354

3455
def default_variables

lib/libvirt_async/timer.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ def close
2828
@fiber.resume(Cancelled.new) if @fiber&.alive?
2929
@fiber = nil
3030
end
31+
32+
def to_s
33+
"#<#{self.class}:0x#{object_id.to_s(16)} fiber=#{@fiber.&object_id&.to_s(16)} alive=#{@fiber&.alive?}>"
34+
end
35+
36+
def inspect
37+
to_s
38+
end
3139
end
3240

3341
include WithDbg
@@ -91,6 +99,14 @@ def unregister
9199
@monitor = nil
92100
end
93101

102+
def to_s
103+
"#<#{self.class}:0x#{object_id.to_s(16)} timer_id=#{timer_id} interval=#{interval} last_fired=#{last_fired} monitor=#{monitor}>"
104+
end
105+
106+
def inspect
107+
to_s
108+
end
109+
94110
private
95111

96112
def dispatch

0 commit comments

Comments
 (0)