diff --git a/lib/voicemeeter/cbindings.rb b/lib/voicemeeter/cbindings.rb index 390de12..732ce7f 100644 --- a/lib/voicemeeter/cbindings.rb +++ b/lib/voicemeeter/cbindings.rb @@ -1,11 +1,13 @@ require_relative "install" require_relative "errors" require_relative "util" +require_relative "logger" module Voicemeeter module CBindings private + extend Logging extend FFI::Library VM_PATH = Install.get_vmpath @@ -53,14 +55,21 @@ module Voicemeeter attach_function :VBVMR_GetMidiMessage, %i[pointer long], :long def call(fn, *args, ok: [0], exp: nil) + to_cname = -> { + "VBVMR_#{Util::String.camelcase(fn.to_s.delete_prefix("bind_")) + .gsub(/(Button|Input|Output)/, '\1_')}" + } + res = send(fn, *args) if exp.nil? unless ok.include?(res) - raise Errors::VMCAPIError.new fn, res + logger.error "#{to_cname.call} returned #{res}" + raise Errors::VMCAPIError.new to_cname.call, res end else unless exp.call(res) || ok.include?(res) - raise Errors::VMCAPIError.new fn, res + logger.error "#{to_cname.call} returned #{res}" + raise Errors::VMCAPIError.new to_cname.call, res end end res diff --git a/lib/voicemeeter/errors.rb b/lib/voicemeeter/errors.rb index dac5f4d..1c1fc69 100644 --- a/lib/voicemeeter/errors.rb +++ b/lib/voicemeeter/errors.rb @@ -6,10 +6,10 @@ module Voicemeeter end class VMCAPIError < VMError - attr_reader :code + attr_reader :fn_name, :code - def initialize(ruby_name, code) - @ruby_name = ruby_name + def initialize(fn_name, code) + @fn_name = fn_name @code = code super(message) end @@ -17,10 +17,6 @@ module Voicemeeter def message "#{fn_name} returned #{code}" end - - def fn_name - "VBVMR_#{Util::String.camelcase(@ruby_name.to_s.delete_prefix("bind_")).sub("macro_button", "macrobutton")}" - end end end end