log_printer.py 3.1 KB

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