Skip to content

Instantly share code, notes, and snippets.

@arika
Last active February 9, 2018 07:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arika/680eebebf6f31734b4badf89d1520729 to your computer and use it in GitHub Desktop.
Save arika/680eebebf6f31734b4badf89d1520729 to your computer and use it in GitHub Desktop.
require 'shellwords'
class MiniMagickCommandExecutionTrace
OUTPUT = $stderr
TERM = {
green: "\e[32m",
red: "\e[31m",
yellow: "\e[33m",
reset: "\e[m",
}.freeze
def initialize
@tps = [
TracePoint.new(:call, &trace_call),
TracePoint.new(:return, &trace_return),
TracePoint.new(:raise, &trace_raise),
]
@in_trace = 0
end
def on
@tps.each(&:enable)
end
def off
@tps.each(&:disable)
end
private
def trace_call
lambda do |tp|
break unless trace_needed?(tp)
method_id = tp.method_id
command = tp.binding.local_variable_get(:command)
puts "=== #{method_id} ================= command (#{Time.now.iso8601})", color: :green
puts command.shelljoin
@in_trace += 1
end
end
def trace_return
lambda do |tp|
break unless trace_needed?(tp)
@in_trace -= 1
timestamp = Time.now.iso8601
method_id = tp.method_id
stdout, stderr, status = tp.return_value
unless stdout.nil? || stdout.empty?
puts "--- #{method_id} ----------------- stdout", color: :green
puts stdout
end
unless stderr.nil? || stderr.empty?
puts "--- #{method_id} ----------------- stderr", color: :yellow
puts stderr
end
color = if status.nil?
:red
elsif status.respond_to?(:success?)
status.success? ? :green : :yellow
else
status.zero? ? :green : :yellow
end
unless status.nil?
puts "--- #{method_id} ----------------- status", color: color
puts status.inspect
end
puts "=== #{method_id} ================= finish (#{timestamp})", color: color
puts
end
end
def trace_raise
lambda do |tp|
break if @in_trace == 0
timestamp = Time.now.iso8601
method_id = tp.method_id
exception = tp.raised_exception
puts "--- #{method_id} ----------------- exception (#{timestamp})", color: :red
puts "#{exception} (#{exception.class})"
exception.backtrace.each {|l| puts "\t#{l}" }
end
end
def trace_needed?(tp)
return false unless tp.self.is_a?(MiniMagick::Shell)
return false unless tp.method_id == :execute_open3 || tp.method_id == :execute_posix_spawn
true
end
def puts(str = '', color: nil)
if color = TERM[color]
str = "#{color}#{str}#{TERM[:reset]}"
end
OUTPUT.puts(str)
end
end
mini_magic_trace = MiniMagickCommandExecutionTrace.new
define_method(:mini_magick_debug_on) { mini_magic_trace.on }
define_method(:mini_magick_debug_off) { mini_magic_trace.off }
def mini_magick_debug
mini_magick_debug_on
yield
ensure
mini_magick_debug_off
end
#gem 'mini_magick', '=4.2.4'
require 'mini_magick'
require_relative 'mini_magick_debug'
if MiniMagick.logger.respond_to?(:level=)
MiniMagick.logger.level = Logger::DEBUG
else
MiniMagick.debug = true
end
Process.setrlimit(:CPU, 1, 1)
mini_magick_debug_on
MiniMagick::Tool::Convert.new do |c|
c.size '4096x4096'
c << 'tile:logo:'
c << '-font' << 'VL-PGothic-Regular'
c << '-fill' << '#000000'
c << '-pointsize' << '14'
c << '-gravity' << 'North-West'
c << '-annotate' << '+10+10' << 'hoge ほげ'
c << 'test.png'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment