From a9466a49f0063aa1d5342b17d2e70b586ae92ec6 Mon Sep 17 00:00:00 2001 From: Thibault Jouan Date: Sat, 18 Apr 2015 18:04:05 +0000 Subject: [PATCH] Refactor env logging common usages with a module --- lib/uh/wm.rb | 2 ++ lib/uh/wm/actions_handler.rb | 6 ++++-- lib/uh/wm/cli.rb | 4 +++- lib/uh/wm/env_logging.rb | 8 ++++++++ lib/uh/wm/runner.rb | 28 ++++++++++++++++------------ 5 files changed, 33 insertions(+), 15 deletions(-) create mode 100644 lib/uh/wm/env_logging.rb diff --git a/lib/uh/wm.rb b/lib/uh/wm.rb index 0614a84..04070e9 100644 --- a/lib/uh/wm.rb +++ b/lib/uh/wm.rb @@ -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' diff --git a/lib/uh/wm/actions_handler.rb b/lib/uh/wm/actions_handler.rb index bd3916d..bb112f8 100644 --- a/lib/uh/wm/actions_handler.rb +++ b/lib/uh/wm/actions_handler.rb @@ -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 diff --git a/lib/uh/wm/cli.rb b/lib/uh/wm/cli.rb index e91732e..f69287b 100644 --- a/lib/uh/wm/cli.rb +++ b/lib/uh/wm/cli.rb @@ -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 diff --git a/lib/uh/wm/env_logging.rb b/lib/uh/wm/env_logging.rb new file mode 100644 index 0000000..daf8a77 --- /dev/null +++ b/lib/uh/wm/env_logging.rb @@ -0,0 +1,8 @@ +module Uh + module WM + module EnvLogging + extend Forwardable + def_delegators :@env, :log, :log_error, :log_debug + end + end +end diff --git a/lib/uh/wm/runner.rb b/lib/uh/wm/runner.rb index 582869a..02443db 100644 --- a/lib/uh/wm/runner.rb +++ b/lib/uh/wm/runner.rb @@ -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,