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