From 5b7486a6311a044aced1db7c8c99d83d7e578118 Mon Sep 17 00:00:00 2001
From: Jake Smith <jws52@cam.ac.uk>
Date: Mon, 22 Feb 2021 14:37:03 +0000
Subject: [PATCH] feat: subprocess option to choose log type

A useful function in ProcessorUtils is subprocess_and_log, which builds a subprocess command and write its stdout and stderr to the log handler. Beforehand, failures only ever generated an error message, but there are some use cases where another log type is preferred. This adds a log type option to the function, and uses it in one case where a warning is more appropriate.
---
 ProcessorComponents.py |  4 +++-
 ProcessorUtils.py      | 13 ++++++++++---
 2 files changed, 13 insertions(+), 4 deletions(-)

diff --git a/ProcessorComponents.py b/ProcessorComponents.py
index 8b0ebde..636a75f 100644
--- a/ProcessorComponents.py
+++ b/ProcessorComponents.py
@@ -155,7 +155,9 @@ def process_in_job_survey(jobPath,status,config,component):
     logger.debug('Performing ' + description_long)
 
     try:
-        subprocess_and_log(ODK_download,description_short,description_long,check=True)
+        # perform a pull from the ODK server, and if it fails write a warning message
+
+        subprocess_and_log(ODK_download,description_short,description_long,log_type='warning',check=True)
         
     except subprocess.CalledProcessError as e:
         status.reset('WARNING')
diff --git a/ProcessorUtils.py b/ProcessorUtils.py
index 23f9e69..f23696f 100644
--- a/ProcessorUtils.py
+++ b/ProcessorUtils.py
@@ -106,9 +106,16 @@ def get_only_existing_globs(file_globs,inplace=True):
             globs_out += [fg]
         return globs_out
 
-def subprocess_and_log(cmd,description_short,description_long,check=True,**kwargs):
+def subprocess_and_log(cmd,description_short,description_long,check=True,log_type='error',**kwargs):
     '''Run a shell command (described by a comma separated list) and send stdout
-    and stderr to logfile, and raise any exception.'''
+    and stderr to logfile, and raise any exception.
+    
+    log_type is a string to determine which log message level to use, must be 
+    one of the logging options: info, debug, warn, warning, error, exception or
+    fatal.'''
+
+    # determine message type to write to log
+    log_call = getattr(logger,log_type)
 
     try:
         process = subprocess.run(
@@ -126,7 +133,7 @@ def subprocess_and_log(cmd,description_short,description_long,check=True,**kwarg
         for line in e.stdout.decode('utf-8').split(r'\n'):
             logger.info(f"{description_short} : " + line)
 
-        logger.exception(f"Some failure when running {description_long}", exc_info=True)
+        log_call(f"Some failure when running {description_long}", exc_info=True)
         raise
 
     return process
-- 
GitLab