Merge "Fix the prefix of messages caputured from stderr"

This commit is contained in:
Jenkins 2015-03-16 20:28:27 +00:00 committed by Gerrit Code Review
commit 2fa4906cb8
2 changed files with 49 additions and 43 deletions

View File

@ -1062,19 +1062,21 @@ class NullLogger(object):
class LoggerFileObject(object): class LoggerFileObject(object):
def __init__(self, logger): def __init__(self, logger, log_type='STDOUT'):
self.logger = logger self.logger = logger
self.log_type = log_type
def write(self, value): def write(self, value):
value = value.strip() value = value.strip()
if value: if value:
if 'Connection reset by peer' in value: if 'Connection reset by peer' in value:
self.logger.error(_('STDOUT: Connection reset by peer')) self.logger.error(
_('%s: Connection reset by peer'), self.log_type)
else: else:
self.logger.error(_('STDOUT: %s'), value) self.logger.error(_('%s: %s'), self.log_type, value)
def writelines(self, values): def writelines(self, values):
self.logger.error(_('STDOUT: %s'), '#012'.join(values)) self.logger.error(_('%s: %s'), self.log_type, '#012'.join(values))
def close(self): def close(self):
pass pass
@ -1641,7 +1643,7 @@ def capture_stdio(logger, **kwargs):
if kwargs.pop('capture_stdout', True): if kwargs.pop('capture_stdout', True):
sys.stdout = LoggerFileObject(logger) sys.stdout = LoggerFileObject(logger)
if kwargs.pop('capture_stderr', True): if kwargs.pop('capture_stderr', True):
sys.stderr = LoggerFileObject(logger) sys.stderr = LoggerFileObject(logger, 'STDERR')
def parse_options(parser=None, once=False, test_args=None): def parse_options(parser=None, once=False, test_args=None):

View File

@ -1043,35 +1043,39 @@ class TestUtils(unittest.TestCase):
handler = logging.StreamHandler(sio) handler = logging.StreamHandler(sio)
logger = logging.getLogger() logger = logging.getLogger()
logger.addHandler(handler) logger.addHandler(handler)
lfo = utils.LoggerFileObject(logger) lfo_stdout = utils.LoggerFileObject(logger)
lfo_stderr = utils.LoggerFileObject(logger)
lfo_stderr = utils.LoggerFileObject(logger, 'STDERR')
print 'test1' print 'test1'
self.assertEquals(sio.getvalue(), '') self.assertEquals(sio.getvalue(), '')
sys.stdout = lfo sys.stdout = lfo_stdout
print 'test2' print 'test2'
self.assertEquals(sio.getvalue(), 'STDOUT: test2\n') self.assertEquals(sio.getvalue(), 'STDOUT: test2\n')
sys.stderr = lfo sys.stderr = lfo_stderr
print >> sys.stderr, 'test4' print >> sys.stderr, 'test4'
self.assertEquals(sio.getvalue(), 'STDOUT: test2\nSTDOUT: test4\n') self.assertEquals(sio.getvalue(), 'STDOUT: test2\nSTDERR: test4\n')
sys.stdout = orig_stdout sys.stdout = orig_stdout
print 'test5' print 'test5'
self.assertEquals(sio.getvalue(), 'STDOUT: test2\nSTDOUT: test4\n') self.assertEquals(sio.getvalue(), 'STDOUT: test2\nSTDERR: test4\n')
print >> sys.stderr, 'test6' print >> sys.stderr, 'test6'
self.assertEquals(sio.getvalue(), 'STDOUT: test2\nSTDOUT: test4\n' self.assertEquals(sio.getvalue(), 'STDOUT: test2\nSTDERR: test4\n'
'STDOUT: test6\n') 'STDERR: test6\n')
sys.stderr = orig_stderr sys.stderr = orig_stderr
print 'test8' print 'test8'
self.assertEquals(sio.getvalue(), 'STDOUT: test2\nSTDOUT: test4\n' self.assertEquals(sio.getvalue(), 'STDOUT: test2\nSTDERR: test4\n'
'STDOUT: test6\n') 'STDERR: test6\n')
lfo.writelines(['a', 'b', 'c']) lfo_stdout.writelines(['a', 'b', 'c'])
self.assertEquals(sio.getvalue(), 'STDOUT: test2\nSTDOUT: test4\n' self.assertEquals(sio.getvalue(), 'STDOUT: test2\nSTDERR: test4\n'
'STDOUT: test6\nSTDOUT: a#012b#012c\n') 'STDERR: test6\nSTDOUT: a#012b#012c\n')
lfo.close() lfo_stdout.close()
lfo.write('d') lfo_stderr.close()
self.assertEquals(sio.getvalue(), 'STDOUT: test2\nSTDOUT: test4\n' lfo_stdout.write('d')
'STDOUT: test6\nSTDOUT: a#012b#012c\nSTDOUT: d\n') self.assertEquals(sio.getvalue(), 'STDOUT: test2\nSTDERR: test4\n'
lfo.flush() 'STDERR: test6\nSTDOUT: a#012b#012c\nSTDOUT: d\n')
self.assertEquals(sio.getvalue(), 'STDOUT: test2\nSTDOUT: test4\n' lfo_stdout.flush()
'STDOUT: test6\nSTDOUT: a#012b#012c\nSTDOUT: d\n') self.assertEquals(sio.getvalue(), 'STDOUT: test2\nSTDERR: test4\n'
'STDERR: test6\nSTDOUT: a#012b#012c\nSTDOUT: d\n')
for lfo in (lfo_stdout, lfo_stderr):
got_exc = False got_exc = False
try: try:
for line in lfo: for line in lfo: