diff --git a/lib/obsws.rb b/lib/obsws.rb index 2cd0dd9..d2bb201 100644 --- a/lib/obsws.rb +++ b/lib/obsws.rb @@ -1,11 +1,5 @@ -require "logger" - require_relative "obsws/req" require_relative "obsws/event" module OBSWS - include Logger::Severity - - LOGGER = Logger.new(STDOUT) - LOGGER.level = WARN end diff --git a/lib/obsws/base.rb b/lib/obsws/base.rb index 6601c7a..7349919 100644 --- a/lib/obsws/base.rb +++ b/lib/obsws/base.rb @@ -6,6 +6,7 @@ require "waitutil" require_relative "mixin" require_relative "error" +require_relative "logger" module OBSWS class Socket @@ -22,6 +23,7 @@ module OBSWS end class Base + include Logging include Mixin::OPCodes attr_reader :closed @@ -37,14 +39,13 @@ module OBSWS @driver = WebSocket::Driver.client(Socket.new("ws://#{host}:#{port}", @socket)) @driver.on :open do |msg| - LOGGER.debug("driver socket open") + logger.debug("driver socket open") end @driver.on :close do |msg| - LOGGER.debug("driver socket closed") + logger.debug("driver socket closed") @closed = true end @driver.on :message do |msg| - LOGGER.debug("received: #{msg.data}") msg_handler(JSON.parse(msg.data, symbolize_names: true)) end start_driver @@ -91,7 +92,7 @@ module OBSWS if @password.empty? raise OBSWSError("auth enabled but no password provided") end - LOGGER.info("initiating authentication") + logger.info("initiating authentication") payload[:d][:authentication] = auth_token(**auth) end @driver.text(JSON.generate(payload)) @@ -117,7 +118,7 @@ module OBSWS } } payload[:d][:requestData] = data if data - LOGGER.debug("sending request: #{payload}") + logger.debug("sending request: #{payload}") @driver.text(JSON.generate(payload)) end end diff --git a/lib/obsws/event.rb b/lib/obsws/event.rb index 7223f50..40fbac5 100644 --- a/lib/obsws/event.rb +++ b/lib/obsws/event.rb @@ -2,6 +2,7 @@ require "json" require_relative "util" require_relative "mixin" +require_relative "logger" module OBSWS module Events @@ -62,6 +63,7 @@ module OBSWS end class Client + include Logging include Callbacks include Mixin::TearDown include Mixin::OPCodes @@ -69,9 +71,10 @@ module OBSWS def initialize(**kwargs) kwargs[:subs] ||= SUBS::LOW_VOLUME @base_client = Base.new(**kwargs) - LOGGER.info("#{self} succesfully identified with server") + logger.info("#{self} succesfully identified with server") @base_client.updater = ->(op_code, data) { if op_code == Mixin::OPCodes::EVENT + logger.debug("received: #{data}") event = data[:eventType] data = data.fetch(:eventData, {}) notify_observers(event, Mixin::Data.new(data, data.keys)) diff --git a/lib/obsws/logger.rb b/lib/obsws/logger.rb new file mode 100644 index 0000000..e27244a --- /dev/null +++ b/lib/obsws/logger.rb @@ -0,0 +1,11 @@ +require "logger" + +module OBSWS + module Logging + def logger + @logger = Logger.new($stdout, level: ENV.fetch("OBSWS_LOG_LEVEL", "WARN")) + @logger.progname = instance_of?(::Module) ? name : self.class.name + @logger + end + end +end diff --git a/lib/obsws/req.rb b/lib/obsws/req.rb index 7803d9b..ef944c8 100644 --- a/lib/obsws/req.rb +++ b/lib/obsws/req.rb @@ -4,18 +4,21 @@ require_relative "base" require_relative "error" require_relative "util" require_relative "mixin" +require_relative "logger" module OBSWS module Requests class Client + include Logging include Error include Mixin::TearDown include Mixin::OPCodes def initialize(**kwargs) @base_client = Base.new(**kwargs) - LOGGER.info("#{self} succesfully identified with server") + logger.info("#{self} succesfully identified with server") @base_client.updater = ->(op_code, data) { + logger.debug("response received: #{data}") @response = data if op_code == Mixin::OPCodes::REQUESTRESPONSE } @response = {requestId: 0} @@ -56,7 +59,7 @@ module OBSWS @response[:responseData] rescue WaitUtil::TimeoutError msg = "no response with matching id received" - LOGGER.error(msg) + logger.error(msg) raise OBSWSError.new(msg) end diff --git a/lib/obsws/util.rb b/lib/obsws/util.rb index 7c68da1..0b5a64e 100644 --- a/lib/obsws/util.rb +++ b/lib/obsws/util.rb @@ -2,12 +2,11 @@ module OBSWS module Util class ::String def to_camel - self.split(/_/).map(&:capitalize).join + split("_").map(&:capitalize).join end def to_snake - self - .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2') + gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2') .gsub(/([a-z\d])([A-Z])/, '\1_\2') .downcase end