Skip to content

Commit

Permalink
Added method logging
Browse files Browse the repository at this point in the history
  • Loading branch information
genya0407 committed Jul 19, 2024
1 parent eb12096 commit e46f590
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/mysql_rewinder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require_relative "mysql_rewinder/version"
require_relative "mysql_rewinder/cleaner"
require_relative "mysql_rewinder/method_logging"
require 'set'
require 'tmpdir'
require 'fileutils'
Expand All @@ -14,6 +15,15 @@ class << self
delegate %i[clean clean_all record_inserted_table] => :@instance

def setup(db_configs, except_tables: [], adapter: :trilogy, logger: nil)
if logger
target_classes = MethodLogging.calculate_target_classes

target_classes.each do |target_class|
MethodLogging.prepend_logger_to_singleton_methods(target_class, logger)
MethodLogging.prepend_logger_to_instance_methods(target_class, logger)
end
end

@instance = new(db_configs: db_configs, except_tables: except_tables, adapter: adapter, logger: logger)
end
end
Expand Down
78 changes: 78 additions & 0 deletions lib/mysql_rewinder/method_logging.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
class MysqlRewinder
module MethodLogging
def self.calculate_target_classes
target_classes = []
current_modules = [MysqlRewinder]
loop do
break if current_modules.empty?

while current_module = current_modules.pop
target_classes << current_module if current_module.is_a?(Class)
current_module.constants.each do |child_constant_name|
child_constant = current_module.const_get(child_constant_name)
if child_constant.is_a?(Module)
current_modules << child_constant
end
end
end
end
target_classes - [self.class]
end

def self.convert_args_to_string(args, kwargs, block)
args_string = args.empty? ? nil : args.map(&:inspect).join(', ')
kwargs_string = kwargs.empty? ? nil : kwargs
block = block ? "block ==> #{block}" : nil

[args_string, kwargs_string, block].compact.join(', ')
end

def self.prepend_logger_to_singleton_methods(target_class, logger)
mod = Module.new do
target_class.singleton_methods(false).each do |singleton_method_name|
define_method singleton_method_name do |*args, **kwargs, &block|
MethodLogging.incur_padding
logger.debug { "#{MethodLogging.padding}[MysqlRewinder] #{target_class}.#{singleton_method_name}(#{MethodLogging.convert_args_to_string(args, kwargs, block)})" }
super(*args, **kwargs, &block)
ensure
MethodLogging.decur_padding
end
end
end
target_class.singleton_class.prepend mod
end

def self.prepend_logger_to_instance_methods(target_class, logger)
mod = Module.new do
target_class.instance_methods(false).each do |method_name|
define_method method_name do |*args, **kwargs, &block|
MethodLogging.incur_padding
logger.debug { "#{MethodLogging.padding}[MysqlRewinder] #{target_class}\##{method_name}(#{MethodLogging.convert_args_to_string(args, kwargs, block)})" }
logger.debug { "#{MethodLogging.padding}[MysqlRewinder] ========= instance inspection starts =======" }
logger.debug { "#{MethodLogging.padding} [MysqlRewinder] #{self.inspect}" }
logger.debug { "#{MethodLogging.padding}[MysqlRewinder] ========= instance inspection finishes =======" }
super(*args, **kwargs, &block)
ensure
MethodLogging.decur_padding
end
end
end
target_class.prepend mod
end

def self.incur_padding
@padding ||= 0
@padding += 1
end

def self.decur_padding
@padding ||= 0
@padding -= 1
end

def self.padding
@padding ||= 0
' ' * @padding * 2
end
end
end

0 comments on commit e46f590

Please sign in to comment.