From e18428b25badc9f7a58b23e7bdf90923d5350b91 Mon Sep 17 00:00:00 2001 From: Ben Boeckel Date: Mon, 12 Oct 2015 13:35:57 -0400 Subject: [PATCH] UIBase: add a syslog ui Rather than having an option for syslog output, make a separate UI option. Signed-off-by: Ben Boeckel Signed-off-by: Nicolas Sebrecht --- contrib/systemd/offlineimap.service | 2 +- contrib/systemd/offlineimap@.service | 2 +- docs/offlineimap.txt | 2 +- docs/offlineimapui.txt | 11 +++++++++++ offlineimap/init.py | 2 +- offlineimap/ui/Noninteractive.py | 20 ++++++++++++++++++++ offlineimap/ui/UIBase.py | 6 +++--- offlineimap/ui/__init__.py | 1 + 8 files changed, 39 insertions(+), 7 deletions(-) diff --git a/contrib/systemd/offlineimap.service b/contrib/systemd/offlineimap.service index a587761..8b77bc4 100644 --- a/contrib/systemd/offlineimap.service +++ b/contrib/systemd/offlineimap.service @@ -3,7 +3,7 @@ Description=Offlineimap Service [Service] Type=oneshot -ExecStart=/usr/bin/offlineimap -o -s -u quiet +ExecStart=/usr/bin/offlineimap -o -u syslog [Install] WantedBy=mail.target diff --git a/contrib/systemd/offlineimap@.service b/contrib/systemd/offlineimap@.service index 5439817..6730652 100644 --- a/contrib/systemd/offlineimap@.service +++ b/contrib/systemd/offlineimap@.service @@ -3,7 +3,7 @@ Description=Offlineimap Service for account %i [Service] Type=oneshot -ExecStart=/usr/bin/offlineimap -o -a %i -s -u quiet +ExecStart=/usr/bin/offlineimap -o -a %i -u syslog [Install] WantedBy=mail.target diff --git a/docs/offlineimap.txt b/docs/offlineimap.txt index cfebe36..a2da7f5 100644 --- a/docs/offlineimap.txt +++ b/docs/offlineimap.txt @@ -149,7 +149,7 @@ option is ignored if maxage is set. + This overrides the default specified in the configuration file. The UI specified with -u will be forced to be used, even if checks determine that it -is not usable. Possible interface choices are: quiet, basic, ttyui, +is not usable. Possible interface choices are: quiet, basic, syslog, ttyui, blinkenlights, machineui. diff --git a/docs/offlineimapui.txt b/docs/offlineimapui.txt index c087ece..2ea661b 100644 --- a/docs/offlineimapui.txt +++ b/docs/offlineimapui.txt @@ -127,6 +127,17 @@ It will output nothing except errors and serious warnings. Like Basic, this user interface is not capable of reading a password from the keyboard; account passwords must be specified using one of the configuration file options. + +Syslog +------ + +Syslog is designed for situations where OfflineIMAP is run as a daemon (e.g., +as a systemd --user service), but errors should be forwarded to the system log. +Like Basic, this user interface is not capable of reading a password from the +keyboard; account passwords must be specified using one of the configuration +file options. + + MachineUI --------- diff --git a/offlineimap/init.py b/offlineimap/init.py index 761ff61..d94dcb7 100644 --- a/offlineimap/init.py +++ b/offlineimap/init.py @@ -115,7 +115,7 @@ class OfflineImap: parser.add_option("-u", dest="interface", help="specifies an alternative user interface" - " (quiet, basic, ttyui, blinkenlights, machineui)") + " (quiet, basic, syslog, ttyui, blinkenlights, machineui)") (options, args) = parser.parse_args() globals.set_options (options) diff --git a/offlineimap/ui/Noninteractive.py b/offlineimap/ui/Noninteractive.py index 4e87d50..11b7247 100644 --- a/offlineimap/ui/Noninteractive.py +++ b/offlineimap/ui/Noninteractive.py @@ -17,6 +17,7 @@ import logging from offlineimap.ui.UIBase import UIBase +import offlineimap class Basic(UIBase): """'Basic' simply sets log level to INFO""" @@ -27,3 +28,22 @@ class Quiet(UIBase): """'Quiet' simply sets log level to WARNING""" def __init__(self, config, loglevel = logging.WARNING): return super(Quiet, self).__init__(config, loglevel) + +class Syslog(UIBase): + """'Syslog' sets log level to INFO and outputs to syslog instead of stdout""" + def __init__(self, config, loglevel = logging.INFO): + return super(Syslog, self).__init__(config, loglevel) + + def setup_consolehandler(self): + # create syslog handler + ch = logging.handlers.SysLogHandler('/dev/log') + # create formatter and add it to the handlers + self.formatter = logging.Formatter("%(message)s") + ch.setFormatter(self.formatter) + # add the handlers to the logger + self.logger.addHandler(ch) + self.logger.info(offlineimap.banner) + return ch + + def setup_sysloghandler(self): + pass diff --git a/offlineimap/ui/UIBase.py b/offlineimap/ui/UIBase.py index f5ba5ba..bc53ae6 100644 --- a/offlineimap/ui/UIBase.py +++ b/offlineimap/ui/UIBase.py @@ -16,6 +16,7 @@ # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA import logging +import logging.handlers import re import time import sys @@ -94,9 +95,8 @@ class UIBase(object): def setup_sysloghandler(self): """Backend specific syslog handler.""" - # create console handler with a higher log level - ch = logging.SysLogHandler(sys.stdout) - #ch.setLevel(logging.DEBUG) + # create syslog handler + ch = logging.handlers.SysLogHandler('/dev/log') # create formatter and add it to the handlers self.formatter = logging.Formatter("%(message)s") ch.setFormatter(self.formatter) diff --git a/offlineimap/ui/__init__.py b/offlineimap/ui/__init__.py index 3da42b9..258b5bb 100644 --- a/offlineimap/ui/__init__.py +++ b/offlineimap/ui/__init__.py @@ -21,6 +21,7 @@ from offlineimap.ui import TTY, Noninteractive, Machine UI_LIST = {'ttyui': TTY.TTYUI, 'basic': Noninteractive.Basic, 'quiet': Noninteractive.Quiet, + 'syslog': Noninteractive.Syslog, 'machineui': Machine.MachineUI} #add Blinkenlights UI if it imports correctly (curses installed)