pythonwise /

'''Send email and log when there is an uncaught exception in your code
Use in your code:
import crashlog; crashlog.install(emails=[""])

from __future__ import with_statement

__author__ = "Miki Tebeka <>"

import sys
from smtplib import SMTP
from email.mime.text import MIMEText
from os import environ
from traceback import format_exception
from cStringIO import StringIO
from time import ctime

_EMAILS = []

def send_email(emails, program, message):
    message = MIMEText(message)
    message["Subject"] = "%s crashed" % program
    crashlog_email = ""
    message["From"] = "Crashlog <%s>" % crashlog_email

    smtp = SMTP("")
    smtp.sendmail(crashlog_email, _EMAILS, message.as_string())

def format_message(type, value, traceback):
    message = StringIO()
    def out(m): message.write(u"%s\n" % m)

    out("== Traceback ==")
    out("".join(format_exception(type, value, traceback)))
    out("\n== Command line ==")
    out(" ".join(sys.argv))
    out("\n== Environment ==")
    for key, value in environ.items():
        out("%s = %s" % (key, value))

    return message.getvalue()

def excepthook(type, value, traceback):
        if not (_EMAILS or _LOGFILE):

        message = format_message(type, value, traceback)
        if _EMAILS:
            send_email(_EMAILS, sys.argv[0], message)

        if _LOGFILE:
            with open(_LOGFILE, "at") as fo:
                print >> fo, message

        if _PREV_EXCEPTHOOK:
            _PREV_EXCEPTHOOK(type, value, traceback)

def install(emails=None, logfile=None):

    _EMAILS = emails or _EMAILS
    _LOGFILE = logfile
    _PREV_EXCEPTHOOK = sys.excepthook
    sys.excepthook = excepthook
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.