2014-03-10 01:18:05 -04:00
|
|
|
# Author: Nic Wolfe <nic@wolfeden.ca>
|
|
|
|
# URL: http://code.google.com/p/sickbeard/
|
|
|
|
#
|
2014-05-23 08:37:22 -04:00
|
|
|
# This file is part of SickRage.
|
2014-03-10 01:18:05 -04:00
|
|
|
#
|
2014-05-23 08:37:22 -04:00
|
|
|
# SickRage is free software: you can redistribute it and/or modify
|
2014-03-10 01:18:05 -04:00
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
2014-05-23 08:37:22 -04:00
|
|
|
# SickRage is distributed in the hope that it will be useful,
|
2014-03-10 01:18:05 -04:00
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
2014-05-23 08:37:22 -04:00
|
|
|
# along with SickRage. If not, see <http://www.gnu.org/licenses/>.
|
2014-03-10 01:18:05 -04:00
|
|
|
|
|
|
|
from __future__ import with_statement
|
|
|
|
import os
|
2014-12-19 16:24:41 -05:00
|
|
|
import re
|
2014-03-10 01:18:05 -04:00
|
|
|
import sys
|
|
|
|
import logging
|
2014-12-16 05:24:06 -05:00
|
|
|
import logging.handlers
|
|
|
|
import threading
|
2014-12-18 16:32:44 -05:00
|
|
|
import platform
|
2014-03-10 01:18:05 -04:00
|
|
|
|
|
|
|
import sickbeard
|
2014-12-19 16:24:41 -05:00
|
|
|
from sickbeard import classes, encodingKludge as ek
|
2014-12-18 16:32:44 -05:00
|
|
|
from github import Github
|
|
|
|
from pastebin import PastebinAPI
|
2014-03-10 01:18:05 -04:00
|
|
|
|
2014-12-16 05:24:06 -05:00
|
|
|
# log levels
|
2014-03-10 01:18:05 -04:00
|
|
|
ERROR = logging.ERROR
|
|
|
|
WARNING = logging.WARNING
|
2014-12-16 05:24:06 -05:00
|
|
|
INFO = logging.INFO
|
2014-03-10 01:18:05 -04:00
|
|
|
DEBUG = logging.DEBUG
|
2014-03-25 01:57:24 -04:00
|
|
|
DB = 5
|
2014-03-10 01:18:05 -04:00
|
|
|
|
2014-12-16 05:35:27 -05:00
|
|
|
reverseNames = {u'ERROR': ERROR,
|
|
|
|
u'WARNING': WARNING,
|
|
|
|
u'INFO': INFO,
|
|
|
|
u'DEBUG': DEBUG,
|
|
|
|
u'DB': DB}
|
|
|
|
|
2014-12-16 21:01:36 -05:00
|
|
|
censoredItems = {}
|
|
|
|
|
|
|
|
class NullHandler(logging.Handler):
|
|
|
|
def emit(self, record):
|
|
|
|
pass
|
|
|
|
|
2014-12-17 03:36:42 -05:00
|
|
|
class CensoredFormatter(logging.Formatter):
|
|
|
|
def format(self, record):
|
2014-12-20 18:30:24 -05:00
|
|
|
msg = record.getMessage()
|
2014-12-17 02:54:24 -05:00
|
|
|
for k, v in censoredItems.items():
|
2014-12-16 21:01:36 -05:00
|
|
|
if v and len(v) > 0 and v in msg:
|
|
|
|
msg = msg.replace(v, len(v) * '*')
|
2014-12-17 03:36:42 -05:00
|
|
|
return msg
|
2014-12-16 21:01:36 -05:00
|
|
|
|
|
|
|
class Logger(object):
|
|
|
|
def __init__(self):
|
2014-12-17 03:36:42 -05:00
|
|
|
self.logger = logging.getLogger('sickrage')
|
2014-12-17 02:20:37 -05:00
|
|
|
|
|
|
|
self.loggers = [
|
|
|
|
logging.getLogger('sickrage'),
|
|
|
|
logging.getLogger('tornado.general'),
|
|
|
|
logging.getLogger('tornado.application'),
|
2014-12-17 02:48:31 -05:00
|
|
|
#logging.getLogger('tornado.access'),
|
2014-12-17 02:20:37 -05:00
|
|
|
]
|
|
|
|
|
2014-12-16 21:01:36 -05:00
|
|
|
self.consoleLogging = False
|
|
|
|
self.fileLogging = False
|
|
|
|
self.debugLogging = False
|
|
|
|
self.logFile = None
|
|
|
|
|
|
|
|
def initLogging(self, consoleLogging=False, fileLogging=False, debugLogging=False):
|
2014-12-17 02:20:37 -05:00
|
|
|
self.logFile = self.logFile or os.path.join(sickbeard.LOG_DIR, 'sickrage.log')
|
|
|
|
self.debugLogging = debugLogging
|
|
|
|
self.consoleLogging = consoleLogging
|
|
|
|
self.fileLogging = fileLogging
|
2014-12-16 21:01:36 -05:00
|
|
|
|
|
|
|
# add a new logging level DB
|
|
|
|
logging.addLevelName(DB, 'DB')
|
|
|
|
|
2014-12-17 02:33:35 -05:00
|
|
|
# nullify root logger
|
|
|
|
logging.getLogger().addHandler(NullHandler())
|
|
|
|
|
2014-12-17 02:20:37 -05:00
|
|
|
# set custom root logger
|
|
|
|
for logger in self.loggers:
|
2014-12-17 03:36:42 -05:00
|
|
|
if logger is not self.logger:
|
|
|
|
logger.root = self.logger
|
|
|
|
logger.parent = self.logger
|
2014-12-16 21:01:36 -05:00
|
|
|
|
2014-12-17 02:20:37 -05:00
|
|
|
# set minimum logging level allowed for loggers
|
|
|
|
for logger in self.loggers:
|
|
|
|
logger.setLevel(DB)
|
2014-12-16 21:01:36 -05:00
|
|
|
|
|
|
|
# console log handler
|
2014-12-17 02:20:37 -05:00
|
|
|
if self.consoleLogging:
|
2014-12-16 21:01:36 -05:00
|
|
|
console = logging.StreamHandler()
|
2014-12-17 03:36:42 -05:00
|
|
|
console.setFormatter(CensoredFormatter('%(asctime)s %(levelname)s::%(message)s', '%H:%M:%S'))
|
2014-12-17 02:20:37 -05:00
|
|
|
console.setLevel(INFO if not self.debugLogging else DEBUG)
|
|
|
|
|
|
|
|
for logger in self.loggers:
|
|
|
|
logger.addHandler(console)
|
2014-12-16 21:01:36 -05:00
|
|
|
|
|
|
|
# rotating log file handler
|
2014-12-17 02:20:37 -05:00
|
|
|
if self.fileLogging:
|
2014-12-16 21:01:36 -05:00
|
|
|
rfh = logging.handlers.RotatingFileHandler(self.logFile, maxBytes=1024 * 1024, backupCount=5, encoding='utf-8')
|
2014-12-17 03:36:42 -05:00
|
|
|
rfh.setFormatter(CensoredFormatter('%(asctime)s %(levelname)-8s %(message)s', '%Y-%m-%d %H:%M:%S'))
|
2014-12-17 02:20:37 -05:00
|
|
|
rfh.setLevel(DEBUG)
|
|
|
|
|
|
|
|
for logger in self.loggers:
|
|
|
|
logger.addHandler(rfh)
|
2014-12-16 21:01:36 -05:00
|
|
|
|
|
|
|
def log(self, msg, level=INFO, *args, **kwargs):
|
|
|
|
meThread = threading.currentThread().getName()
|
|
|
|
message = meThread + u" :: " + msg
|
|
|
|
|
2014-12-17 02:20:37 -05:00
|
|
|
# pass exception information if debugging enabled
|
2014-12-16 21:01:36 -05:00
|
|
|
|
2014-12-18 16:32:44 -05:00
|
|
|
if level == ERROR:
|
2014-12-20 08:49:35 -05:00
|
|
|
self.logger.exception(message, *args, **kwargs)
|
2014-12-18 16:32:44 -05:00
|
|
|
classes.ErrorViewer.add(classes.UIError(message))
|
2014-12-20 08:49:35 -05:00
|
|
|
|
2014-12-18 16:32:44 -05:00
|
|
|
#if sickbeard.GIT_AUTOISSUES:
|
|
|
|
# self.submit_errors()
|
2014-12-20 08:49:35 -05:00
|
|
|
else:
|
|
|
|
self.logger.log(level, message, *args, **kwargs)
|
2014-12-18 16:32:44 -05:00
|
|
|
|
2014-12-16 21:01:36 -05:00
|
|
|
def log_error_and_exit(self, error_msg, *args, **kwargs):
|
|
|
|
self.log(error_msg, ERROR, *args, **kwargs)
|
|
|
|
|
|
|
|
if not self.consoleLogging:
|
|
|
|
sys.exit(error_msg.encode(sickbeard.SYS_ENCODING, 'xmlcharrefreplace'))
|
|
|
|
else:
|
|
|
|
sys.exit(1)
|
|
|
|
|
2014-12-18 16:32:44 -05:00
|
|
|
def submit_errors(self):
|
|
|
|
if not (sickbeard.GIT_USERNAME and sickbeard.GIT_PASSWORD and len(classes.ErrorViewer.errors) > 0):
|
|
|
|
return
|
|
|
|
|
|
|
|
gh_org = sickbeard.GIT_ORG or 'SiCKRAGETV'
|
|
|
|
gh_repo = 'sickrage-issues'
|
|
|
|
|
|
|
|
self.gh_issues = Github(login_or_token=sickbeard.GIT_USERNAME, password=sickbeard.GIT_PASSWORD,
|
|
|
|
user_agent="SiCKRAGE").get_organization(gh_org).get_repo(gh_repo)
|
|
|
|
|
|
|
|
try:
|
2014-12-20 08:49:35 -05:00
|
|
|
# read log file
|
2014-12-18 19:16:55 -05:00
|
|
|
if self.logFile and os.path.isfile(self.logFile):
|
2014-12-19 16:24:41 -05:00
|
|
|
with ek.ek(open, self.logFile) as f:
|
|
|
|
log_data = f.readlines()
|
2014-12-20 08:49:35 -05:00
|
|
|
log_data = [line for line in reversed(log_data)]
|
2014-12-18 16:32:44 -05:00
|
|
|
|
2014-12-20 08:49:35 -05:00
|
|
|
# parse and submit errors to issue tracker
|
2014-12-18 16:32:44 -05:00
|
|
|
for curError in sorted(classes.ErrorViewer.errors, key=lambda error: error.time, reverse=True)[:500]:
|
2014-12-18 16:48:35 -05:00
|
|
|
if not curError.title:
|
|
|
|
continue
|
|
|
|
|
2014-12-19 16:24:41 -05:00
|
|
|
pastebin_url = None
|
2014-12-20 08:49:35 -05:00
|
|
|
regex = "^(%s)\s*([A-Z]+)\s*(.+?)\s*\:\:\s*(.*)$" % curError.time
|
|
|
|
for i, x in enumerate(log_data):
|
2014-12-19 16:24:41 -05:00
|
|
|
x = ek.ss(x)
|
|
|
|
match = re.match(regex, x)
|
|
|
|
if match:
|
|
|
|
level = match.group(2)
|
2014-12-20 03:58:40 -05:00
|
|
|
if reverseNames[level] == ERROR:
|
2014-12-20 08:49:35 -05:00
|
|
|
paste_data = "".join(log_data[i:50])
|
2014-12-19 16:24:41 -05:00
|
|
|
pastebin_url = PastebinAPI().paste('f59b8e9fa1fc2d033e399e6c7fb09d19', paste_data)
|
|
|
|
break
|
|
|
|
|
2014-12-18 16:32:44 -05:00
|
|
|
message = u"### INFO\n"
|
|
|
|
message += u"Python Version: **" + sys.version[:120] + "**\n"
|
|
|
|
message += u"Operating System: **" + platform.platform() + "**\n"
|
|
|
|
message += u"Branch: **" + sickbeard.BRANCH + "**\n"
|
|
|
|
message += u"Commit: SiCKRAGETV/SickRage@" + sickbeard.CUR_COMMIT_HASH + "\n"
|
|
|
|
if pastebin_url:
|
|
|
|
message += u"Pastebin Log URL: " + pastebin_url + "\n"
|
|
|
|
message += u"### ERROR\n"
|
|
|
|
message += u"```\n"
|
2014-12-18 16:44:02 -05:00
|
|
|
message += curError.message + "\n"
|
2014-12-18 16:32:44 -05:00
|
|
|
message += u"```\n"
|
|
|
|
message += u"---\n"
|
|
|
|
message += u"_STAFF NOTIFIED_: @SiCKRAGETV/owners @SiCKRAGETV/moderators"
|
|
|
|
|
2014-12-20 08:49:35 -05:00
|
|
|
issue = self.gh_issues.create_issue("[APP SUBMITTED]: " + curError.title, message)
|
2014-12-18 16:32:44 -05:00
|
|
|
if issue:
|
2014-12-19 09:36:08 -05:00
|
|
|
self.log('Your issue ticket #%s was submitted successfully!' % issue.number)
|
|
|
|
|
|
|
|
if not sickbeard.GIT_AUTOISSUES:
|
|
|
|
ui.notifications.message('Your issue ticket #%s was submitted successfully!' % issue.number)
|
2014-12-20 08:49:35 -05:00
|
|
|
|
|
|
|
# clear error from error list
|
|
|
|
classes.ErrorViewer.errors.remove(curError)
|
|
|
|
except Exception as e:
|
|
|
|
self.log(sickbeard.exceptions.ex(e), ERROR)
|
2014-12-18 16:32:44 -05:00
|
|
|
|
|
|
|
|
2014-12-16 21:01:36 -05:00
|
|
|
class Wrapper(object):
|
|
|
|
instance = Logger()
|
|
|
|
|
|
|
|
def __init__(self, wrapped):
|
|
|
|
self.wrapped = wrapped
|
|
|
|
|
|
|
|
def __getattr__(self, name):
|
|
|
|
try:
|
|
|
|
return getattr(self.wrapped, name)
|
|
|
|
except AttributeError:
|
|
|
|
return getattr(self.instance, name)
|
|
|
|
|
|
|
|
_globals = sys.modules[__name__] = Wrapper(sys.modules[__name__])
|
|
|
|
|