Refactor env logging common usages with a module
This commit is contained in:
parent
5a102e7739
commit
a9466a49f0
@ -3,6 +3,8 @@ require 'logger'
|
||||
require 'optparse'
|
||||
require 'uh'
|
||||
|
||||
require 'uh/wm/env_logging'
|
||||
|
||||
require 'uh/wm/actions_handler'
|
||||
require 'uh/wm/cli'
|
||||
require 'uh/wm/client'
|
||||
|
@ -1,6 +1,8 @@
|
||||
module Uh
|
||||
module WM
|
||||
class ActionsHandler
|
||||
include EnvLogging
|
||||
|
||||
def initialize env, events
|
||||
@env, @events = env, events
|
||||
end
|
||||
@ -14,14 +16,14 @@ module Uh
|
||||
end
|
||||
|
||||
def execute command
|
||||
@env.log "Execute: #{command}"
|
||||
log "Execute: #{command}"
|
||||
pid = fork do
|
||||
fork do
|
||||
Process.setsid
|
||||
begin
|
||||
exec command
|
||||
rescue Errno::ENOENT => e
|
||||
@env.log_error "ExecuteError: #{e}"
|
||||
log_error "ExecuteError: #{e}"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -3,6 +3,8 @@ module Uh
|
||||
class CLI
|
||||
ArgumentError = Class.new(ArgumentError)
|
||||
|
||||
include EnvLogging
|
||||
|
||||
USAGE = "Usage: #{File.basename $0} [options]".freeze
|
||||
|
||||
EX_USAGE = 64
|
||||
@ -63,7 +65,7 @@ module Uh
|
||||
end
|
||||
opts.on '-r', '--require PATH', 'require ruby feature' do |feature|
|
||||
require feature
|
||||
@env.log "Loaded `#{feature}' ruby feature"
|
||||
log "Loaded `#{feature}' ruby feature"
|
||||
end
|
||||
opts.on '-l', '--layout LAYOUT', 'specify layout' do |layout|
|
||||
@env.layout_class = self.class.const_get layout.to_sym
|
||||
|
8
lib/uh/wm/env_logging.rb
Normal file
8
lib/uh/wm/env_logging.rb
Normal file
@ -0,0 +1,8 @@
|
||||
module Uh
|
||||
module WM
|
||||
module EnvLogging
|
||||
extend Forwardable
|
||||
def_delegators :@env, :log, :log_error, :log_debug
|
||||
end
|
||||
end
|
||||
end
|
@ -1,6 +1,8 @@
|
||||
module Uh
|
||||
module WM
|
||||
class Runner
|
||||
include EnvLogging
|
||||
|
||||
class << self
|
||||
def run env, **options
|
||||
runner = new env, **options
|
||||
@ -57,16 +59,16 @@ module Uh
|
||||
def worker
|
||||
@worker ||= Workers.build(*(@env.worker)).tap do |w|
|
||||
w.on_read do
|
||||
@env.log_debug 'Processing pending events'
|
||||
log_debug 'Processing pending events'
|
||||
@manager.handle_pending_events
|
||||
end
|
||||
w.on_read_next do
|
||||
@env.log_debug 'Processing next event'
|
||||
log_debug 'Processing next event'
|
||||
@manager.handle_next_event
|
||||
end
|
||||
w.on_timeout do |*args|
|
||||
@env.log_debug "Worker timeout: #{args.inspect}"
|
||||
@env.log_debug 'Flushing X output buffer'
|
||||
log_debug "Worker timeout: #{args.inspect}"
|
||||
log_debug 'Flushing X output buffer'
|
||||
@manager.flush
|
||||
end
|
||||
end
|
||||
@ -74,12 +76,12 @@ module Uh
|
||||
|
||||
def run_until &block
|
||||
worker.watch @manager
|
||||
@env.log "Working events with `#{worker.class}'"
|
||||
log "Working events with `#{worker.class}'"
|
||||
worker.work_events until block.call
|
||||
end
|
||||
|
||||
def terminate
|
||||
@env.log "Terminating..."
|
||||
log "Terminating..."
|
||||
manager.disconnect
|
||||
end
|
||||
|
||||
@ -92,22 +94,22 @@ module Uh
|
||||
|
||||
def register_manager_hooks
|
||||
@events.on :connecting do |display|
|
||||
@env.log_debug "Connecting to X server on `#{display}'"
|
||||
log_debug "Connecting to X server on `#{display}'"
|
||||
end
|
||||
@events.on :connected do |display|
|
||||
@env.log "Connected to X server on `#{display}'"
|
||||
log "Connected to X server on `#{display}'"
|
||||
end
|
||||
@events.on(:disconnected) { @env.log "Disconnected from X server" }
|
||||
@events.on(:disconnected) { log "Disconnected from X server" }
|
||||
@events.on(:xevent) { |event| XEventLogger.new(env).log_event event }
|
||||
end
|
||||
|
||||
def register_layout_hooks
|
||||
@events.on :connected do |display|
|
||||
@env.log "Registering layout `#{layout.class}'"
|
||||
log "Registering layout `#{layout.class}'"
|
||||
layout.register display
|
||||
end
|
||||
@events.on :manage do |client|
|
||||
@env.log "Manage client #{client}"
|
||||
log "Manage client #{client}"
|
||||
layout << client
|
||||
end
|
||||
end
|
||||
@ -120,6 +122,8 @@ module Uh
|
||||
|
||||
|
||||
class XEventLogger
|
||||
include EnvLogging
|
||||
|
||||
def initialize env
|
||||
@env = env
|
||||
end
|
||||
@ -132,7 +136,7 @@ module Uh
|
||||
"window: #{xev.window}"
|
||||
end
|
||||
|
||||
@env.log_debug [
|
||||
log_debug [
|
||||
'XEvent',
|
||||
xev.type,
|
||||
xev.send_event ? 'SENT' : nil,
|
||||
|
Loading…
x
Reference in New Issue
Block a user