10faf6144SSimon Glass# SPDX-License-Identifier: GPL-2.0+ 283d290c5STom Rini# Copyright (c) 2016 Google, Inc 30faf6144SSimon Glass# 40faf6144SSimon Glass# Terminal output logging. 50faf6144SSimon Glass# 60faf6144SSimon Glass 70faf6144SSimon Glassimport sys 80faf6144SSimon Glass 90faf6144SSimon Glassimport terminal 100faf6144SSimon Glass 110faf6144SSimon Glass# Output verbosity levels that we support 120faf6144SSimon GlassERROR = 0 130faf6144SSimon GlassWARNING = 1 140faf6144SSimon GlassNOTICE = 2 150faf6144SSimon GlassINFO = 3 160faf6144SSimon GlassDEBUG = 4 170faf6144SSimon Glass 18*008b0300SSimon Glassin_progress = False 19*008b0300SSimon Glass 200faf6144SSimon Glass""" 210faf6144SSimon GlassThis class handles output of progress and other useful information 220faf6144SSimon Glassto the user. It provides for simple verbosity level control and can 230faf6144SSimon Glassoutput nothing but errors at verbosity zero. 240faf6144SSimon Glass 250faf6144SSimon GlassThe idea is that modules set up an Output object early in their years and pass 260faf6144SSimon Glassit around to other modules that need it. This keeps the output under control 270faf6144SSimon Glassof a single class. 280faf6144SSimon Glass 290faf6144SSimon GlassPublic properties: 300faf6144SSimon Glass verbose: Verbosity level: 0=silent, 1=progress, 3=full, 4=debug 310faf6144SSimon Glass""" 320faf6144SSimon Glassdef __enter__(): 330faf6144SSimon Glass return 340faf6144SSimon Glass 350faf6144SSimon Glassdef __exit__(unused1, unused2, unused3): 360faf6144SSimon Glass """Clean up and remove any progress message.""" 370faf6144SSimon Glass ClearProgress() 380faf6144SSimon Glass return False 390faf6144SSimon Glass 400faf6144SSimon Glassdef UserIsPresent(): 410faf6144SSimon Glass """This returns True if it is likely that a user is present. 420faf6144SSimon Glass 430faf6144SSimon Glass Sometimes we want to prompt the user, but if no one is there then this 440faf6144SSimon Glass is a waste of time, and may lock a script which should otherwise fail. 450faf6144SSimon Glass 460faf6144SSimon Glass Returns: 470faf6144SSimon Glass True if it thinks the user is there, and False otherwise 480faf6144SSimon Glass """ 490faf6144SSimon Glass return stdout_is_tty and verbose > 0 500faf6144SSimon Glass 510faf6144SSimon Glassdef ClearProgress(): 520faf6144SSimon Glass """Clear any active progress message on the terminal.""" 53*008b0300SSimon Glass global in_progress 54*008b0300SSimon Glass if verbose > 0 and stdout_is_tty and in_progress: 550faf6144SSimon Glass _stdout.write('\r%s\r' % (" " * len (_progress))) 560faf6144SSimon Glass _stdout.flush() 57*008b0300SSimon Glass in_progress = False 580faf6144SSimon Glass 590faf6144SSimon Glassdef Progress(msg, warning=False, trailer='...'): 600faf6144SSimon Glass """Display progress information. 610faf6144SSimon Glass 620faf6144SSimon Glass Args: 630faf6144SSimon Glass msg: Message to display. 640faf6144SSimon Glass warning: True if this is a warning.""" 65*008b0300SSimon Glass global in_progress 660faf6144SSimon Glass ClearProgress() 670faf6144SSimon Glass if verbose > 0: 680faf6144SSimon Glass _progress = msg + trailer 690faf6144SSimon Glass if stdout_is_tty: 700faf6144SSimon Glass col = _color.YELLOW if warning else _color.GREEN 710faf6144SSimon Glass _stdout.write('\r' + _color.Color(col, _progress)) 720faf6144SSimon Glass _stdout.flush() 73*008b0300SSimon Glass in_progress = True 740faf6144SSimon Glass else: 750faf6144SSimon Glass _stdout.write(_progress + '\n') 760faf6144SSimon Glass 770faf6144SSimon Glassdef _Output(level, msg, color=None): 780faf6144SSimon Glass """Output a message to the terminal. 790faf6144SSimon Glass 800faf6144SSimon Glass Args: 810faf6144SSimon Glass level: Verbosity level for this message. It will only be displayed if 820faf6144SSimon Glass this as high as the currently selected level. 830faf6144SSimon Glass msg; Message to display. 840faf6144SSimon Glass error: True if this is an error message, else False. 850faf6144SSimon Glass """ 860faf6144SSimon Glass if verbose >= level: 870faf6144SSimon Glass ClearProgress() 880faf6144SSimon Glass if color: 890faf6144SSimon Glass msg = _color.Color(color, msg) 900faf6144SSimon Glass _stdout.write(msg + '\n') 910faf6144SSimon Glass 920faf6144SSimon Glassdef DoOutput(level, msg): 930faf6144SSimon Glass """Output a message to the terminal. 940faf6144SSimon Glass 950faf6144SSimon Glass Args: 960faf6144SSimon Glass level: Verbosity level for this message. It will only be displayed if 970faf6144SSimon Glass this as high as the currently selected level. 980faf6144SSimon Glass msg; Message to display. 990faf6144SSimon Glass """ 1000faf6144SSimon Glass _Output(level, msg) 1010faf6144SSimon Glass 1020faf6144SSimon Glassdef Error(msg): 1030faf6144SSimon Glass """Display an error message 1040faf6144SSimon Glass 1050faf6144SSimon Glass Args: 1060faf6144SSimon Glass msg; Message to display. 1070faf6144SSimon Glass """ 1080faf6144SSimon Glass _Output(0, msg, _color.RED) 1090faf6144SSimon Glass 1100faf6144SSimon Glassdef Warning(msg): 1110faf6144SSimon Glass """Display a warning message 1120faf6144SSimon Glass 1130faf6144SSimon Glass Args: 1140faf6144SSimon Glass msg; Message to display. 1150faf6144SSimon Glass """ 1160faf6144SSimon Glass _Output(1, msg, _color.YELLOW) 1170faf6144SSimon Glass 1180faf6144SSimon Glassdef Notice(msg): 1190faf6144SSimon Glass """Display an important infomation message 1200faf6144SSimon Glass 1210faf6144SSimon Glass Args: 1220faf6144SSimon Glass msg; Message to display. 1230faf6144SSimon Glass """ 1240faf6144SSimon Glass _Output(2, msg) 1250faf6144SSimon Glass 1260faf6144SSimon Glassdef Info(msg): 1270faf6144SSimon Glass """Display an infomation message 1280faf6144SSimon Glass 1290faf6144SSimon Glass Args: 1300faf6144SSimon Glass msg; Message to display. 1310faf6144SSimon Glass """ 1320faf6144SSimon Glass _Output(3, msg) 1330faf6144SSimon Glass 1340faf6144SSimon Glassdef Debug(msg): 1350faf6144SSimon Glass """Display a debug message 1360faf6144SSimon Glass 1370faf6144SSimon Glass Args: 1380faf6144SSimon Glass msg; Message to display. 1390faf6144SSimon Glass """ 1400faf6144SSimon Glass _Output(4, msg) 1410faf6144SSimon Glass 1420faf6144SSimon Glassdef UserOutput(msg): 1430faf6144SSimon Glass """Display a message regardless of the current output level. 1440faf6144SSimon Glass 1450faf6144SSimon Glass This is used when the output was specifically requested by the user. 1460faf6144SSimon Glass Args: 1470faf6144SSimon Glass msg; Message to display. 1480faf6144SSimon Glass """ 1490faf6144SSimon Glass _Output(0, msg) 1500faf6144SSimon Glass 1510faf6144SSimon Glassdef Init(_verbose=WARNING, stdout=sys.stdout): 1520faf6144SSimon Glass """Initialize a new output object. 1530faf6144SSimon Glass 1540faf6144SSimon Glass Args: 1550faf6144SSimon Glass verbose: Verbosity level (0-4). 1560faf6144SSimon Glass stdout: File to use for stdout. 1570faf6144SSimon Glass """ 1580faf6144SSimon Glass global verbose, _progress, _color, _stdout, stdout_is_tty 1590faf6144SSimon Glass 1600faf6144SSimon Glass verbose = _verbose 1610faf6144SSimon Glass _progress = '' # Our last progress message 1620faf6144SSimon Glass _color = terminal.Color() 1630faf6144SSimon Glass _stdout = stdout 1640faf6144SSimon Glass 1650faf6144SSimon Glass # TODO(sjg): Move this into Chromite libraries when we have them 1660faf6144SSimon Glass stdout_is_tty = hasattr(sys.stdout, 'isatty') and sys.stdout.isatty() 1670faf6144SSimon Glass 1680faf6144SSimon Glassdef Uninit(): 1690faf6144SSimon Glass ClearProgress() 1700faf6144SSimon Glass 1710faf6144SSimon GlassInit() 172