This commit is contained in:
Edwin Eefting 2021-04-13 17:07:01 +02:00
parent bef3be4955
commit 85d493469d
3 changed files with 25 additions and 25 deletions

View File

@ -8,17 +8,17 @@ class TestLog(unittest2.TestCase):
"""test with color output""" """test with color output"""
with OutputIO() as buf: with OutputIO() as buf:
with redirect_stdout(buf): with redirect_stdout(buf):
l=LogConsole(show_verbose=False, show_debug=False) l=LogConsole(show_verbose=False, show_debug=False, color=True)
l.verbose("verbose") l.verbose("verbose")
l.debug("debug") l.debug("debug")
with redirect_stdout(buf): with redirect_stdout(buf):
l=LogConsole(show_verbose=True, show_debug=True) l=LogConsole(show_verbose=True, show_debug=True, color=True)
l.verbose("verbose") l.verbose("verbose")
l.debug("debug") l.debug("debug")
with redirect_stderr(buf): with redirect_stderr(buf):
l=LogConsole() l=LogConsole(show_verbose=False, show_debug=False, color=True)
l.error("error") l.error("error")
print(list(buf.getvalue())) print(list(buf.getvalue()))
@ -27,21 +27,19 @@ class TestLog(unittest2.TestCase):
def test_nocolor(self): def test_nocolor(self):
"""test without color output""" """test without color output"""
zfs_autobackup.LogConsole.colorama=False
with OutputIO() as buf: with OutputIO() as buf:
with redirect_stdout(buf): with redirect_stdout(buf):
l=LogConsole(show_verbose=False, show_debug=False) l=LogConsole(show_verbose=False, show_debug=False, color=False)
l.verbose("verbose") l.verbose("verbose")
l.debug("debug") l.debug("debug")
with redirect_stdout(buf): with redirect_stdout(buf):
l=LogConsole(show_verbose=True, show_debug=True) l=LogConsole(show_verbose=True, show_debug=True, color=False)
l.verbose("verbose") l.verbose("verbose")
l.debug("debug") l.debug("debug")
with redirect_stderr(buf): with redirect_stderr(buf):
l=LogConsole() l=LogConsole(show_verbose=False, show_debug=False, color=False)
l.error("error") l.error("error")
print(list(buf.getvalue())) print(list(buf.getvalue()))

View File

@ -3,27 +3,29 @@ from __future__ import print_function
import sys import sys
colorama = False
if sys.stdout.isatty():
try:
import colorama
except ImportError:
colorama = False
pass
class LogConsole: class LogConsole:
"""Log-class that outputs to console, adding colors if needed""" """Log-class that outputs to console, adding colors if needed"""
def __init__(self, show_debug=False, show_verbose=False): def __init__(self, show_debug, show_verbose, color):
self.last_log = "" self.last_log = ""
self.show_debug = show_debug self.show_debug = show_debug
self.show_verbose = show_verbose self.show_verbose = show_verbose
@staticmethod if color:
def error(txt): # try to use color, failback if colorama not available
if colorama: self.colorama=False
try:
import colorama
global colorama
self.colorama = True
except ImportError:
pass
else:
self.colorama=False
def error(self, txt):
if self.colorama:
print(colorama.Fore.RED + colorama.Style.BRIGHT + "! " + txt + colorama.Style.RESET_ALL, file=sys.stderr) print(colorama.Fore.RED + colorama.Style.BRIGHT + "! " + txt + colorama.Style.RESET_ALL, file=sys.stderr)
else: else:
print("! " + txt, file=sys.stderr) print("! " + txt, file=sys.stderr)
@ -31,7 +33,7 @@ class LogConsole:
def verbose(self, txt): def verbose(self, txt):
if self.show_verbose: if self.show_verbose:
if colorama: if self.colorama:
print(colorama.Style.NORMAL + " " + txt + colorama.Style.RESET_ALL) print(colorama.Style.NORMAL + " " + txt + colorama.Style.RESET_ALL)
else: else:
print(" " + txt) print(" " + txt)
@ -39,7 +41,7 @@ class LogConsole:
def debug(self, txt): def debug(self, txt):
if self.show_debug: if self.show_debug:
if colorama: if self.colorama:
print(colorama.Fore.GREEN + "# " + txt + colorama.Style.RESET_ALL) print(colorama.Fore.GREEN + "# " + txt + colorama.Style.RESET_ALL)
else: else:
print("# " + txt) print("# " + txt)

View File

@ -131,7 +131,7 @@ class ZfsAutobackup:
if args.destroy_incompatible: if args.destroy_incompatible:
args.rollback = True args.rollback = True
self.log = LogConsole(show_debug=self.args.debug, show_verbose=self.args.verbose) self.log = LogConsole(show_debug=self.args.debug, show_verbose=self.args.verbose, color=sys.stdout.isatty())
if args.resume: if args.resume:
self.verbose("NOTE: The --resume option isn't needed anymore (its autodetected now)") self.verbose("NOTE: The --resume option isn't needed anymore (its autodetected now)")