Skip to content

Commit 5670994

Browse files
committed
Decode stdout/stderr before sending them to logger
In python 3, stdout/stderr are bytes objects, which get logged incorrectly unless they get decoded into strings.
1 parent 681faec commit 5670994

File tree

5 files changed

+16
-6
lines changed

5 files changed

+16
-6
lines changed

mpas_analysis/shared/analysis_task.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -509,11 +509,11 @@ class AnalysisFormatter(logging.Formatter): # {{{
509509

510510
# printing error messages without a prefix because they are sometimes
511511
# errors and sometimes only warnings sent to stderr
512-
err_fmt = "%(msg)s"
513512
dbg_fmt = "DEBUG: %(module)s: %(lineno)d: %(msg)s"
514513
info_fmt = "%(msg)s"
514+
err_fmt = info_fmt
515515

516-
def __init__(self, fmt="%(levelno)s: %(msg)s"):
516+
def __init__(self, fmt=info_fmt):
517517
logging.Formatter.__init__(self, fmt)
518518

519519
def format(self, record):
@@ -561,7 +561,7 @@ def __init__(self, logger, log_level=logging.INFO):
561561

562562
def write(self, buf):
563563
for line in buf.rstrip().splitlines():
564-
self.logger.log(self.log_level, line.rstrip())
564+
self.logger.log(self.log_level, str(line.rstrip()))
565565

566566
def flush(self):
567567
pass

mpas_analysis/shared/climatology/mpas_climatology_task.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,8 +429,11 @@ def _compute_climatologies_with_ncclimo(self, inDirectory, outDirectory,
429429
stdout, stderr = process.communicate()
430430

431431
if stdout:
432-
self.logger.info(stdout)
432+
stdout = stdout.decode('utf-8')
433+
for line in stdout.split('\n'):
434+
self.logger.info(line)
433435
if stderr:
436+
stderr = stderr.decode('utf-8')
434437
for line in stderr.split('\n'):
435438
self.logger.error(line)
436439

mpas_analysis/shared/html/image_xml.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ def _provenance_command(root, history): # {{{
164164
p = subprocess.Popen(['git', 'describe', '--always', '--dirty'],
165165
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
166166
stdout, stderr = p.communicate()
167+
stdout = stdout.decode('utf-8')
167168
if p.returncode == 0:
168169
githash = stdout.strip('\n')
169170
else:

mpas_analysis/shared/interpolation/remapper.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,8 +319,11 @@ def remap_file(self, inFileName, outFileName, variableList=None,
319319
stdout, stderr = process.communicate()
320320

321321
if stdout:
322-
logger.info(stdout)
322+
stdout = stdout.decode('utf-8')
323+
for line in stdout.split('\n'):
324+
logger.info(line)
323325
if stderr:
326+
stderr = stderr.decode('utf-8')
324327
for line in stderr.split('\n'):
325328
logger.error(line)
326329

mpas_analysis/shared/time_series/mpas_time_series_task.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,8 +317,11 @@ def _compute_time_series_with_ncrcat(self):
317317
stdout, stderr = process.communicate()
318318

319319
if stdout:
320-
self.logger.info(stdout)
320+
stdout = stdout.decode('utf-8')
321+
for line in stdout.split('\n'):
322+
self.logger.info(line)
321323
if stderr:
324+
stderr = stderr.decode('utf-8')
322325
for line in stderr.split('\n'):
323326
self.logger.error(line)
324327

0 commit comments

Comments
 (0)