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