tout.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. # SPDX-License-Identifier: GPL-2.0+
  2. # Copyright (c) 2016 Google, Inc
  3. #
  4. # Terminal output logging.
  5. #
  6. import sys
  7. from patman import terminal
  8. # Output verbosity levels that we support
  9. ERROR, WARNING, NOTICE, INFO, DETAIL, DEBUG = range(6)
  10. in_progress = False
  11. """
  12. This class handles output of progress and other useful information
  13. to the user. It provides for simple verbosity level control and can
  14. output nothing but errors at verbosity zero.
  15. The idea is that modules set up an Output object early in their years and pass
  16. it around to other modules that need it. This keeps the output under control
  17. of a single class.
  18. Public properties:
  19. verbose: Verbosity level: 0=silent, 1=progress, 3=full, 4=debug
  20. """
  21. def __enter__():
  22. return
  23. def __exit__(unused1, unused2, unused3):
  24. """Clean up and remove any progress message."""
  25. ClearProgress()
  26. return False
  27. def UserIsPresent():
  28. """This returns True if it is likely that a user is present.
  29. Sometimes we want to prompt the user, but if no one is there then this
  30. is a waste of time, and may lock a script which should otherwise fail.
  31. Returns:
  32. True if it thinks the user is there, and False otherwise
  33. """
  34. return stdout_is_tty and verbose > 0
  35. def ClearProgress():
  36. """Clear any active progress message on the terminal."""
  37. global in_progress
  38. if verbose > 0 and stdout_is_tty and in_progress:
  39. _stdout.write('\r%s\r' % (" " * len (_progress)))
  40. _stdout.flush()
  41. in_progress = False
  42. def Progress(msg, warning=False, trailer='...'):
  43. """Display progress information.
  44. Args:
  45. msg: Message to display.
  46. warning: True if this is a warning."""
  47. global in_progress
  48. ClearProgress()
  49. if verbose > 0:
  50. _progress = msg + trailer
  51. if stdout_is_tty:
  52. col = _color.YELLOW if warning else _color.GREEN
  53. _stdout.write('\r' + _color.Color(col, _progress))
  54. _stdout.flush()
  55. in_progress = True
  56. else:
  57. _stdout.write(_progress + '\n')
  58. def _Output(level, msg, color=None):
  59. """Output a message to the terminal.
  60. Args:
  61. level: Verbosity level for this message. It will only be displayed if
  62. this as high as the currently selected level.
  63. msg; Message to display.
  64. error: True if this is an error message, else False.
  65. """
  66. if verbose >= level:
  67. ClearProgress()
  68. if color:
  69. msg = _color.Color(color, msg)
  70. if level < NOTICE:
  71. print(msg, file=sys.stderr)
  72. else:
  73. print(msg)
  74. def DoOutput(level, msg):
  75. """Output a message to the terminal.
  76. Args:
  77. level: Verbosity level for this message. It will only be displayed if
  78. this as high as the currently selected level.
  79. msg; Message to display.
  80. """
  81. _Output(level, msg)
  82. def Error(msg):
  83. """Display an error message
  84. Args:
  85. msg; Message to display.
  86. """
  87. _Output(ERROR, msg, _color.RED)
  88. def Warning(msg):
  89. """Display a warning message
  90. Args:
  91. msg; Message to display.
  92. """
  93. _Output(WARNING, msg, _color.YELLOW)
  94. def Notice(msg):
  95. """Display an important infomation message
  96. Args:
  97. msg; Message to display.
  98. """
  99. _Output(NOTICE, msg)
  100. def Info(msg):
  101. """Display an infomation message
  102. Args:
  103. msg; Message to display.
  104. """
  105. _Output(INFO, msg)
  106. def Detail(msg):
  107. """Display a detailed message
  108. Args:
  109. msg; Message to display.
  110. """
  111. _Output(DETAIL, msg)
  112. def Debug(msg):
  113. """Display a debug message
  114. Args:
  115. msg; Message to display.
  116. """
  117. _Output(DEBUG, msg)
  118. def UserOutput(msg):
  119. """Display a message regardless of the current output level.
  120. This is used when the output was specifically requested by the user.
  121. Args:
  122. msg; Message to display.
  123. """
  124. _Output(0, msg)
  125. def Init(_verbose=WARNING, stdout=sys.stdout):
  126. """Initialize a new output object.
  127. Args:
  128. verbose: Verbosity level (0-4).
  129. stdout: File to use for stdout.
  130. """
  131. global verbose, _progress, _color, _stdout, stdout_is_tty
  132. verbose = _verbose
  133. _progress = '' # Our last progress message
  134. _color = terminal.Color()
  135. _stdout = stdout
  136. # TODO(sjg): Move this into Chromite libraries when we have them
  137. stdout_is_tty = hasattr(sys.stdout, 'isatty') and sys.stdout.isatty()
  138. stderr_is_tty = hasattr(sys.stderr, 'isatty') and sys.stderr.isatty()
  139. def Uninit():
  140. ClearProgress()
  141. Init()