From d8e394ec27e47c58570f588868409d520e5d4362 Mon Sep 17 00:00:00 2001
From: Alan De Smet <alan.desmet@ssec.wisc.edu>
Date: Fri, 6 Aug 2021 16:14:19 -0500
Subject: [PATCH] Expanded logging support

---
 example/aitf-update-cache     | 18 +++++++++--------
 example/aitf/argparsetools.py | 37 ++++++++++++++++++++++++++++-------
 example/aitf/conlog.py        | 22 +++++++++++++++++++++
 3 files changed, 62 insertions(+), 15 deletions(-)

diff --git a/example/aitf-update-cache b/example/aitf-update-cache
index da0755b..8d130f3 100755
--- a/example/aitf-update-cache
+++ b/example/aitf-update-cache
@@ -7,7 +7,8 @@ import tempfile
 sys.path.append(os.path.abspath('..'))
 from csppfetch import DownloadStatistics
 import aitf.ancil
-from aitf.conlog import add_console_logging
+import aitf.conlog
+import logging
 
 
 # Environment variable that can hold the path to the cache
@@ -75,12 +76,13 @@ def parse_args():
     return args
 
 
-def print_download_report(name, stats):
-    print(name)
-    print("\n".join(["    "+x for x in stats.report()]))
+def log_download_report(name, stats):
+    logging.summary(name)
+    for x in stats.report():
+        logging.summary("    "+x)
 
 def main():
-    #add_console_logging()
+    aitf.conlog.setup_logging()
     args = parse_args()
 
     sststats = DownloadStatistics()
@@ -89,7 +91,7 @@ def main():
             end_time=args.newest,
             download_stats = sststats,
             )
-    print_download_report("SST Download Summary", sststats)
+    log_download_report("SST Download Summary", sststats)
 
     gfsstats = DownloadStatistics()
     aitf.ancil.GFS.update_cache(args.dir,
@@ -97,9 +99,9 @@ def main():
             end_time=args.newest,
             download_stats = gfsstats,
             )
-    print_download_report("GFS Download Summary", gfsstats)
+    log_download_report("GFS Download Summary", gfsstats)
 
-    print_download_report("Total Download Summary", sststats + gfsstats)
+    log_download_report("Total Download Summary", sststats + gfsstats)
 
     return 0
 
diff --git a/example/aitf/argparsetools.py b/example/aitf/argparsetools.py
index eaeeaf9..d186b35 100644
--- a/example/aitf/argparsetools.py
+++ b/example/aitf/argparsetools.py
@@ -11,11 +11,33 @@ CACHE_ENV="CSPP_GEO_AITF_CACHE"
 def argerror(msg):
     raise argparse.ArgumentTypeError(msg)
 
+
+logging_levels = None
+def initialize_logging_level_info():
+    import logging
+    global logging_levels
+    # Need to delay until logging is configured
+    logging_levels = [ ('warnings', logging.WARNING) ]
+    # These are from conlog, but I don't want to depend on them.
+    if hasattr(logging,'SUMMARY'): logging_levels += [ ('a summary', logging.SUMMARY) ]
+    if hasattr(logging,'PROGRESS'): logging_levels += [ ('progress', logging.PROGRESS) ]
+    logging_levels += [('general information', logging.INFO),
+            ('debugging information', logging.DEBUG) ]
+
 def add_verbose_quiet_args(parser):
+    import logging
+    initialize_logging_level_info()
+
+    helplist = []
+    for i, lvldesc in enumerate([x[0] for x in logging_levels[1:]]):
+        helplist.append(" -"+("v"*(i+1))+f" adds {lvldesc},")
+    help = "increase verbosity of output; "+"".join(helplist) + ' (default: only warnings and errors)'
+    
+
+
     g = parser.add_mutually_exclusive_group()
     g.add_argument('--verbose', '-v', dest='verbosity', action='count',
-            help='increase verbosity of output;-v adds a summary,  -vv adds general information, -vvv adds debugging information (default: only warnings and errors)'
-        )
+            help=help)
     g.add_argument('--quiet', '-q', dest='silent', action='store_true',
         help='only print errors'
         )
@@ -23,11 +45,12 @@ def add_verbose_quiet_args(parser):
 
 def process_verbosity(args):
     import logging
-    #logging_levels = [logging.WARNING, logging.SUMMARY, logging.INFO, logging.DEBUG]
-    logging_levels = [logging.WARNING, logging.INFO, logging.DEBUG]
-    if args.verbosity is None: args.verbosity = 0
-    if args.verbosity >= len(logging_levels): args.verbosity = -1
-    args.log_level = logging_levels[args.verbosity]
+    levels = [x[1] for x in logging_levels]
+    if args.verbosity is None:
+        args.verbosity = 0
+    if args.verbosity >= len(logging_levels):
+        args.verbosity = len(logging_levels)-1
+    args.log_level = logging_levels[args.verbosity][1]
     if args.silent:
         args.log_level = logging.ERROR
     logging.getLogger().setLevel(args.log_level)
diff --git a/example/aitf/conlog.py b/example/aitf/conlog.py
index 84741bf..1494904 100644
--- a/example/aitf/conlog.py
+++ b/example/aitf/conlog.py
@@ -3,6 +3,19 @@
 import logging
 import sys
 
+def create_log_level(level, name):
+    logging.addLevelName(level, name.upper())
+    def logger_func(self, message, *args, **kws):
+        if self.isEnabledFor(level):
+            self._log(level, message, args, **kws)
+    def logging_func(message, *args, **kws):
+        logging.log(level, message, *args, **kws)
+    setattr(logging.Logger, name.lower(), logger_func)
+    setattr(logging, name.lower(), logging_func)
+    setattr(logging, name.upper(), level)
+    logging.basicConfig()
+
+
 def add_console_logging():
     """ Log to console, prefixing entries with the log level
 
@@ -14,6 +27,8 @@ def add_console_logging():
             labels = {
                 logging.DEBUG: "debug: ",
                 logging.INFO: "",
+                logging.PROGRESS: "",
+                logging.SUMMARY: "",
                 logging.WARNING: "Warning: ",
                 logging.ERROR: "ERROR: ",
                 logging.CRITICAL: "CRITICAL ERROR: ",
@@ -29,8 +44,15 @@ def add_console_logging():
     console_handler.setFormatter(console_formatter)
 
     root = logging.getLogger()
+    root.handlers.clear()
     root.addHandler(console_handler)
 
     root.setLevel(logging.DEBUG)
 
     return console_handler
+
+def setup_logging():
+    create_log_level(25, "SUMMARY")
+    create_log_level(22, "PROGRESS")
+    logging.basicConfig()
+    add_console_logging()
-- 
GitLab