-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
-Include GUI in logging and messaging system
-Log all GUI infos and warnings through logging module
- Loading branch information
Artur Glavic
committed
Apr 4, 2013
1 parent
61c3bfd
commit 9f8e663
Showing
4 changed files
with
126 additions
and
119 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ | |
import sys | ||
import atexit | ||
import logging | ||
import traceback | ||
from .version import str_version | ||
|
||
# default options used | ||
|
@@ -23,30 +24,126 @@ | |
LOG_DIR=os.path.join(USER_DIR, 'debug.log') | ||
if not os.path.exists(USER_DIR): | ||
os.makedirs(USER_DIR) | ||
ADMIN_MAIL='[email protected]' | ||
|
||
def excepthook_overwrite(*exc_info): | ||
logging.error('unexpected python error', exc_info=exc_info) | ||
logging.critical('python error', exc_info=exc_info) | ||
|
||
def goodby(): | ||
logging.debug('*** QuickNXS %s Logging ended ***'%str_version) | ||
|
||
def setup_system(): | ||
logger=logging.root#logging.getLogger('quick_nxs') | ||
logger.setLevel(logging.DEBUG) | ||
console=logging.StreamHandler(sys.__stdout__) | ||
formatter=logging.Formatter('%(levelname) 7s: %(message)s') | ||
console.setFormatter(formatter) | ||
console.setLevel(CONSOLE_LEVEL) | ||
|
||
logger.addHandler(console) | ||
logfile=logging.FileHandler(LOG_DIR, 'w') | ||
formatter=logging.Formatter('[%(levelname)s] - %(asctime)s - %(filename)s:%(lineno)i:%(funcName)s %(message)s', | ||
'') | ||
logfile.setFormatter(formatter) | ||
logfile.setLevel(FILE_LEVEL) | ||
logger.addHandler(logfile) | ||
|
||
logging.debug('*** QuickNXS %s Logging started ***'%str_version) | ||
|
||
sys.excepthook=excepthook_overwrite | ||
atexit.register(goodby) | ||
|
||
class QtHandler(logging.Handler): | ||
''' | ||
A logging Handler to be used by a GUI widget to show the data. | ||
''' | ||
max_items=1e5 | ||
info_limit=logging.INFO | ||
warn_limit=logging.WARNING | ||
|
||
def __init__(self, main_window): | ||
logging.Handler.__init__(self, level=GUI_LEVEL) | ||
self.logged_items=[] | ||
self.main_window=main_window | ||
|
||
def emit(self, record): | ||
self.logged_items.append(record) | ||
# make sure the buffer doesn't get infinitly large | ||
if len(self.logged_items)>self.max_items: | ||
self.logged_items.pop(0) | ||
if record.levelno<=self.info_limit: | ||
self.show_info(record) | ||
elif record.levelno<=self.warn_limit: | ||
self.show_warning(record) | ||
else: | ||
self.show_error(record) | ||
|
||
def show_info(self, record): | ||
msg=record.msg | ||
if record.levelno!=logging.INFO: | ||
msg=record.levelname+': '+msg | ||
self.main_window.ui.statusbar.showMessage(msg, 5000.) | ||
|
||
def show_warning(self, record): | ||
''' | ||
Warning messages display a dialog to the user. | ||
''' | ||
from PyQt4.QtGui import QMessageBox | ||
QMessageBox.warning(self.main_window, 'QuickNXS '+record.levelname, record.msg) | ||
|
||
def show_error(self, record): | ||
''' | ||
More urgent error messages allow to send a bug report. | ||
''' | ||
from PyQt4.QtGui import QMessageBox | ||
from PyQt4.QtCore import Qt | ||
mbox=QMessageBox(self.main_window) | ||
mbox.setIcon(QMessageBox.Critical) | ||
mbox.setTextFormat(Qt.RichText) | ||
mbox.setInformativeText('Do you want to send the logfile to the developer?') | ||
mbox.setStandardButtons(QMessageBox.Yes|QMessageBox.No) | ||
mbox.setDefaultButton(QMessageBox.No) | ||
|
||
if record.exc_info: | ||
tb=traceback.format_exception(*record.exc_info) | ||
message='\n'.join(tb) | ||
mbox.setDetailedText(message) | ||
mbox.setText(u'An unexpected error has occurred: <b>%s</b><br /> <i>%s</i>: %s'%( | ||
record.msg, | ||
record.exc_info[0].__name__, | ||
record.exc_info[1])) | ||
else: | ||
mbox.setText(u'An unexpected error has occurred: <br /> <b>%s</b>'%record.msg) | ||
result=mbox.exec_() | ||
if result==QMessageBox.Yes: | ||
logging.info('Sending mail') | ||
try: | ||
import smtplib | ||
from email.mime.multipart import MIMEMultipart | ||
from email.mime.text import MIMEText | ||
from getpass import getuser | ||
|
||
msg=MIMEMultipart() | ||
msg['Subject']='QuickNXS error report' | ||
msg['From']='%[email protected]'%getuser() | ||
msg['To']=ADMIN_MAIL | ||
text='This is an automatic bugreport from QuickNXS\n\n%s'%record.msg | ||
if record.exc_info: | ||
text+='\n\n'+message | ||
text+='\n' | ||
msg.preamble=text | ||
msg.attach(MIMEText(text)) | ||
|
||
mitem=MIMEText(open(LOG_DIR, 'r').read(), 'log') | ||
mitem.add_header('Content-Disposition', 'attachment', filename='debug.log') | ||
msg.attach(mitem) | ||
|
||
smtp=smtplib.SMTP('160.91.4.26') | ||
smtp.sendmail(msg['From'], msg['To'].split(','), msg.as_string()) | ||
smtp.quit() | ||
logging.info('Mail sent') | ||
except: | ||
logging.warning('problem sending the mail', exc_info=True) | ||
|
||
def install_gui_handler(main_window): | ||
logging.root.addHandler(QtHandler(main_window)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.