Adding logging support
authorepage <eopage@byu.net>
Thu, 3 Sep 2009 01:53:54 +0000 (01:53 +0000)
committerepage <eopage@byu.net>
Thu, 3 Sep 2009 01:53:54 +0000 (01:53 +0000)
git-svn-id: file:///svnroot/gc-dialer/trunk@431 c39d3808-3fe2-4d86-a59f-b7f623ee9f21

src/browser_emu.py
src/dc_glade.py
src/gtk_toolbox.py
src/gv_backend.py
src/gv_views.py

index 5dafaa9..88a0b62 100644 (file)
@@ -27,7 +27,7 @@ Optional steps:
 
 import urllib2
 import cookielib
-import warnings
+import logging
 
 import socket
 
@@ -100,7 +100,7 @@ class MozillaEmulator(object):
 
                @return: The raw HTML page data
                """
-               warnings.warn("Performing download of %s" % url, UserWarning, 2)
+               logging.warning("Performing download of %s" % url)
 
                if extraheaders is None:
                        extraheaders = {}
index abec548..4d16c74 100755 (executable)
@@ -29,7 +29,7 @@ import threading
 import base64
 import ConfigParser
 import itertools
-import warnings
+import logging
 
 import gtk
 import gtk.glade
@@ -130,7 +130,8 @@ class Dialcentral(object):
                        self._window.connect("key-press-event", self._on_key_press)
                        self._window.connect("window-state-event", self._on_window_state_change)
                else:
-                       pass # warnings.warn("No Hildon", UserWarning, 2)
+                       logging.warning("No hildonization support")
+
 
                hildonize.set_application_title(self._window, "%s" % constants.__pretty_app_name__)
 
@@ -188,7 +189,7 @@ class Dialcentral(object):
                                device = osso.DeviceState(self._osso)
                                device.set_device_state_callback(self._on_device_state_change, 0)
                        else:
-                               pass # warnings.warn("No OSSO", UserWarning, 2)
+                               logging.warning("No device state support")
 
                        try:
                                import alarm_handler
@@ -199,6 +200,7 @@ class Dialcentral(object):
                                with gtk_toolbox.gtk_lock():
                                        self._errorDisplay.push_exception()
                                alarm_handler = None
+                               logging.warning("No notification support")
                        if hildonize.IS_HILDON:
                                import led_handler
                                self._ledHandler = led_handler.LedHandler()
@@ -214,7 +216,7 @@ class Dialcentral(object):
                                self._connection.connect("connection-event", self._on_connection_change, constants.__app_magic__)
                                self._connection.request_connection(conic.CONNECT_FLAG_NONE)
                        else:
-                               pass # warnings.warn("No Internet Connectivity API ", UserWarning)
+                               logging.warning("No connection support")
 
                        # Setup costly backends
                        import gv_backend
@@ -326,7 +328,7 @@ class Dialcentral(object):
                                        serviceId = self._defaultBackendId
                                        loggedIn = True
                                except StandardError, e:
-                                       warnings.warn('Session refresh failed with the following message "%s"' % e.message, UserWarning, 2)
+                                       logging.exception('Session refresh failed with the following message "%s"' % e.message)
 
                        if not loggedIn:
                                loggedIn, serviceId = self._login_by_user(numOfAttempts)
@@ -361,10 +363,7 @@ class Dialcentral(object):
                """
                loggedIn = self._phoneBackends[self._defaultBackendId].is_authed()
                if loggedIn:
-                       warnings.warn(
-                               "Logged into %r through cookies" % self._phoneBackends[self._defaultBackendId],
-                               UserWarning, 2
-                       )
+                       logging.info("Logged into %r through cookies" % self._phoneBackends[self._defaultBackendId])
                return loggedIn
 
        def _login_by_settings(self):
@@ -375,10 +374,7 @@ class Dialcentral(object):
                loggedIn = self._phoneBackends[self._defaultBackendId].login(username, password)
                if loggedIn:
                        self._credentials = username, password
-                       warnings.warn(
-                               "Logged into %r through settings" % self._phoneBackends[self._defaultBackendId],
-                               UserWarning, 2
-                       )
+                       logging.info("Logged into %r through settings" % self._phoneBackends[self._defaultBackendId])
                return loggedIn
 
        def _login_by_user(self, numOfAttempts):
