Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added method logging #13

Draft
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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