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"""
with OutputIO() as 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.debug("debug")
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.debug("debug")
with redirect_stderr(buf):
l=LogConsole()
l=LogConsole(show_verbose=False, show_debug=False, color=True)
l.error("error")
print(list(buf.getvalue()))
@ -27,21 +27,19 @@ class TestLog(unittest2.TestCase):
def test_nocolor(self):
"""test without color output"""
zfs_autobackup.LogConsole.colorama=False
with OutputIO() as 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.debug("debug")
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.debug("debug")
with redirect_stderr(buf):
l=LogConsole()
l=LogConsole(show_verbose=False, show_debug=False, color=False)
l.error("error")
print(list(buf.getvalue()))

View File

@ -3,27 +3,29 @@ from __future__ import print_function
import sys
colorama = False
if sys.stdout.isatty():
try:
import colorama
except ImportError:
colorama = False
pass
class LogConsole:
"""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.show_debug = show_debug
self.show_verbose = show_verbose
@staticmethod
def error(txt):
if colorama:
if color:
# try to use color, failback if colorama not available
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)
else:
print("! " + txt, file=sys.stderr)
@ -31,7 +33,7 @@ class LogConsole:
def verbose(self, txt):
if self.show_verbose:
if colorama:
if self.colorama:
print(colorama.Style.NORMAL + " " + txt + colorama.Style.RESET_ALL)
else:
print(" " + txt)
@ -39,7 +41,7 @@ class LogConsole:
def debug(self, txt):
if self.show_debug:
if colorama:
if self.colorama:
print(colorama.Fore.GREEN + "# " + txt + colorama.Style.RESET_ALL)
else:
print("# " + txt)

View File

@ -131,7 +131,7 @@ class ZfsAutobackup:
if args.destroy_incompatible:
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:
self.verbose("NOTE: The --resume option isn't needed anymore (its autodetected now)")