add logger
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
diff --git a/lib/mpd_client.rb b/lib/mpd_client.rb
index 403eb50..2f5336c 100644
--- a/lib/mpd_client.rb
+++ b/lib/mpd_client.rb
@@ -82,8 +82,8 @@ COMMANDS = {
"listallinfo" => "fetch_database",
"lsinfo" => "fetch_database",
"search" => "fetch_songs",
- "searchadd" => "fetch_nothing",
- "searchaddp1" => "fetch_nothing",
+ "searchadd" => "fetch_nothing",
+ "searchaddp1" => "fetch_nothing",
"update" => "fetch_item",
"rescan" => "fetch_item",
# Sticker Commands
@@ -116,9 +116,40 @@ COMMANDS = {
"sendmessage" => "fetch_nothing"
}
+# The MPDClient library is used for interactions with a MPD.
+#
+# == Example
+#
+# require 'mpd_client'
+# require 'logger'
+#
+# client = MPDClient.new
+# client.log = Logger.new($stderr)
+# client.connect('/var/run/mpd/socket')
+#
class MPDClient
attr_reader :mpd_version
+ class << self
+ # Default logger for all MPDClient instances
+ #
+ # MPDClient.log = Logger.new($stderr)
+ #
+ attr_accessor :log
+
+ def add_command(name, retval)
+ escaped_name = name.gsub(' ', '_')
+ define_method escaped_name.to_sym do |*args|
+ execute(name, *args, retval)
+ end
+ end
+
+ def remove_command(name)
+ raise "Can't remove not existent '#{name}' command" unless method_defined? name.to_sym
+ remove_method name.to_sym
+ end
+ end
+
def initialize
reset
end
@@ -152,19 +183,20 @@ class MPDClient
return fetch_command_list
end
- class << self
- def add_command(name, retval)
- define_method name.to_sym do |*args|
- execute(name, *args, retval)
- end
- end
+ # Sets the +logger+ used by this instance of MPDClient
+ #
+ def log
+ @log || MPDClient.log
+ end
- def remove_command(name)
- raise "Can't remove not existent '#{name}' command" unless method_defined? name.to_sym
- remove_method name.to_sym
- end
+ # Sets the +logger+ used by this instance of MPDClient
+ #
+ def log= logger
+ @log = logger
end
+
+
private
def execute(command, *args, retval)
@@ -178,6 +210,7 @@ class MPDClient
end
def write_line(line)
+ log.debug("MPD command: #{line}") if log
@socket.puts line
@socket.flush
end
@@ -327,6 +360,7 @@ class MPDClient
@mpd_version = nil
@command_list = nil
@socket = nil
+ @log = nil
end
def escape(text)