@@ -400,10 +396,7 @@ class Dialcentral(object):
                if loggedIn:
                        serviceId = tmpServiceId
                        self._credentials = username, password
-                       warnings.warn(
-                               "Logged into %r through user request" % self._phoneBackends[serviceId],
-                               UserWarning, 2
-                       )
+                       logging.info("Logged into %r through user request" % self._phoneBackends[serviceId])
                else:
                        serviceId = self.NULL_BACKEND
 
@@ -463,20 +456,18 @@ class Dialcentral(object):
                        if self._alarmHandler is not None:
                                self._alarmHandler.load_settings(config, "alarm")
                except ConfigParser.NoOptionError, e:
-                       warnings.warn(
+                       logging.exception(
                                "Settings file %s is missing section %s" % (
                                        constants._user_settings_,
                                        e.section,
                                ),
-                               stacklevel=2
                        )
                except ConfigParser.NoSectionError, e:
-                       warnings.warn(
+                       logging.exception(
                                "Settings file %s is missing section %s" % (
                                        constants._user_settings_,
                                        e.section,
                                ),
-                               stacklevel=2
                        )
 
                for backendId, view in itertools.chain(
@@ -490,20 +481,18 @@ class Dialcentral(object):
                        try:
                                view.load_settings(config, sectionName)
                        except ConfigParser.NoOptionError, e:
-                               warnings.warn(
+                               logging.exception(
                                        "Settings file %s is missing section %s" % (
                                                constants._user_settings_,
                                                e.section,
                                        ),
-                                       stacklevel=2
                                )
                        except ConfigParser.NoSectionError, e:
-                               warnings.warn(
+                               logging.exception(
                                        "Settings file %s is missing section %s" % (
                                                constants._user_settings_,
                                                e.section,
                                        ),
-                                       stacklevel=2
                                )
 
        def save_settings(self, config):
@@ -766,21 +755,29 @@ class DummyOptions(object):
 
 
 if __name__ == "__main__":
-       if len(sys.argv) > 1:
-               try:
-                       import optparse
-               except ImportError:
-                       optparse = None
-
-               if optparse is not None:
-                       parser = optparse.OptionParser()
-                       parser.add_option("-t", "--test", action="store_true", dest="test", help="Run tests")
-                       (commandOptions, commandArgs) = parser.parse_args()
+       if hildonize.IS_HILDON:
+               userLogPath = "%s/dialcentral.log" % constants._data_path_
+               logging.basicConfig(level=logging.DEBUG, filename=userLogPath)
        else:
-               commandOptions = DummyOptions()
-               commandArgs = []
+               logging.basicConfig(level=logging.DEBUG)
+       try:
+               if len(sys.argv) > 1:
+                       try:
+                               import optparse
+                       except ImportError:
+                               optparse = None
 
-       if commandOptions.test:
-               run_doctest()
-       else:
-               run_dialpad()
+                       if optparse is not None:
+                               parser = optparse.OptionParser()
+                               parser.add_option("-t", "--test", action="store_true", dest="test", help="Run tests")
+                               (commandOptions, commandArgs) = parser.parse_args()
+               else:
+                       commandOptions = DummyOptions()
+                       commandArgs = []
+
+               if commandOptions.test:
+                       run_doctest()
+               else:
+                       run_dialpad()
+       finally:
+               logging.shutdown()
index 16c49c6..cb27d7b 100644 (file)
@@ -6,10 +6,9 @@ import os
 import errno
 import sys
 import time
-import traceback
 import functools
 import contextlib
-import warnings
+import logging
 import threading
 import Queue
 
@@ -394,19 +393,14 @@ class ErrorDisplay(object):
                else:
                        self.__show_message(message)
 
-       def push_exception_with_lock(self, exception = None, stacklevel=3):
+       def push_exception_with_lock(self):
                with gtk_lock():
-                       self.push_exception(exception, stacklevel=stacklevel)
+                       self.push_exception()
 
-       def push_exception(self, exception = None, stacklevel=2):
-               if exception is None:
-                       userMessage = str(sys.exc_value)
-                       warningMessage = str(traceback.format_exc())
-               else:
-                       userMessage = str(exception)
-                       warningMessage = str(exception)
+       def push_exception(self):
+               userMessage = str(sys.exc_value)
                self.push_message(userMessage)
-               warnings.warn(warningMessage, stacklevel=stacklevel)
+               logging.exception(userMessage)
 
        def pop_message(self):
                if 0 < len(self.__messages):
@@ -445,11 +439,8 @@ class DummyErrorDisplay(object):
                        self.__show_message(message)
 
        def push_exception(self, exception = None):
-               if exception is None:
-                       warningMessage = traceback.format_exc()
-               else:
-                       warningMessage = exception
-               warnings.warn(warningMessage, stacklevel=3)
+               userMessage = str(sys.exc_value)
+               logging.exception(userMessage)
 
        def pop_message(self):
                if 0 < len(self.__messages):
@@ -457,7 +448,7 @@ class DummyErrorDisplay(object):
                        del self.__messages[0]
 
        def __show_message(self, message):
-               warnings.warn(message, stacklevel=2)
+               logging.debug(message)
 
 
 class MessageBox(gtk.MessageDialog):
@@ -531,7 +522,7 @@ class PopupCalendar(object):
                        self._calendar.select_month(self._displayDate.month, self._displayDate.year)
                        self._calendar.select_day(self._displayDate.day)
                except StandardError, e:
-                       warnings.warn(e.message)
+                       logging.exception(e.message)
 
 
 class QuickAddView(object):
index 29021c2..6aab60e 100644 (file)
@@ -33,8 +33,7 @@ import urllib2
 import time
 import datetime
 import itertools
-import warnings
-import traceback
+import logging
 from xml.sax import saxutils
 
 from xml.etree import ElementTree
@@ -134,7 +133,7 @@ class GVDialer(object):
                try:
                        self._grab_account_info()
                except StandardError, e:
-                       warnings.warn(traceback.format_exc())
+                       logging.exception(str(e))
                        return False
 
                self._browser.cookies.save()
@@ -163,7 +162,7 @@ class GVDialer(object):
                try:
                        loginSuccessOrFailurePage = self._browser.download(self._loginURL, loginPostData)
                except urllib2.URLError, e:
-                       warnings.warn(traceback.format_exc())
+                       logging.exception(str(e))
                        raise RuntimeError("%s is not accesible" % self._loginURL)
 
                return self.is_authed()
@@ -194,7 +193,7 @@ class GVDialer(object):
                        }
                        callSuccessPage = self._browser.download(self._clicktocallURL, clickToCallData, None, otherData)
                except urllib2.URLError, e:
-                       warnings.warn(traceback.format_exc())
+                       logging.exception(str(e))
                        raise RuntimeError("%s is not accesible" % self._clicktocallURL)
 
                if self._gvDialingStrRe.search(callSuccessPage) is None:
@@ -219,7 +218,7 @@ class GVDialer(object):
                        }
                        smsSuccessPage = self._browser.download(self._sendSmsURL, smsData, None, otherData)
                except urllib2.URLError, e:
-                       warnings.warn(traceback.format_exc())
+                       logging.exception(str(e))
                        raise RuntimeError("%s is not accesible" % self._sendSmsURL)
 
                return True
@@ -287,28 +286,12 @@ class GVDialer(object):
                @param callbacknumber should be a proper 10 digit number
                """
                self._callbackNumber = callbacknumber
-
-               # Currently this isn't working out in GoogleVoice, but thats ok, we pass the callback on dial
-               #callbackPostData = urllib.urlencode({
-               #       '_rnr_se': self._token,
-               #       'phone': callbacknumber
-               #})
-               #try:
-               #       callbackSetPage = self._browser.download(self._setforwardURL, callbackPostData)
-               #       self._browser.cookies.save()
-               #except urllib2.URLError, e:
-               #       warnings.warn(traceback.format_exc())
-               #       raise RuntimeError("%s is not accesible" % self._setforwardURL)
-
                return True
 
        def get_callback_number(self):
                """
                @returns Current callback number or None
                """
-               #for c in self._browser.cookies:
-               #       if c.name == "gv-ph":
-               #               return c.value
                return self._callbackNumber
 
        def get_recent(self):
@@ -356,7 +339,7 @@ class GVDialer(object):
                                try:
                                        contactsPage = self._browser.download(contactsPageUrl)
                                except urllib2.URLError, e:
-                                       warnings.warn(traceback.format_exc())
+                                       logging.exception(str(e))
                                        raise RuntimeError("%s is not accesible" % contactsPageUrl)
                                for contact_match in self._contactsRe.finditer(contactsPage):
                                        contactId = contact_match.group(1)
@@ -383,7 +366,7 @@ class GVDialer(object):
                try:
                        detailPage = self._browser.download(self._contactDetailURL + '/' + contactId)
                except urllib2.URLError, e:
-                       warnings.warn(traceback.format_exc())
+                       logging.exception(str(e))
                        raise RuntimeError("%s is not accesible" % self._contactDetailURL)
 
                for detail_match in self._contactDetailPhoneRe.finditer(detailPage):
@@ -398,7 +381,7 @@ class GVDialer(object):
                try:
                        voicemailPage = self._browser.download(self._voicemailURL)
                except urllib2.URLError, e:
-                       warnings.warn(traceback.format_exc())
+                       logging.exception(str(e))
                        raise RuntimeError("%s is not accesible" % self._voicemailURL)
                voicemailHtml = self._grab_html(voicemailPage)
                parsedVoicemail = self._parse_voicemail(voicemailHtml)
@@ -407,7 +390,7 @@ class GVDialer(object):
                try:
                        smsPage = self._browser.download(self._smsURL)
                except urllib2.URLError, e:
-                       warnings.warn(traceback.format_exc())
+                       logging.exception(str(e))
                        raise RuntimeError("%s is not accesible" % self._smsURL)
                smsHtml = self._grab_html(smsPage)
                parsedSms = self._parse_sms(smsHtml)
@@ -449,7 +432,7 @@ class GVDialer(object):
                if anGroup is not None:
                        self._accountNum = anGroup.group(1)
                else:
-                       warnings.warn("Could not extract account number from GoogleVoice", UserWarning, 2)
+                       logging.debug("Could not extract account number from GoogleVoice")
 
                self._callbackNumbers = {}
                for match in self._callbackRe.finditer(page):
@@ -485,7 +468,7 @@ class GVDialer(object):
                        try:
                                flatXml = self._browser.download(url)
                        except urllib2.URLError, e:
-                               warnings.warn(traceback.format_exc())
+                               logging.exception(str(e))
                                raise RuntimeError("%s is not accesible" % url)
 
                        allRecentHtml = self._grab_html(flatXml)
@@ -630,7 +613,6 @@ class GVDialer(object):
 
 
 def test_backend(username, password):
-       import pprint
        backend = GVDialer()
        print "Authenticated: ", backend.is_authed()
        print "Login?: ", backend.login(username, password)
@@ -639,6 +621,7 @@ def test_backend(username, password):
        print "Account: ", backend.get_account_number()
        print "Callback: ", backend.get_callback_number()
        # print "All Callback: ",
+       #import pprint
        # pprint.pprint(backend.get_callback_numbers())
        # print "Recent: ",
        # pprint.pprint(list(backend.get_recent()))
index 91f75d2..c1a0fff 100644 (file)
@@ -22,7 +22,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 from __future__ import with_statement
 
 import ConfigParser
-import warnings
+import logging
 
 import gobject
 import pango
@@ -733,23 +733,20 @@ class AccountInfo(object):
                        if not self._backend.is_valid_syntax(number):
                                self._errorDisplay.push_message("%s is not a valid callback number" % number)
                        elif number == self._backend.get_callback_number():
-                               warnings.warn(
+                               logging.warning(
                                        "Callback number already is %s" % (
                                                self._backend.get_callback_number(),
                                        ),
-                                       UserWarning,
-                                       2
                                )
                        else:
                                self._backend.set_callback_number(number)
                                assert make_ugly(number) == make_ugly(self._backend.get_callback_number()), "Callback number should be %s but instead is %s" % (
                                        make_pretty(number), make_pretty(self._backend.get_callback_number())
                                )
-                               warnings.warn(
+                               logging.info(
                                        "Callback number set to %s" % (
                                                self._backend.get_callback_number(),
                                        ),
-                                       UserWarning, 2
                                )
                except StandardError, e:
                        self._errorDisplay.push_exception()