From 584a6d93ad1fe1ba46929108f002a16a8b70e95d Mon Sep 17 00:00:00 2001 From: Ivan Alejandro Date: Fri, 5 Jun 2015 13:36:51 -0300 Subject: [feat] add a zmq based logger, change log window - Add a new handler for a zmq/thread-safe log send between components. - Update the log window to use this new handler. - Remove old custom handler We have implemented a new handler that uses logbook, so this one is no longer needed. - Adapt log silencer to new handler - Use log file always as default - Related: #6733 --- src/leap/bitmask/app.py | 11 +- src/leap/bitmask/config/flags.py | 4 + src/leap/bitmask/gui/loggerwindow.py | 263 --------------- src/leap/bitmask/gui/logwindow.py | 360 +++++++++++++++++++++ src/leap/bitmask/logs/__init__.py | 6 +- src/leap/bitmask/logs/leap_log_handler.py | 137 -------- src/leap/bitmask/logs/log_silencer.py | 10 +- src/leap/bitmask/logs/safezmqhandler.py | 118 +++++++ .../bitmask/logs/tests/test_leap_log_handler.py | 120 ------- src/leap/bitmask/logs/utils.py | 87 ++--- src/leap/bitmask/util/leap_argparse.py | 3 - 11 files changed, 530 insertions(+), 589 deletions(-) delete mode 100644 src/leap/bitmask/gui/loggerwindow.py create mode 100644 src/leap/bitmask/gui/logwindow.py delete mode 100644 src/leap/bitmask/logs/leap_log_handler.py create mode 100644 src/leap/bitmask/logs/safezmqhandler.py delete mode 100644 src/leap/bitmask/logs/tests/test_leap_log_handler.py (limited to 'src') diff --git a/src/leap/bitmask/app.py b/src/leap/bitmask/app.py index 731c168c..adab8652 100644 --- a/src/leap/bitmask/app.py +++ b/src/leap/bitmask/app.py @@ -124,7 +124,6 @@ def start_app(): options = { 'start_hidden': opts.start_hidden, 'debug': opts.debug, - 'log_file': opts.log_file, } flags.STANDALONE = opts.standalone @@ -137,7 +136,13 @@ def start_app(): flags.CA_CERT_FILE = opts.ca_cert_file - replace_stdout = True + flags.DEBUG = opts.debug + + logger = get_logger() + + # NOTE: since we are not using this right now, the code that replaces the + # stdout needs to be reviewed when we enable this again + # replace_stdout = True # XXX mail repair commands disabled for now # if opts.repair or opts.import_maildir: @@ -145,8 +150,6 @@ def start_app(): # this could be more generic with a Command class. # replace_stdout = False - logger = create_logger(opts.debug, opts.log_file, replace_stdout) - # ok, we got logging in place, we can satisfy mail plumbing requests # and show logs there. it normally will exit there if we got that path. # XXX mail repair commands disabled for now diff --git a/src/leap/bitmask/config/flags.py b/src/leap/bitmask/config/flags.py index cdde1971..1cf1d15a 100644 --- a/src/leap/bitmask/config/flags.py +++ b/src/leap/bitmask/config/flags.py @@ -58,3 +58,7 @@ SKIP_WIZARD_CHECKS = False # This flag tells us whether the current pyzmq supports using CurveZMQ or not. ZMQ_HAS_CURVE = None + +# Store the needed loglevel globally since the logger handlers goes through +# threads and processes +DEBUG = False diff --git a/src/leap/bitmask/gui/loggerwindow.py b/src/leap/bitmask/gui/loggerwindow.py deleted file mode 100644 index 463d2412..00000000 --- a/src/leap/bitmask/gui/loggerwindow.py +++ /dev/null @@ -1,263 +0,0 @@ -# -*- coding: utf-8 -*- -# loggerwindow.py -# Copyright (C) 2013 LEAP -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . - -""" -History log window -""" -import cgi -import logging - -from PySide import QtCore, QtGui - -from ui_loggerwindow import Ui_LoggerWindow - -from leap.bitmask.util.constants import PASTEBIN_API_DEV_KEY -from leap.bitmask.logs.leap_log_handler import LeapLogHandler -from leap.bitmask.util import pastebin -from leap.common.check import leap_assert, leap_assert_type - -logger = logging.getLogger(__name__) - - -class LoggerWindow(QtGui.QDialog): - """ - Window that displays a history of the logged messages in the app. - """ - _paste_ok = QtCore.Signal(object) - _paste_error = QtCore.Signal(object) - - def __init__(self, parent, handler): - """ - Initialize the widget with the custom handler. - - :param handler: Custom handler that supports history and signal. - :type handler: LeapLogHandler. - """ - QtGui.QDialog.__init__(self, parent) - leap_assert(handler, "We need a handler for the logger window") - leap_assert_type(handler, LeapLogHandler) - - # Load UI - self.ui = Ui_LoggerWindow() - self.ui.setupUi(self) - - # Make connections - self.ui.btnSave.clicked.connect(self._save_log_to_file) - self.ui.btnDebug.toggled.connect(self._load_history), - self.ui.btnInfo.toggled.connect(self._load_history), - self.ui.btnWarning.toggled.connect(self._load_history), - self.ui.btnError.toggled.connect(self._load_history), - self.ui.btnCritical.toggled.connect(self._load_history) - self.ui.leFilterBy.textEdited.connect(self._filter_by) - self.ui.cbCaseInsensitive.stateChanged.connect(self._load_history) - self.ui.btnPastebin.clicked.connect(self._pastebin_this) - - self._paste_ok.connect(self._pastebin_ok) - self._paste_error.connect(self._pastebin_err) - - self._current_filter = "" - self._current_history = "" - - # Load logging history and connect logger with the widget - self._logging_handler = handler - self._connect_to_handler() - self._load_history() - - def _connect_to_handler(self): - """ - This method connects the loggerwindow with the handler through a - signal communicate the logger events. - """ - self._logging_handler.new_log.connect(self._add_log_line) - - def _add_log_line(self, log): - """ - Adds a line to the history, only if it's in the desired levels to show. - - :param log: a log record to be inserted in the widget - :type log: a dict with RECORD_KEY and MESSAGE_KEY. - the record contains the LogRecord of the logging module, - the message contains the formatted message for the log. - """ - html_style = { - logging.DEBUG: "background: #CDFFFF;", - logging.INFO: "background: white;", - logging.WARNING: "background: #FFFF66;", - logging.ERROR: "background: red; color: white;", - logging.CRITICAL: "background: red; color: white; font: bold;" - } - level = log[LeapLogHandler.RECORD_KEY].levelno - message = cgi.escape(log[LeapLogHandler.MESSAGE_KEY]) - - if self._logs_to_display[level]: - open_tag = "" - open_tag += "" - close_tag = "" - message = open_tag + message + close_tag - - filter_by = self._current_filter - msg = message - if self.ui.cbCaseInsensitive.isChecked(): - msg = msg.upper() - filter_by = filter_by.upper() - - if msg.find(filter_by) != -1: - self.ui.txtLogHistory.append(message) - - def _load_history(self): - """ - Load the previous logged messages in the widget. - They are stored in the custom handler. - """ - self._set_logs_to_display() - self.ui.txtLogHistory.clear() - history = self._logging_handler.log_history - current_history = [] - for line in history: - self._add_log_line(line) - message = line[LeapLogHandler.MESSAGE_KEY] - current_history.append(message) - - self._current_history = "\n".join(current_history) - - def _set_logs_to_display(self): - """ - Sets the logs_to_display dict getting the toggled options from the ui - """ - self._logs_to_display = { - logging.DEBUG: self.ui.btnDebug.isChecked(), - logging.INFO: self.ui.btnInfo.isChecked(), - logging.WARNING: self.ui.btnWarning.isChecked(), - logging.ERROR: self.ui.btnError.isChecked(), - logging.CRITICAL: self.ui.btnCritical.isChecked() - } - - def _filter_by(self, text): - """ - Sets the text to use for filtering logs in the log window. - - :param text: the text to compare with the logs when filtering. - :type text: str - """ - self._current_filter = text - self._load_history() - - def _save_log_to_file(self): - """ - Lets the user save the current log to a file - """ - fileName, filtr = QtGui.QFileDialog.getSaveFileName( - self, self.tr("Save As"), - options=QtGui.QFileDialog.DontUseNativeDialog) - - if fileName: - try: - with open(fileName, 'w') as output: - history = self.ui.txtLogHistory.toPlainText() - # Chop some \n. - # html->plain adds several \n because the html is made - # using table cells. - history = history.replace('\n\n\n', '\n') - - output.write(history) - logger.debug('Log saved in %s' % (fileName, )) - except IOError, e: - logger.error("Error saving log file: %r" % (e, )) - else: - logger.debug('Log not saved!') - - def _set_pastebin_sending(self, sending): - """ - Define the status of the pastebin button. - Change the text and enable/disable according to the current action. - - :param sending: if we are sending to pastebin or not. - :type sending: bool - """ - if sending: - self.ui.btnPastebin.setText(self.tr("Sending to pastebin...")) - self.ui.btnPastebin.setEnabled(False) - else: - self.ui.btnPastebin.setText(self.tr("Send to Pastebin.com")) - self.ui.btnPastebin.setEnabled(True) - - def _pastebin_ok(self, link): - """ - Handle a successful paste. - - :param link: the recently created pastebin link. - :type link: str - """ - self._set_pastebin_sending(False) - msg = self.tr("Your pastebin link {0}") - msg = msg.format(link) - - # We save the dialog in an instance member to avoid dialog being - # deleted right after we exit this method - self._msgBox = msgBox = QtGui.QMessageBox( - QtGui.QMessageBox.Information, self.tr("Pastebin OK"), msg) - msgBox.setWindowModality(QtCore.Qt.NonModal) - msgBox.show() - - def _pastebin_err(self, failure): - """ - Handle a failure in paste. - - :param failure: the exception that made the paste fail. - :type failure: Exception - """ - self._set_pastebin_sending(False) - logger.error(repr(failure)) - - msg = self.tr("Sending logs to Pastebin failed!") - if isinstance(failure, pastebin.PostLimitError): - msg = self.tr('Maximum amount of submissions reached for today.') - - # We save the dialog in an instance member to avoid dialog being - # deleted right after we exit this method - self._msgBox = msgBox = QtGui.QMessageBox( - QtGui.QMessageBox.Critical, self.tr("Pastebin Error"), msg) - msgBox.setWindowModality(QtCore.Qt.NonModal) - msgBox.show() - - def _pastebin_this(self): - """ - Send the current log history to pastebin.com and gives the user a link - to see it. - """ - def do_pastebin(): - """ - Send content to pastebin and return the link. - """ - content = self._current_history - pb = pastebin.PastebinAPI() - try: - link = pb.paste(PASTEBIN_API_DEV_KEY, content, - paste_name="Bitmask log", - paste_expire_date='1M') - # convert to 'raw' link - link = "http://pastebin.com/raw.php?i=" + link.split('/')[-1] - - self._paste_ok.emit(link) - except Exception as e: - self._paste_error.emit(e) - - self._set_pastebin_sending(True) - - self._paste_thread = QtCore.QThread() - self._paste_thread.run = lambda: do_pastebin() - self._paste_thread.start() diff --git a/src/leap/bitmask/gui/logwindow.py b/src/leap/bitmask/gui/logwindow.py new file mode 100644 index 00000000..123f14cc --- /dev/null +++ b/src/leap/bitmask/gui/logwindow.py @@ -0,0 +1,360 @@ +# -*- coding: utf-8 -*- +# logwindow.py +# Copyright (C) 2013 LEAP +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +""" +History log window +""" +import cgi + +from PySide import QtCore, QtGui + +import logbook +from logbook.queues import ZeroMQSubscriber + +from ui_loggerwindow import Ui_LoggerWindow + +from leap.bitmask.logs import LOG_FORMAT +from leap.bitmask.logs.utils import get_logger +from leap.bitmask.util.constants import PASTEBIN_API_DEV_KEY +from leap.bitmask.util import pastebin + +logger = get_logger() + +# log history global variable used to store received logs through different +# opened instances of this window +_LOGS_HISTORY = [] + + +class QtLogHandler(logbook.Handler, logbook.StringFormatterHandlerMixin): + """ + Custom log handler which emits a log record with the message properly + formatted using a Qt Signal. + """ + + class _QtSignaler(QtCore.QObject): + """ + inline class used to hold the `new_log` Signal, if this is used + directly in the outside class it fails due how PySide works. + + This is the message we get if not use this method: + TypeError: Error when calling the metaclass bases + metaclass conflict: the metaclass of a derived class must be a + (non-strict) subclass of the metaclasses of all its bases + + """ + new_log = QtCore.Signal(object) + + def emit(self, data): + """ + emit the `new_log` Signal with the given `data` parameter. + + :param data: the data to emit along with the signal. + :type data: object + """ + # WARNING: the new-style connection does NOT work because PySide + # translates the emit method to self.emit, and that collides with + # the emit method for logging.Handler + # self.new_log.emit(log_item) + QtCore.QObject.emit(self, QtCore.SIGNAL('new_log(PyObject)'), data) + + def __init__(self, level=logbook.NOTSET, format_string=None, + encoding=None, filter=None, bubble=False): + + logbook.Handler.__init__(self, level, filter, bubble) + logbook.StringFormatterHandlerMixin.__init__(self, format_string) + + self.qt = self._QtSignaler() + + def __enter__(self): + return logbook.Handler.__enter__(self) + + def __exit__(self, exc_type, exc_value, tb): + return logbook.Handler.__exit__(self, exc_type, exc_value, tb) + + def emit(self, record): + """ + Emit the specified logging record using a Qt Signal. + Also add it to the history in order to be able to access it later. + + :param record: the record to emit + :type record: logbook.LogRecord + """ + global _LOGS_HISTORY + record.msg = self.format(record) + # NOTE: not optimal approach, we may want to look at + # bisect.insort with a custom approach to use key or + # http://code.activestate.com/recipes/577197-sortedcollection/ + # Sort logs on arrival, logs transmitted over zmq may arrive unsorted. + _LOGS_HISTORY.append(record) + _LOGS_HISTORY = sorted(_LOGS_HISTORY, key=lambda r: r.time) + + # XXX: emitting the record on arrival does not allow us to sort here so + # in the GUI the logs may arrive with with some time sort problem. + # We should implement a sort-on-arrive for the log window. + # Maybe we should switch to a tablewidget item that sort automatically + # by timestamp. + # As a user workaround you can close/open the log window + self.qt.emit(record) + + +class LoggerWindow(QtGui.QDialog): + """ + Window that displays a history of the logged messages in the app. + """ + _paste_ok = QtCore.Signal(object) + _paste_error = QtCore.Signal(object) + + def __init__(self, parent): + """ + Initialize the widget. + """ + QtGui.QDialog.__init__(self, parent) + + # Load UI + self.ui = Ui_LoggerWindow() + self.ui.setupUi(self) + + # Make connections + self.ui.btnSave.clicked.connect(self._save_log_to_file) + self.ui.btnDebug.toggled.connect(self._load_history), + self.ui.btnInfo.toggled.connect(self._load_history), + self.ui.btnWarning.toggled.connect(self._load_history), + self.ui.btnError.toggled.connect(self._load_history), + self.ui.btnCritical.toggled.connect(self._load_history) + self.ui.leFilterBy.textEdited.connect(self._filter_by) + self.ui.cbCaseInsensitive.stateChanged.connect(self._load_history) + self.ui.btnPastebin.clicked.connect(self._pastebin_this) + + self._paste_ok.connect(self._pastebin_ok) + self._paste_error.connect(self._pastebin_err) + + self._current_filter = "" + self._current_history = "" + + self._set_logs_to_display() + + self._my_handler = QtLogHandler(format_string=LOG_FORMAT) + self._my_handler.qt.new_log.connect(self._add_log_line) + + self._load_history() + self._connect_to_logbook() + + def _connect_to_logbook(self): + """ + Run in the background the log receiver. + """ + subscriber = ZeroMQSubscriber('tcp://127.0.0.1:5000', multi=True) + self._logbook_controller = subscriber.dispatch_in_background( + self._my_handler) + + def _add_log_line(self, log): + """ + Adds a line to the history, only if it's in the desired levels to show. + + :param log: a log record to be inserted in the widget + :type log: Logbook.LogRecord. + """ + html_style = { + logbook.DEBUG: "background: #CDFFFF;", + logbook.INFO: "background: white;", + logbook.WARNING: "background: #FFFF66;", + logbook.ERROR: "background: red; color: white;", + logbook.CRITICAL: "background: red; color: white; font: bold;" + } + level = log.level + message = cgi.escape(log.msg) + + if self._logs_to_display[level]: + open_tag = "" + open_tag += "" + close_tag = "" + message = open_tag + message + close_tag + + filter_by = self._current_filter + msg = message + if self.ui.cbCaseInsensitive.isChecked(): + msg = msg.upper() + filter_by = filter_by.upper() + + if msg.find(filter_by) != -1: + self.ui.txtLogHistory.append(message) + + def _load_history(self): + """ + Load the previous logged messages in the widget. + They are stored in the custom handler. + """ + self._set_logs_to_display() + self.ui.txtLogHistory.clear() + current_history = [] + for record in _LOGS_HISTORY: + self._add_log_line(record) + current_history.append(record.msg) + + self._current_history = "\n".join(current_history) + + def _set_logs_to_display(self): + """ + Sets the logs_to_display dict getting the toggled options from the ui + """ + self._logs_to_display = { + logbook.DEBUG: self.ui.btnDebug.isChecked(), + logbook.INFO: self.ui.btnInfo.isChecked(), + logbook.WARNING: self.ui.btnWarning.isChecked(), + logbook.ERROR: self.ui.btnError.isChecked(), + logbook.CRITICAL: self.ui.btnCritical.isChecked() + } + + def _filter_by(self, text): + """ + Sets the text to use for filtering logs in the log window. + + :param text: the text to compare with the logs when filtering. + :type text: str + """ + self._current_filter = text + self._load_history() + + def _save_log_to_file(self): + """ + Lets the user save the current log to a file + """ + fileName, filtr = QtGui.QFileDialog.getSaveFileName( + self, self.tr("Save As"), + options=QtGui.QFileDialog.DontUseNativeDialog) + + if fileName: + try: + with open(fileName, 'w') as output: + history = self.ui.txtLogHistory.toPlainText() + # Chop some \n. + # html->plain adds several \n because the html is made + # using table cells. + history = history.replace('\n\n\n', '\n') + + output.write(history) + logger.debug('Log saved in %s' % (fileName, )) + except IOError, e: + logger.error("Error saving log file: %r" % (e, )) + else: + logger.debug('Log not saved!') + + def _set_pastebin_sending(self, sending): + """ + Define the status of the pastebin button. + Change the text and enable/disable according to the current action. + + :param sending: if we are sending to pastebin or not. + :type sending: bool + """ + if sending: + self.ui.btnPastebin.setText(self.tr("Sending to pastebin...")) + self.ui.btnPastebin.setEnabled(False) + else: + self.ui.btnPastebin.setText(self.tr("Send to Pastebin.com")) + self.ui.btnPastebin.setEnabled(True) + + def _pastebin_ok(self, link): + """ + Handle a successful paste. + + :param link: the recently created pastebin link. + :type link: str + """ + self._set_pastebin_sending(False) + msg = self.tr("Your pastebin link {0}") + msg = msg.format(link) + + # We save the dialog in an instance member to avoid dialog being + # deleted right after we exit this method + self._msgBox = msgBox = QtGui.QMessageBox( + QtGui.QMessageBox.Information, self.tr("Pastebin OK"), msg) + msgBox.setWindowModality(QtCore.Qt.NonModal) + msgBox.show() + + def _pastebin_err(self, failure): + """ + Handle a failure in paste. + + :param failure: the exception that made the paste fail. + :type failure: Exception + """ + self._set_pastebin_sending(False) + logger.error(repr(failure)) + + msg = self.tr("Sending logs to Pastebin failed!") + if isinstance(failure, pastebin.PostLimitError): + msg = self.tr('Maximum amount of submissions reached for today.') + + # We save the dialog in an instance member to avoid dialog being + # deleted right after we exit this method + self._msgBox = msgBox = QtGui.QMessageBox( + QtGui.QMessageBox.Critical, self.tr("Pastebin Error"), msg) + msgBox.setWindowModality(QtCore.Qt.NonModal) + msgBox.show() + + def _pastebin_this(self): + """ + Send the current log history to pastebin.com and gives the user a link + to see it. + """ + def do_pastebin(): + """ + Send content to pastebin and return the link. + """ + content = self._current_history + pb = pastebin.PastebinAPI() + try: + link = pb.paste(PASTEBIN_API_DEV_KEY, content, + paste_name="Bitmask log", + paste_expire_date='1M') + # convert to 'raw' link + link = "http://pastebin.com/raw.php?i=" + link.split('/')[-1] + + self._paste_ok.emit(link) + except Exception as e: + self._paste_error.emit(e) + + self._set_pastebin_sending(True) + + self._paste_thread = QtCore.QThread() + self._paste_thread.run = lambda: do_pastebin() + self._paste_thread.start() + + def closeEvent(self, e): + """ + Disconnect logger on close. + """ + self._disconnect_logger() + e.accept() + + def reject(self): + """ + Disconnect logger on reject. + """ + self._disconnect_logger() + QtGui.QDialog.reject(self) + + def _disconnect_logger(self): + """ + Stop the background thread that receives messages through zmq, also + close the subscriber socket. + This allows us to re-create the subscriber when we reopen this window + without getting an error at trying to connect twice to the zmq port. + """ + self._logbook_controller.stop() + self._logbook_controller.subscriber.close() diff --git a/src/leap/bitmask/logs/__init__.py b/src/leap/bitmask/logs/__init__.py index 0516b304..837a5ed9 100644 --- a/src/leap/bitmask/logs/__init__.py +++ b/src/leap/bitmask/logs/__init__.py @@ -1,3 +1,3 @@ -# levelname length == 8, since 'CRITICAL' is the longest -LOG_FORMAT = ('%(asctime)s - %(levelname)-8s - ' - 'L#%(lineno)-4s : %(name)s:%(funcName)s() - %(message)s') +LOG_FORMAT = (u'[{record.time:%Y-%m-%d %H:%M:%S}] ' + u'{record.level_name: <8} - L#{record.lineno: <4} : ' + u'{record.module}:{record.func_name} - {record.message}') diff --git a/src/leap/bitmask/logs/leap_log_handler.py b/src/leap/bitmask/logs/leap_log_handler.py deleted file mode 100644 index 24141638..00000000 --- a/src/leap/bitmask/logs/leap_log_handler.py +++ /dev/null @@ -1,137 +0,0 @@ -# -*- coding: utf-8 -*- -# leap_log_handler.py -# Copyright (C) 2013 LEAP -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . -""" -Custom handler for the logger window. -""" -import logging - -from PySide import QtCore - -from leap.bitmask.logs import LOG_FORMAT - - -class LogHandler(logging.Handler): - """ - This is the custom handler that implements our desired formatting - and also keeps a history of all the logged events. - """ - - MESSAGE_KEY = 'message' - RECORD_KEY = 'record' - - def __init__(self, qtsignal): - """ - LogHander initialization. - Calls parent method and keeps a reference to the qtsignal - that will be used to fire the gui update. - """ - # TODO This is going to eat lots of memory after some time. - # Should be pruned at some moment. - self._log_history = [] - - logging.Handler.__init__(self) - self._qtsignal = qtsignal - - def _get_format(self, logging_level): - """ - Sets the log format depending on the parameter. - It uses html and css to set the colors for the logs. - - :param logging_level: the debug level to define the color. - :type logging_level: str. - """ - formatter = logging.Formatter(LOG_FORMAT) - return formatter - - def emit(self, logRecord): - """ - This method is fired every time that a record is logged by the - logging module. - This method reimplements logging.Handler.emit that is fired - in every logged message. - - :param logRecord: the record emitted by the logging module. - :type logRecord: logging.LogRecord. - """ - self.setFormatter(self._get_format(logRecord.levelname)) - log = self.format(logRecord) - log_item = {self.RECORD_KEY: logRecord, self.MESSAGE_KEY: log} - self._log_history.append(log_item) - self._qtsignal(log_item) - - -class HandlerAdapter(object): - """ - New style class that accesses all attributes from the LogHandler. - - Used as a workaround for a problem with multiple inheritance with Pyside - that surfaced under OSX with pyside 1.1.0. - """ - MESSAGE_KEY = 'message' - RECORD_KEY = 'record' - - def __init__(self, qtsignal): - self._handler = LogHandler(qtsignal=qtsignal) - - def setLevel(self, *args, **kwargs): - return self._handler.setLevel(*args, **kwargs) - - def addFilter(self, *args, **kwargs): - return self._handler.addFilter(*args, **kwargs) - - def handle(self, *args, **kwargs): - return self._handler.handle(*args, **kwargs) - - @property - def level(self): - return self._handler.level - - -class LeapLogHandler(QtCore.QObject, HandlerAdapter): - """ - Custom logging handler. It emits Qt signals so it can be plugged to a gui. - - Its inner handler also stores an history of logs that can be fetched after - having been connected to a gui. - """ - # All dicts returned are of the form - # {'record': LogRecord, 'message': str} - new_log = QtCore.Signal(dict) - - def __init__(self): - """ - LeapLogHandler initialization. - Initializes parent classes. - """ - QtCore.QObject.__init__(self) - HandlerAdapter.__init__(self, qtsignal=self.qtsignal) - - def qtsignal(self, log_item): - # WARNING: the new-style connection does NOT work because PySide - # translates the emit method to self.emit, and that collides with - # the emit method for logging.Handler - # self.new_log.emit(log_item) - QtCore.QObject.emit( - self, - QtCore.SIGNAL('new_log(PyObject)'), log_item) - - @property - def log_history(self): - """ - Returns the history of the logged messages. - """ - return self._handler._log_history diff --git a/src/leap/bitmask/logs/log_silencer.py b/src/leap/bitmask/logs/log_silencer.py index 56b290e4..540532cb 100644 --- a/src/leap/bitmask/logs/log_silencer.py +++ b/src/leap/bitmask/logs/log_silencer.py @@ -17,14 +17,13 @@ """ Filter for leap logs. """ -import logging import os import re from leap.bitmask.util import get_path_prefix -class SelectiveSilencerFilter(logging.Filter): +class SelectiveSilencerFilter(object): """ Configurable filter for root leap logger. @@ -75,7 +74,7 @@ class SelectiveSilencerFilter(logging.Filter): return map(lambda line: re.sub('\s', '', line), lines) - def filter(self, record): + def filter(self, record, handler): """ Implements the filter functionality for this Filter @@ -86,7 +85,10 @@ class SelectiveSilencerFilter(logging.Filter): """ if not self.rules: return True - logger_path = record.name + logger_path = record.module + if logger_path is None: + return True + for path in self.rules: if logger_path.startswith(path): return False diff --git a/src/leap/bitmask/logs/safezmqhandler.py b/src/leap/bitmask/logs/safezmqhandler.py new file mode 100644 index 00000000..7aac6a6a --- /dev/null +++ b/src/leap/bitmask/logs/safezmqhandler.py @@ -0,0 +1,118 @@ +# -*- coding: utf-8 -*- +# safezmqhandler.py +# Copyright (C) 2013, 2014, 2015 LEAP +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +""" +A thread-safe zmq handler for LogBook. +""" +import json +import threading + +from logbook.queues import ZeroMQHandler +from logbook import NOTSET + +import zmq + + +class SafeZMQHandler(ZeroMQHandler): + """ + A ZMQ log handler for LogBook that is thread-safe. + + This log handler makes use of the existing zmq handler and if the user + tries to log something from a different thread than the one used to + create the handler a new socket is created for that thread. + + Note: In ZMQ, Contexts are threadsafe objects, but Sockets are not. + """ + + def __init__(self, uri=None, level=NOTSET, filter=None, bubble=False, + context=None, multi=False): + + ZeroMQHandler.__init__(self, uri, level, filter, bubble, context, + multi) + + current_id = self._get_caller_id() + # we store the socket created on the parent + self._sockets = {current_id: self.socket} + + # store the settings for new socket creation + self._multi = multi + self._uri = uri + + def _get_caller_id(self): + """ + Return an id for the caller that depends on the current thread. + Thanks to this we can detect if we are running in a thread different + than the one who created the socket and create a new one for it. + + :rtype: int + """ + # NOTE it makes no sense to use multiprocessing id since the sockets + # list can't/shouldn't be shared between processes. We only use + # thread id. The user needs to make sure that the handler is created + # inside each process. + return threading.current_thread().ident + + def _get_new_socket(self): + """ + Return a new socket using the `uri` and `multi` parameters given in the + constructor. + + :rtype: zmq.Socket + """ + socket = None + + if self._multi: + socket = self.context.socket(zmq.PUSH) + if self._uri is not None: + socket.connect(self._uri) + else: + socket = self.context.socket(zmq.PUB) + if self._uri is not None: + socket.bind(self._uri) + + return socket + + def emit(self, record): + """ + Emit the given `record` through the socket. + + :param record: the record to emit + :type record: Logbook.LogRecord + """ + current_id = self._get_caller_id() + socket = None + + if current_id in self._sockets: + socket = self._sockets[current_id] + else: + # TODO: create new socket + socket = self._get_new_socket() + self._sockets[current_id] = socket + + socket.send(json.dumps(self.export_record(record)).encode("utf-8")) + + def close(self, linger=-1): + """ + Close all the sockets and linger `linger` time. + + This reimplements the ZeroMQHandler.close method that is used by + context methods. + + :param linger: time to linger, -1 to not to. + :type linger: int + """ + for socket in self._sockets.values(): + socket.close(linger) diff --git a/src/leap/bitmask/logs/tests/test_leap_log_handler.py b/src/leap/bitmask/logs/tests/test_leap_log_handler.py deleted file mode 100644 index 20b09aef..00000000 --- a/src/leap/bitmask/logs/tests/test_leap_log_handler.py +++ /dev/null @@ -1,120 +0,0 @@ -# -*- coding: utf-8 -*- -# test_leap_log_handler.py -# Copyright (C) 2013 LEAP -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . -""" -tests for leap_log_handler -""" -try: - import unittest2 as unittest -except ImportError: - import unittest - -import logging - -from leap.bitmask.logs.leap_log_handler import LeapLogHandler -from leap.bitmask.util.pyside_tests_helper import BasicPySlotCase -from leap.common.testing.basetest import BaseLeapTest - -from mock import Mock - - -class LeapLogHandlerTest(BaseLeapTest, BasicPySlotCase): - """ - LeapLogHandlerTest's tests. - """ - def _callback(self, *args): - """ - Simple callback to track if a signal was emitted. - """ - self.called = True - self.emitted_msg = args[0][LeapLogHandler.MESSAGE_KEY] - - def setUp(self): - BasicPySlotCase.setUp(self) - - # Create the logger - level = logging.DEBUG - self.logger = logging.getLogger(name='test') - self.logger.setLevel(level) - - # Create the handler - self.leap_handler = LeapLogHandler() - self.leap_handler.setLevel(level) - self.logger.addHandler(self.leap_handler) - - def tearDown(self): - BasicPySlotCase.tearDown(self) - try: - self.leap_handler.new_log.disconnect() - except Exception: - pass - - def test_history_starts_empty(self): - self.assertEqual(self.leap_handler.log_history, []) - - def test_one_log_captured(self): - self.logger.debug('test') - self.assertEqual(len(self.leap_handler.log_history), 1) - - def test_history_records_order(self): - self.logger.debug('test 01') - self.logger.debug('test 02') - self.logger.debug('test 03') - - logs = [] - for message in self.leap_handler.log_history: - logs.append(message[LeapLogHandler.RECORD_KEY].msg) - - self.assertIn('test 01', logs) - self.assertIn('test 02', logs) - self.assertIn('test 03', logs) - - def test_history_messages_order(self): - self.logger.debug('test 01') - self.logger.debug('test 02') - self.logger.debug('test 03') - - logs = [] - for message in self.leap_handler.log_history: - logs.append(message[LeapLogHandler.MESSAGE_KEY]) - - self.assertIn('test 01', logs[0]) - self.assertIn('test 02', logs[1]) - self.assertIn('test 03', logs[2]) - - def test_emits_signal(self): - log_format = '%(name)s - %(levelname)s - %(message)s' - formatter = logging.Formatter(log_format) - get_format = Mock(return_value=formatter) - self.leap_handler._handler._get_format = get_format - - self.leap_handler.new_log.connect(self._callback) - self.logger.debug('test') - - expected_log_msg = "test - DEBUG - test" - - # signal emitted - self.assertTrue(self.called) - - # emitted message - self.assertEqual(self.emitted_msg, expected_log_msg) - - # Mock called - self.assertTrue(get_format.called) - - -if __name__ == "__main__": - unittest.main() diff --git a/src/leap/bitmask/logs/utils.py b/src/leap/bitmask/logs/utils.py index 8367937a..72efae97 100644 --- a/src/leap/bitmask/logs/utils.py +++ b/src/leap/bitmask/logs/utils.py @@ -1,80 +1,57 @@ import logging import sys +from leap.bitmask.config import flags from leap.bitmask.logs import LOG_FORMAT from leap.bitmask.logs.log_silencer import SelectiveSilencerFilter -from leap.bitmask.logs.leap_log_handler import LeapLogHandler +from leap.bitmask.logs.safezmqhandler import SafeZMQHandler from leap.bitmask.logs.streamtologger import StreamToLogger from leap.bitmask.platform_init import IS_WIN +import logbook +from logbook.more import ColorizedStderrHandler -def create_logger(debug=False, logfile=None, replace_stdout=True): - """ - Create the logger and attach the handlers. - - :param debug: the level of the messages that we should log - :type debug: bool - :param logfile: the file name of where we should to save the logs - :type logfile: str - :return: the new logger with the attached handlers. - :rtype: logging.Logger - """ - # TODO: get severity from command line args - if debug: - level = logging.DEBUG - else: - level = logging.WARNING - # Create logger and formatter - logger = logging.getLogger(name='leap') - logger.setLevel(level) - formatter = logging.Formatter(LOG_FORMAT) +def get_logger(debug=True, logfile=None, replace_stdout=True): + level = logbook.WARNING + if flags.DEBUG: + level = logbook.NOTSET - # Console handler - try: - import coloredlogs - console = coloredlogs.ColoredStreamHandler(level=level) - except ImportError: - console = logging.StreamHandler() - console.setLevel(level) - console.setFormatter(formatter) - using_coloredlog = False - else: - using_coloredlog = True - - if using_coloredlog: - replace_stdout = False + # This handler consumes logs not handled by the others + null_handler = logbook.NullHandler(bubble=False) + null_handler.push_application() silencer = SelectiveSilencerFilter() - console.addFilter(silencer) - logger.addHandler(console) - logger.debug('Console handler plugged!') - # LEAP custom handler - leap_handler = LeapLogHandler() - leap_handler.setLevel(level) - leap_handler.addFilter(silencer) - logger.addHandler(leap_handler) - logger.debug('Leap handler plugged!') + zmq_handler = SafeZMQHandler('tcp://127.0.0.1:5000', multi=True, + level=level, filter=silencer.filter) + zmq_handler.push_application() + + file_handler = logbook.FileHandler('bitmask.log', format_string=LOG_FORMAT, + bubble=True, filter=silencer.filter) + file_handler.push_application() - # File handler - if logfile is not None: - logger.debug('Setting logfile to %s ', logfile) - fileh = logging.FileHandler(logfile) - fileh.setLevel(logging.DEBUG) - fileh.setFormatter(formatter) - fileh.addFilter(silencer) - logger.addHandler(fileh) - logger.debug('File handler plugged!') + # don't use simple stream, go for colored log handler instead + # stream_handler = logbook.StreamHandler(sys.stdout, + # format_string=LOG_FORMAT, + # bubble=True) + # stream_handler.push_application() + stream_handler = ColorizedStderrHandler( + level=level, format_string=LOG_FORMAT, bubble=True, + filter=silencer.filter) + stream_handler.push_application() - if replace_stdout: - replace_stdout_stderr_with_logging(logger) + logger = logbook.Logger('leap') return logger def replace_stdout_stderr_with_logging(logger): """ + NOTE: + we are not using this right now (see commented lines on app.py), + this needs to be reviewed since the log handler has changed. + Replace: - the standard output - the standard error diff --git a/src/leap/bitmask/util/leap_argparse.py b/src/leap/bitmask/util/leap_argparse.py index 12fd9736..383e5f8a 100644 --- a/src/leap/bitmask/util/leap_argparse.py +++ b/src/leap/bitmask/util/leap_argparse.py @@ -38,9 +38,6 @@ def build_parser(): help='Displays Bitmask version and exits.') # files - parser.add_argument('-l', '--logfile', metavar="LOG FILE", nargs='?', - action="store", dest="log_file", - help='Optional log file.') parser.add_argument('-m', '--mail-logfile', metavar="MAIL LOG FILE", nargs='?', action="store", dest="mail_log_file", -- cgit v1.2.3