diff --git a/tests/test_log.py b/tests/test_log.py index 55571e5..0dbc4ab 100644 --- a/tests/test_log.py +++ b/tests/test_log.py @@ -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())) diff --git a/zfs_autobackup/LogConsole.py b/zfs_autobackup/LogConsole.py index a0abd6b..0a72942 100644 --- a/zfs_autobackup/LogConsole.py +++ b/zfs_autobackup/LogConsole.py @@ -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) diff --git a/zfs_autobackup/ZfsAutobackup.py b/zfs_autobackup/ZfsAutobackup.py index b384af3..6a60670 100644 --- a/zfs_autobackup/ZfsAutobackup.py +++ b/zfs_autobackup/ZfsAutobackup.py @@ -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)")