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

from __future__ import with_statement

__author__ = "<NAME> <<EMAIL>>"

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 = []
_LOGFILE = None
_PREV_EXCEPTHOOK = None

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

    smtp = SMTP("mailhost.somewhere.com")
    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(ctime())
    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):
    try:
        if not (_EMAILS or _LOGFILE):
            return

        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

    finally:
        if _PREV_EXCEPTHOOK:
            _PREV_EXCEPTHOOK(type, value, traceback)

def install(emails=None, logfile=None):
    global _EMAILS, _PREV_EXCEPTHOOK, _LOGFILE

    _EMAILS = emails or _EMAILS
    _LOGFILE = logfile
    _PREV_EXCEPTHOOK = sys.excepthook
    sys.excepthook = excepthook<commit_msg>space<commit_after>'''Send email and log when there is an uncaught exception in your code
Use in your code:
import crashlog; crashlog.install(emails=["<EMAIL>"])
'''

from __future__ import with_statement

__author__ = "<NAME> <<EMAIL>>"

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 = []
_LOGFILE = None
_PREV_EXCEPTHOOK = None

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

    smtp = SMTP("mailhost.somewhere.com")
    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(ctime())
    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):
    try:
        if not (_EMAILS or _LOGFILE):
            return

        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

    finally:
        if _PREV_EXCEPTHOOK:
            _PREV_EXCEPTHOOK(type, value, traceback)

def install(emails=None, logfile=None):
    global _EMAILS, _PREV_EXCEPTHOOK, _LOGFILE

    _EMAILS = emails or _EMAILS
    _LOGFILE = logfile
    _PREV_EXCEPTHOOK = sys.excepthook
    sys.excepthook = excepthook
{'fraction_non_alphanumeric': 0.11975407645014702, 'fraction_numerical': 0.0005346164127238706, 'mean_word_length': 3.3511627906976744, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '166379', 'n_tokens_mistral': 1288, 'n_tokens_neox': 1280, 'n_words': 331}