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/event"
module OBSWS
include Logger::Severity
LOGGER = Logger.new(STDOUT)
LOGGER.level = WARN
end

View File

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

View File

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

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

View File

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