Logging improvements

Mon, 22 Jan 2018 18:56:43 +0000

author
Tuomo Valkonen <tuomov@iki.fi>
date
Mon, 22 Jan 2018 18:56:43 +0000
changeset 50
2d8947351727
parent 49
db33dfa64ad6
child 51
1614b2184bb4

Logging improvements

backup.py file | annotate | diff | comparison | revisions
borgend.py file | annotate | diff | comparison | revisions
ui.py file | annotate | diff | comparison | revisions
utils.py file | annotate | diff | comparison | revisions
--- a/backup.py	Mon Jan 22 18:16:51 2018 +0000
+++ b/backup.py	Mon Jan 22 18:56:43 2018 +0000
@@ -106,7 +106,7 @@
                 try:
                     pw=keyring.get_password("borg-backup", acc)
                 except Exception as err:
-                    logger.error('Failed to retrieve password: %s' % str(err))
+                    logger.error('Failed to retrieve passphrase')
                     raise err
                 else:
                     logger.debug('Received passphrase')
--- a/borgend.py	Mon Jan 22 18:16:51 2018 +0000
+++ b/borgend.py	Mon Jan 22 18:56:43 2018 +0000
@@ -6,7 +6,6 @@
 import logging.handlers
 import argparse
 import platform
-import utils
 
 #
 # Branding
@@ -30,6 +29,7 @@
 logger=logging.getLogger(appname)
 logger.setLevel(loglevel)
 stderrlog=logging.StreamHandler()
+stderrlog.setLevel(logging.WARNING)
 logger.addHandler(stderrlog)
 logger.propagate=True
 
@@ -73,6 +73,7 @@
     # Parse args. Let argparse handle errors/exit if there are any
     args= do_args()
     tray = None
+    backups=[]
 
     try:
         args=do_args()
@@ -91,39 +92,41 @@
         from queue import Queue
         import signal, os
 
+        scheduler = Scheduler()
+        scheduler.start()
+
         backupconfigs=config.settings['backups']
-        backups=[None]*len(backupconfigs);
 
-        scheduler = Scheduler()
-
-        try:
-            scheduler.start()
+        for i in range(len(backupconfigs)):
+            logger.info('Setting up backup set %d' % i)
+            b=Backup(i, backupconfigs[i], scheduler)
+            backups.append(b)
 
-            for i in range(len(backupconfigs)):
-                logger.info('Setting up backup set %d' % i)
-                backups[i]=Backup(i, backupconfigs[i], scheduler)
-                backups[i].start()
+        for b in backups:
+            b.start()
 
-            if args.notray or platform.system()!='Darwin':
-                pass
-            else:
-                from ui import BorgendTray
-                tray=BorgendTray(backups);
-                tray.run()
-        finally:
-            for i in range(len(backups)):
-                if backups[i]:
-                    backups[i].abort()
-            backups=[]
+        if args.notray or platform.system()!='Darwin':
+            # Wait for scheduler to finish
+            scheduler.join()
+        else:
+            # Start UI, and let it handle exit control
+            from ui import BorgendTray
+            tray=BorgendTray(backups);
+            tray.run()
+
     except Exception as err:
         # TODO: Should write errors here to stderr;
         # perhaps add an extra stderr logger for error level messages
-        utils.log_exception(logger, err, detail='Exiting')
+        logger.exception("Exception fell through to outer level: exiting")
+
+    finally:
+        for b in backups:
+            b.terminate()
+
         if tray:
             tray.quit()
         else:
             logging.shutdown()
-            #sys.exit()
 
     #
     # This shit is fucked, disables ^C etc., and threading doesn't seem to help
--- a/ui.py	Mon Jan 22 18:16:51 2018 +0000
+++ b/ui.py	Mon Jan 22 18:56:43 2018 +0000
@@ -7,7 +7,6 @@
 import datetime
 import logging
 import borgend
-import utils
 import backup
 from threading import Lock, Timer
 from config import settings
@@ -45,8 +44,7 @@
 
         rumps.notification(title, subtitle, message, data=d)
     except Exception as err:
-        utils.log_exception(logger, err,
-                            detail='Unable to display notification')
+        logger.exception("Failed to display notification")
 
 # Based on code snatched from
 # https://stackoverflow.com/questions/12523586/python-format-size-application-converting-b-to-kb-mb-gb-tb/37423778
@@ -71,6 +69,7 @@
 
 def make_title(status):
     state=status['state']
+    detail=''
     if status['type']=='scheduled':
         # Operation scheduled
         when=status['when']
@@ -178,7 +177,7 @@
             self.title=traynames[active]
 
     def __status_callback(self, obj, index, status, errorlog):
-        logger.debug('Status callbackup %s' % str(status))
+        logger.debug('Status callback: %s' % str(status))
 
         with self.lock:
             self.statuses[index]=status
@@ -208,7 +207,7 @@
         try:
             b.create()
         except Exception as err:
-            utils.log_exception(logger, err)
+            logger.exception("Failure to initialise backup")
             notification_workaround(borgend.appname_stylised,
                                     err.__class__.__name__, str(err))
 
@@ -238,7 +237,7 @@
                 with logwindow_lock:
                     logwindow[0]=None
     except Exception as err:
-        utils.log_exception(logger, err)
+        logger.exception("Failed to display log")
 
 #
 # Notification click response => show log window
@@ -247,3 +246,4 @@
 @rumps.notifications
 def notification_center(_):
     showlog()
+
--- a/utils.py	Mon Jan 22 18:16:51 2018 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,10 +0,0 @@
-#
-# Utility functions for Borgend
-#
-
-def log_exception(logger, err, detail=None):
-    err_type=err.__class__.__name__
-    if detail:
-        logger.critical('%s:%s "%s"', str(detail), err_type, str(err))
-    else:
-        logger.critical('%s "%s"', err_type, str(err))

mercurial