Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpd: use plugin logger instead of global logger #3196

Merged
merged 2 commits into from
Apr 1, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 20 additions & 29 deletions beetsplug/bpd/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import beets
from beets.plugins import BeetsPlugin
import beets.ui
from beets import logging
from beets import vfs
from beets.util import bluelet
from beets.library import Item
Expand Down Expand Up @@ -73,10 +72,6 @@

ITEM_KEYS_WRITABLE = set(MediaFile.fields()).intersection(Item._fields.keys())

# Loggers.
log = logging.getLogger('beets.bpd')
global_log = logging.getLogger('beets')


# Gstreamer import error.
class NoGstreamerError(Exception):
Expand Down Expand Up @@ -166,12 +161,13 @@ class BaseServer(object):
This is a generic superclass and doesn't support many commands.
"""

def __init__(self, host, port, password):
def __init__(self, host, port, password, log):
"""Create a new server bound to address `host` and listening
on port `port`. If `password` is given, it is required to do
anything significant on the server.
"""
self.host, self.port, self.password = host, port, password
self._log = log

# Default server values.
self.random = False
Expand Down Expand Up @@ -573,7 +569,9 @@ def send(self, lines):
if isinstance(lines, six.string_types):
lines = [lines]
out = NEWLINE.join(lines) + NEWLINE
log.debug('{}', out[:-1]) # Don't log trailing newline.
# Don't log trailing newline:
message = out[:-1].replace(u'\n', u'\n' + u' ' * 13)
self.server._log.debug('server: {}', message)
if isinstance(out, six.text_type):
out = out.encode('utf-8')
return self.sock.sendall(out)
Expand All @@ -594,6 +592,7 @@ def run(self):
"""Send a greeting to the client and begin processing commands
as they arrive.
"""
self.server._log.debug('New client connected')
yield self.send(HELLO)

clist = None # Initially, no command list is being constructed.
Expand All @@ -605,7 +604,8 @@ def run(self):
if not line:
break
line = line.decode('utf8') # MPD protocol uses UTF-8.
log.debug(u'{}', line)
message = line.replace(u'\n', u'\n' + u' ' * 13)
self.server._log.debug(u'client: {}', message)

if clist is not None:
# Command list already opened.
Expand Down Expand Up @@ -699,7 +699,7 @@ def run(self, conn):

except Exception as e:
# An "unintentional" error. Hide it from the client.
log.error('{}', traceback.format_exc(e))
conn.server._log.error('{}', traceback.format_exc(e))
raise BPDError(ERROR_SYSTEM, u'server error', self.name)


Expand Down Expand Up @@ -729,7 +729,7 @@ def run(self, conn):
e.index = i # Give the error the correct index.
raise e

# Otherwise, possibly send the output delimeter if we're in a
# Otherwise, possibly send the output delimiter if we're in a
# verbose ("OK") command list.
if self.verbose:
yield conn.send(RESP_CLIST_VERBOSE)
Expand All @@ -743,7 +743,7 @@ class Server(BaseServer):
to store its library.
"""

def __init__(self, library, host, port, password):
def __init__(self, library, host, port, password, log):
try:
from beetsplug.bpd import gstplayer
except ImportError as e:
Expand All @@ -752,7 +752,7 @@ def __init__(self, library, host, port, password):
raise NoGstreamerError()
else:
raise
super(Server, self).__init__(host, port, password)
super(Server, self).__init__(host, port, password, log)
self.lib = library
self.player = gstplayer.GstPlayer(self.play_finished)
self.cmd_update(None)
Expand Down Expand Up @@ -807,9 +807,9 @@ def cmd_update(self, conn, path=u'/'):
"""
# Path is ignored. Also, the real MPD does this asynchronously;
# this is done inline.
print(u'Building directory tree...')
self._log.debug(u'Building directory tree...')
self.tree = vfs.libtree(self.lib)
print(u'... done.')
self._log.debug(u'Finished building directory tree.')
self.updated_time = time.time()

# Path (directory tree) browsing.
Expand Down Expand Up @@ -1156,29 +1156,21 @@ def __init__(self):
})
self.config['password'].redact = True

def start_bpd(self, lib, host, port, password, volume, debug):
def start_bpd(self, lib, host, port, password, volume):
"""Starts a BPD server."""
if debug: # FIXME this should be managed by BeetsPlugin
self._log.setLevel(logging.DEBUG)
else:
self._log.setLevel(logging.WARNING)
try:
server = Server(lib, host, port, password)
server = Server(lib, host, port, password, self._log)
server.cmd_setvol(None, volume)
server.run()
except NoGstreamerError:
global_log.error(u'Gstreamer Python bindings not found.')
global_log.error(u'Install "gstreamer1.0" and "python-gi"'
u'or similar package to use BPD.')
self._log.error(u'Gstreamer Python bindings not found.')
self._log.error(u'Install "gstreamer1.0" and "python-gi"'
u'or similar package to use BPD.')

def commands(self):
cmd = beets.ui.Subcommand(
'bpd', help=u'run an MPD-compatible music player server'
)
cmd.parser.add_option(
'-d', '--debug', action='store_true',
help=u'dump all MPD traffic to stdout'
)

def func(lib, opts, args):
host = self.config['host'].as_str()
Expand All @@ -1188,8 +1180,7 @@ def func(lib, opts, args):
raise beets.ui.UserError(u'too many arguments')
password = self.config['password'].as_str()
volume = self.config['volume'].get(int)
debug = opts.debug or False
self.start_bpd(lib, host, int(port), password, volume, debug)
self.start_bpd(lib, host, int(port), password, volume)

cmd.func = func
return [cmd]