diff options
| author | Ivan Alejandro <ivanalejandro0@gmail.com> | 2014-02-24 18:38:39 -0300 | 
|---|---|---|
| committer | Ivan Alejandro <ivanalejandro0@gmail.com> | 2014-02-26 10:52:32 -0300 | 
| commit | 4fddabec963015655a7129f1f95b95412b10ccf6 (patch) | |
| tree | 517aca426aeffbfa0090ec609880f8665c5ef4a7 | |
| parent | 0f56f7c2c5ac1367e42694b34778d6e1b513feb7 (diff) | |
Add pastebin support for upload logs.
| -rw-r--r-- | src/leap/bitmask/gui/loggerwindow.py | 61 | ||||
| -rw-r--r-- | src/leap/bitmask/util/constants.py | 1 | 
2 files changed, 62 insertions, 0 deletions
| diff --git a/src/leap/bitmask/gui/loggerwindow.py b/src/leap/bitmask/gui/loggerwindow.py index 6ef58558..b32852c0 100644 --- a/src/leap/bitmask/gui/loggerwindow.py +++ b/src/leap/bitmask/gui/loggerwindow.py @@ -22,10 +22,13 @@ import logging  import cgi  from PySide import QtGui +from twisted.internet import threads  from ui_loggerwindow import Ui_LoggerWindow +from leap.bitmask.util.constants import PASTEBIN_API_DEV_KEY  from leap.bitmask.util.leap_log_handler import LeapLogHandler +from leap.bitmask.util.pastebin import PastebinAPI, PastebinError  from leap.common.check import leap_assert, leap_assert_type  logger = logging.getLogger(__name__) @@ -42,6 +45,9 @@ class LoggerWindow(QtGui.QDialog):          :param handler: Custom handler that supports history and signal.          :type handler: LeapLogHandler.          """ +        from twisted.internet import reactor +        self.reactor = reactor +          QtGui.QDialog.__init__(self)          leap_assert(handler, "We need a handler for the logger window")          leap_assert_type(handler, LeapLogHandler) @@ -59,8 +65,10 @@ class LoggerWindow(QtGui.QDialog):          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._current_filter = "" +        self._current_history = ""          # Load logging history and connect logger with the widget          self._logging_handler = handler @@ -116,8 +124,13 @@ class LoggerWindow(QtGui.QDialog):          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 = cgi.escape(line[LeapLogHandler.MESSAGE_KEY]) +            current_history.append(message) + +        self._current_history = "\n".join(current_history)      def _set_logs_to_display(self):          """ @@ -164,3 +177,51 @@ class LoggerWindow(QtGui.QDialog):                  logger.error("Error saving log file: %r" % (e, ))          else:              logger.debug('Log not saved!') + +    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 = PastebinAPI() +            link = pb.paste(PASTEBIN_API_DEV_KEY, content, +                            paste_name="Bitmask log", +                            paste_expire_date='1W') +            return link + +        def pastebin_ok(link): +            """ +            Callback handler for `do_pastebin`. + +            :param link: the recently created pastebin link. +            :type link: str +            """ +            msg = self.tr("Your pastebin link <a href='{0}'>{0}</a>") +            msg = msg.format(link) +            logger.debug(msg) +            show_info = lambda: QtGui.QMessageBox.information( +                self, self.tr("Pastebin OK"), msg) +            self.reactor.callLater(0, show_info) + +        def pastebin_err(failure): +            """ +            Errback handler for `do_pastebin`. + +            :param failure: the failure that triggered the errback. +            :type failure: twisted.python.failure.Failure +            """ +            logger.error(repr(failure)) +            msg = self.tr("Sending logs to Pastebin failed!") +            show_err = lambda: QtGui.QMessageBox.error( +                self, self.tr("Pastebin Error"), msg) +            self.reactor.callLater(0, show_err) +            failure.trap(PastebinError) + +        d = threads.deferToThread(do_pastebin) +        d.addCallback(pastebin_ok) +        d.addErrback(pastebin_err) diff --git a/src/leap/bitmask/util/constants.py b/src/leap/bitmask/util/constants.py index e6a6bdce..e7e72cc4 100644 --- a/src/leap/bitmask/util/constants.py +++ b/src/leap/bitmask/util/constants.py @@ -17,3 +17,4 @@  SIGNUP_TIMEOUT = 5  REQUEST_TIMEOUT = 15 +PASTEBIN_API_DEV_KEY = "09563100642af6085d641f749a1922b4" | 
