log_printer.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. from __future__ import absolute_import
  2. from __future__ import unicode_literals
  3. import sys
  4. from itertools import cycle
  5. from . import colors
  6. from .multiplexer import Multiplexer
  7. from compose import utils
  8. from compose.utils import split_buffer
  9. class LogPrinter(object):
  10. """Print logs from many containers to a single output stream."""
  11. def __init__(self, containers, output=sys.stdout, monochrome=False):
  12. self.containers = containers
  13. self.output = utils.get_output_stream(output)
  14. self.monochrome = monochrome
  15. def run(self):
  16. if not self.containers:
  17. return
  18. prefix_width = max_name_width(self.containers)
  19. generators = list(self._make_log_generators(self.monochrome, prefix_width))
  20. for line in Multiplexer(generators).loop():
  21. self.output.write(line)
  22. self.output.flush()
  23. def _make_log_generators(self, monochrome, prefix_width):
  24. def no_color(text):
  25. return text
  26. if monochrome:
  27. color_funcs = cycle([no_color])
  28. else:
  29. color_funcs = cycle(colors.rainbow())
  30. for color_func, container in zip(color_funcs, self.containers):
  31. generator_func = get_log_generator(container)
  32. prefix = color_func(build_log_prefix(container, prefix_width))
  33. yield generator_func(container, prefix, color_func)
  34. def build_log_prefix(container, prefix_width):
  35. return container.name_without_project.ljust(prefix_width) + ' | '
  36. def max_name_width(containers):
  37. """Calculate the maximum width of container names so we can make the log
  38. prefixes line up like so:
  39. db_1 | Listening
  40. web_1 | Listening
  41. """
  42. return max(len(container.name_without_project) for container in containers)
  43. def get_log_generator(container):
  44. if container.has_api_logs:
  45. return build_log_generator
  46. return build_no_log_generator
  47. def build_no_log_generator(container, prefix, color_func):
  48. """Return a generator that prints a warning about logs and waits for
  49. container to exit.
  50. """
  51. yield "{} WARNING: no logs are available with the '{}' log driver\n".format(
  52. prefix,
  53. container.log_driver)
  54. yield color_func(wait_on_exit(container))
  55. def build_log_generator(container, prefix, color_func):
  56. # if the container doesn't have a log_stream we need to attach to container
  57. # before log printer starts running
  58. if container.log_stream is None:
  59. stream = container.attach(stdout=True, stderr=True, stream=True, logs=True)
  60. line_generator = split_buffer(stream)
  61. else:
  62. line_generator = split_buffer(container.log_stream)
  63. for line in line_generator:
  64. yield prefix + line
  65. yield color_func(wait_on_exit(container))
  66. def wait_on_exit(container):
  67. exit_code = container.wait()
  68. return "%s exited with code %s\n" % (container.name, exit_code)