Refactor LogPrinter to make it immutable and remove logic from the constructor.

Signed-off-by: Daniel Nephin <dnephin@gmail.com>
This commit is contained in:
Daniel Nephin 2015-09-09 18:26:34 -04:00 committed by Daniel Nephin
parent 413b76e228
commit 7d8ae9aa6d

View File

@ -4,8 +4,6 @@ from __future__ import unicode_literals
import sys import sys
from itertools import cycle from itertools import cycle
from six import next
from . import colors from . import colors
from .multiplexer import Multiplexer from .multiplexer import Multiplexer
from .utils import split_buffer from .utils import split_buffer
@ -13,82 +11,75 @@ from compose import utils
class LogPrinter(object): class LogPrinter(object):
# TODO: move logic to run """Print logs from many containers to a single output stream."""
def __init__(self, containers, output=sys.stdout, monochrome=False): def __init__(self, containers, output=sys.stdout, monochrome=False):
self.containers = containers self.containers = containers
self.prefix_width = self._calculate_prefix_width(containers)
self.generators = self._make_log_generators(monochrome)
self.output = utils.get_output_stream(output) self.output = utils.get_output_stream(output)
self.monochrome = monochrome
def run(self): def run(self):
mux = Multiplexer(self.generators) if not self.containers:
for line in mux.loop(): return
prefix_width = max_name_width(self.containers)
generators = list(self._make_log_generators(self.monochrome, prefix_width))
for line in Multiplexer(generators).loop():
self.output.write(line) self.output.write(line)
# TODO: doesn't use self, remove from class def _make_log_generators(self, monochrome, prefix_width):
def _calculate_prefix_width(self, containers):
"""
Calculate the maximum width of container names so we can make the log
prefixes line up like so:
db_1 | Listening
web_1 | Listening
"""
prefix_width = 0
for container in containers:
prefix_width = max(prefix_width, len(container.name_without_project))
return prefix_width
def _make_log_generators(self, monochrome):
color_fns = cycle(colors.rainbow())
generators = []
def no_color(text): def no_color(text):
return text return text
for container in self.containers: if monochrome:
if monochrome: color_funcs = cycle([no_color])
color_fn = no_color else:
else: color_funcs = cycle(colors.rainbow())
color_fn = next(color_fns)
generators.append(self._make_log_generator(container, color_fn))
return generators for color_func, container in zip(color_funcs, self.containers):
generator_func = get_log_generator(container)
def _make_log_generator(self, container, color_fn): prefix = color_func(build_log_prefix(container, prefix_width))
prefix = color_fn(self._generate_prefix(container)) yield generator_func(container, prefix, color_func)
if container.has_api_logs:
return build_log_generator(container, prefix, color_fn)
return build_no_log_generator(container, prefix, color_fn)
def _generate_prefix(self, container):
"""
Generate the prefix for a log line without colour
"""
name = container.name_without_project
padding = ' ' * (self.prefix_width - len(name))
return ''.join([name, padding, ' | '])
def build_no_log_generator(container, prefix, color_fn): def build_log_prefix(container, prefix_width):
return container.name_without_project.ljust(prefix_width) + ' | '
def max_name_width(containers):
"""Calculate the maximum width of container names so we can make the log
prefixes line up like so:
db_1 | Listening
web_1 | Listening
"""
return max(len(container.name_without_project) for container in containers)
def get_log_generator(container):
if container.has_api_logs:
return build_log_generator
return build_no_log_generator
def build_no_log_generator(container, prefix, color_func):
"""Return a generator that prints a warning about logs and waits for """Return a generator that prints a warning about logs and waits for
container to exit. container to exit.
""" """
yield "{} WARNING: no logs are available with the '{}' log driver\n".format( yield "{} WARNING: no logs are available with the '{}' log driver\n".format(
prefix, prefix,
container.log_driver) container.log_driver)
yield color_fn(wait_on_exit(container)) yield color_func(wait_on_exit(container))
def build_log_generator(container, prefix, color_fn): def build_log_generator(container, prefix, color_func):
# Attach to container before log printer starts running # Attach to container before log printer starts running
stream = container.attach(stdout=True, stderr=True, stream=True, logs=True) stream = container.attach(stdout=True, stderr=True, stream=True, logs=True)
line_generator = split_buffer(stream, u'\n') line_generator = split_buffer(stream, u'\n')
for line in line_generator: for line in line_generator:
yield prefix + line yield prefix + line
yield color_fn(wait_on_exit(container)) yield color_func(wait_on_exit(container))
def wait_on_exit(container): def wait_on_exit(container):