From c9e55e2615604e16edacf439470e0728b5153cac Mon Sep 17 00:00:00 2001 From: Koichi Sasada Date: Thu, 30 Jun 2022 16:54:42 +0900 Subject: [PATCH] Create message lazily for `DEBUGGER__.debug` `DEBUGGER__.debug` can be called many times so the building messages should be lazy. --- lib/debug/session.rb | 27 ++++++++++++++++----------- lib/debug/thread_client.rb | 22 +++++++++++++++------- 2 files changed, 31 insertions(+), 18 deletions(-) diff --git a/lib/debug/session.rb b/lib/debug/session.rb index 0075231e3..668afa37a 100644 --- a/lib/debug/session.rb +++ b/lib/debug/session.rb @@ -2080,22 +2080,27 @@ def self.info msg log :INFO, msg end - def self.debug msg - log :DEBUG, msg - end - - def self.log level, msg - @logfile = STDERR unless defined? @logfile - + def self.check_loglevel level lv = LOG_LEVELS[level] config_lv = LOG_LEVELS[CONFIG[:log_level] || :WARN] + lv <= config_lv + end - if defined? SESSION - pi = SESSION.process_info - process_info = pi ? "[#{pi}]" : nil + def self.debug(&b) + if check_loglevel :DEBUG + log :DEBUG, b.call end + end + + def self.log level, msg + if check_loglevel level + @logfile = STDERR unless defined? @logfile + + if defined? SESSION + pi = SESSION.process_info + process_info = pi ? "[#{pi}]" : nil + end - if lv <= config_lv if level == :WARN # :WARN on debugger is general information @logfile.puts "DEBUGGER#{process_info}: #{msg}" diff --git a/lib/debug/thread_client.rb b/lib/debug/thread_client.rb index 4e526bde0..b194edda9 100644 --- a/lib/debug/thread_client.rb +++ b/lib/debug/thread_client.rb @@ -1057,22 +1057,30 @@ def wait_next_action_ end def debug_event(ev, args) - args = args.map { |arg| DEBUGGER__.safe_inspect(arg) } - DEBUGGER__.debug("#{inspect} sends Event { type: #{ev.inspect}, args: #{args} } to Session") + DEBUGGER__.debug{ + args = args.map { |arg| DEBUGGER__.safe_inspect(arg) } + "#{inspect} sends Event { type: #{ev.inspect}, args: #{args} } to Session" + } end def debug_mode(old_mode, new_mode) - DEBUGGER__.debug("#{inspect} changes mode (#{old_mode} -> #{new_mode})") + DEBUGGER__.debug{ + "#{inspect} changes mode (#{old_mode} -> #{new_mode})" + } end def debug_cmd(cmds) - cmd, *args = *cmds - args = args.map { |arg| DEBUGGER__.safe_inspect(arg) } - DEBUGGER__.debug("#{inspect} receives Cmd { type: #{cmd.inspect}, args: #{args} } from Session") + DEBUGGER__.debug{ + cmd, *args = *cmds + args = args.map { |arg| DEBUGGER__.safe_inspect(arg) } + "#{inspect} receives Cmd { type: #{cmd.inspect}, args: #{args} } from Session" + } end def debug_suspend(event) - DEBUGGER__.debug("#{inspect} is suspended for #{event.inspect}") + DEBUGGER__.debug{ + "#{inspect} is suspended for #{event.inspect}" + } end class Recorder