summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndy Grover <agrover@redhat.com>2014-03-03 16:25:26 -0800
committerAndy Grover <agrover@redhat.com>2014-03-03 16:25:26 -0800
commit6068a76699ae45afd96e6645f9c94f7f85c121b8 (patch)
treedad1ff7d5ff1adecedc5ca927ed7f318f49e3840
parent283db6ba1cd32f4d34b8cf791502bf3497a04c3e (diff)
downloadconfigshell-fb-6068a76699ae45afd96e6645f9c94f7f85c121b8.tar.gz
Make error-level log messages go to stderr
Extend con.display() with an error boolean optional parameter. If true, output goes to stderr. Is this in log._log() to send messages >= 'error' to stderr. This is based on work by Tregaron Bayly and Jason Earl at Bluehost, thanks. Signed-off-by: Andy Grover <agrover@redhat.com>
-rw-r--r--configshell/console.py21
-rw-r--r--configshell/log.py6
2 files changed, 19 insertions, 8 deletions
diff --git a/configshell/console.py b/configshell/console.py
index 061853d..6e2cd1d 100644
--- a/configshell/console.py
+++ b/configshell/console.py
@@ -56,7 +56,7 @@ class Console(object):
__borg_state = {}
- def __init__(self, stdin=sys.stdin, stdout=sys.stdout):
+ def __init__(self, stdin=sys.stdin, stdout=sys.stdout, stderr=sys.stderr):
'''
Initializes a Console instance.
@param stdin: The console standard input.
@@ -67,6 +67,7 @@ class Console(object):
self.__dict__ = self.__borg_state
self._stdout = stdout
self._stdin = stdin
+ self._stderr = stderr
self.prefs = prefs.Prefs()
# Public methods
@@ -130,16 +131,16 @@ class Console(object):
'''
self.escape("%d;%dH" % (ypos, xpos))
- def raw_write(self, text):
+ def raw_write(self, text, output=self._stdout):
'''
Raw console printing function.
@param text: The text to print.
@type text: str
'''
- self._stdout.write(text)
- self._stdout.flush()
+ output.write(text)
+ output.flush()
- def display(self, text, no_lf=False):
+ def display(self, text, no_lf=False, error=False):
'''
Display a text with a default style.
@param text: Text to display
@@ -148,9 +149,15 @@ class Console(object):
@type no_lf: bool
'''
text = self.render_text(text)
- self.raw_write(text)
+
+ if error:
+ output = self._stderr
+ else:
+ output = self._stdout
+
+ self.raw_write(text, output=output)
if not no_lf:
- self.raw_write('\n')
+ self.raw_write('\n', output=output)
def epy_write(self, text):
'''
diff --git a/configshell/log.py b/configshell/log.py
index 67527e1..e111d78 100644
--- a/configshell/log.py
+++ b/configshell/log.py
@@ -20,6 +20,7 @@ import time
import prefs
import inspect
import console
+import sys
import traceback
class Log(object):
@@ -109,7 +110,10 @@ class Log(object):
msg = self.con.render_text(msg, self.colors[level])
else:
msg = "%s: %s" % (level.capitalize(), msg)
- self.con.display(msg)
+ error = False
+ if self.levels.index(level) <= self.levels.index('error'):
+ error = True
+ self.con.display(msg, error=error)
# Public methods