From c80b2774d45adaadf68ed53d935f6be38e0d021c Mon Sep 17 00:00:00 2001
From: Einar Forselv <eforselv@gmail.com>
Date: Fri, 6 Dec 2019 17:19:51 +0100
Subject: [PATCH] Better handling of stdout/stderr logging

---
 src/restic_compose_backup/commands.py | 32 +++++++++++++++++++++------
 src/restic_compose_backup/restic.py   | 18 +++++++--------
 2 files changed, 34 insertions(+), 16 deletions(-)

diff --git a/src/restic_compose_backup/commands.py b/src/restic_compose_backup/commands.py
index 706dd85..cf209cb 100644
--- a/src/restic_compose_backup/commands.py
+++ b/src/restic_compose_backup/commands.py
@@ -53,14 +53,12 @@ def run(cmd: List[str]) -> int:
     child = Popen(cmd, stdout=PIPE, stderr=PIPE)
     stdoutdata, stderrdata = child.communicate()
 
-    if stdoutdata:
-        logger.debug(stdoutdata.decode().strip())
-        logger.debug('-' * 28)
+    if stdoutdata.strip():
+        log_std('stdout', stdoutdata.decode(),
+                logging.DEBUG if child.returncode == 0 else logging.ERROR)
 
-    if stderrdata:
-        logger.error('%s STDERR %s', '-' * 10, '-' * 10)
-        logger.error(stderrdata.decode().strip())
-        logger.error('-' * 28)
+    if stderrdata.strip():
+        log_std('stderr', stderrdata.decode(), logging.ERROR)
 
     logger.debug("returncode %s", child.returncode)
     return child.returncode
@@ -71,3 +69,23 @@ def run_capture_std(cmd: List[str]) -> Tuple[str, str]:
     logger.debug('cmd: %s', ' '.join(cmd))
     child = Popen(cmd, stdout=PIPE, stderr=PIPE)
     return child.communicate()
+
+
+def log_std(source: str, data: str, level: int):
+    if isinstance(data, bytes):
+        data = data.decode()
+
+    if not data.strip():
+        return
+
+    log_func = logger.debug if level == logging.DEBUG else logger.error
+    log_func('%s %s %s', '-' * 10, source, '-' * 10)
+
+    lines = data.split('\n')
+    if lines[-1] == '':
+        lines.pop()
+
+    for line in lines:
+        log_func(line)
+
+    log_func('-' * 28)
diff --git a/src/restic_compose_backup/restic.py b/src/restic_compose_backup/restic.py
index 7eb23cc..11fb760 100644
--- a/src/restic_compose_backup/restic.py
+++ b/src/restic_compose_backup/restic.py
@@ -47,17 +47,17 @@ def backup_from_stdin(repository: str, filename: str, source_command: List[str])
     dest_process = Popen(dest_command, stdin=source_process.stdout, stdout=PIPE, stderr=PIPE)
     stdout, stderr = dest_process.communicate()
 
-    if stdout:
-        for line in stdout.decode().split('\n'):
-            logger.debug(line)
-
-    if stderr:
-        for line in stderr.decode().split('\n'):
-            logger.error(line)
-
     # Ensure both processes exited with code 0
     source_exit, dest_exit = source_process.poll(), dest_process.poll()
-    return 0 if (source_exit == 0 and dest_exit == 0) else 1
+    exit_code = 0 if (source_exit == 0 and dest_exit == 0) else 1
+
+    if stdout:
+        commands.log_std('stdout', stdout, logging.DEBUG if exit_code == 0 else logging.ERROR)
+
+    if stderr:
+        commands.log_std('stderr', stderr, logging.ERROR)
+
+    return exit_code
 
 
 def snapshots(repository: str, last=True) -> Tuple[str, str]: