logger module added

This commit is contained in:
onyx-and-iris 2023-07-26 14:37:35 +01:00
parent 72ee539b96
commit 82c6ced760
6 changed files with 28 additions and 17 deletions

View File

@ -1,11 +1,5 @@
require "logger"
require_relative "obsws/req" require_relative "obsws/req"
require_relative "obsws/event" require_relative "obsws/event"
module OBSWS module OBSWS
include Logger::Severity
LOGGER = Logger.new(STDOUT)
LOGGER.level = WARN
end end

View File

@ -6,6 +6,7 @@ require "waitutil"
require_relative "mixin" require_relative "mixin"
require_relative "error" require_relative "error"
require_relative "logger"
module OBSWS module OBSWS
class Socket class Socket
@ -22,6 +23,7 @@ module OBSWS
end end
class Base class Base
include Logging
include Mixin::OPCodes include Mixin::OPCodes
attr_reader :closed attr_reader :closed
@ -37,14 +39,13 @@ module OBSWS
@driver = @driver =
WebSocket::Driver.client(Socket.new("ws://#{host}:#{port}", @socket)) WebSocket::Driver.client(Socket.new("ws://#{host}:#{port}", @socket))
@driver.on :open do |msg| @driver.on :open do |msg|
LOGGER.debug("driver socket open") logger.debug("driver socket open")
end end
@driver.on :close do |msg| @driver.on :close do |msg|
LOGGER.debug("driver socket closed") logger.debug("driver socket closed")
@closed = true @closed = true
end end
@driver.on :message do |msg| @driver.on :message do |msg|
LOGGER.debug("received: #{msg.data}")
msg_handler(JSON.parse(msg.data, symbolize_names: true)) msg_handler(JSON.parse(msg.data, symbolize_names: true))
end end
start_driver start_driver
@ -91,7 +92,7 @@ module OBSWS
if @password.empty? if @password.empty?
raise OBSWSError("auth enabled but no password provided") raise OBSWSError("auth enabled but no password provided")
end end
LOGGER.info("initiating authentication") logger.info("initiating authentication")
payload[:d][:authentication] = auth_token(**auth) payload[:d][:authentication] = auth_token(**auth)
end end
@driver.text(JSON.generate(payload)) @driver.text(JSON.generate(payload))
@ -117,7 +118,7 @@ module OBSWS
} }
} }
payload[:d][:requestData] = data if data payload[:d][:requestData] = data if data
LOGGER.debug("sending request: #{payload}") logger.debug("sending request: #{payload}")
@driver.text(JSON.generate(payload)) @driver.text(JSON.generate(payload))
end end
end end

View File

@ -2,6 +2,7 @@ require "json"
require_relative "util" require_relative "util"
require_relative "mixin" require_relative "mixin"
require_relative "logger"
module OBSWS module OBSWS
module Events module Events
@ -62,6 +63,7 @@ module OBSWS
end end
class Client class Client
include Logging
include Callbacks include Callbacks
include Mixin::TearDown include Mixin::TearDown
include Mixin::OPCodes include Mixin::OPCodes
@ -69,9 +71,10 @@ module OBSWS
def initialize(**kwargs) def initialize(**kwargs)
kwargs[:subs] ||= SUBS::LOW_VOLUME kwargs[:subs] ||= SUBS::LOW_VOLUME
@base_client = Base.new(**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) { @base_client.updater = ->(op_code, data) {
if op_code == Mixin::OPCodes::EVENT if op_code == Mixin::OPCodes::EVENT
logger.debug("received: #{data}")
event = data[:eventType] event = data[:eventType]
data = data.fetch(:eventData, {}) data = data.fetch(:eventData, {})
notify_observers(event, Mixin::Data.new(data, data.keys)) notify_observers(event, Mixin::Data.new(data, data.keys))

11
lib/obsws/logger.rb Normal file
View File

@ -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

View File

@ -4,18 +4,21 @@ require_relative "base"
require_relative "error" require_relative "error"
require_relative "util" require_relative "util"
require_relative "mixin" require_relative "mixin"
require_relative "logger"
module OBSWS module OBSWS
module Requests module Requests
class Client class Client
include Logging
include Error include Error
include Mixin::TearDown include Mixin::TearDown
include Mixin::OPCodes include Mixin::OPCodes
def initialize(**kwargs) def initialize(**kwargs)
@base_client = Base.new(**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) { @base_client.updater = ->(op_code, data) {
logger.debug("response received: #{data}")
@response = data if op_code == Mixin::OPCodes::REQUESTRESPONSE @response = data if op_code == Mixin::OPCodes::REQUESTRESPONSE
} }
@response = {requestId: 0} @response = {requestId: 0}
@ -56,7 +59,7 @@ module OBSWS
@response[:responseData] @response[:responseData]
rescue WaitUtil::TimeoutError rescue WaitUtil::TimeoutError
msg = "no response with matching id received" msg = "no response with matching id received"
LOGGER.error(msg) logger.error(msg)
raise OBSWSError.new(msg) raise OBSWSError.new(msg)
end end

View File

@ -2,12 +2,11 @@ module OBSWS
module Util module Util
class ::String class ::String
def to_camel def to_camel
self.split(/_/).map(&:capitalize).join split("_").map(&:capitalize).join
end end
def to_snake 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') .gsub(/([a-z\d])([A-Z])/, '\1_\2')
.downcase .downcase
end end