tout.py 4.0 KB

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