2010-08-16 12:52:39 -04:00
|
|
|
import logging
|
2010-12-05 16:56:33 -05:00
|
|
|
import re
|
2011-12-18 23:48:44 -08:00
|
|
|
import string
|
2012-07-13 14:56:12 -07:00
|
|
|
import time
|
2011-09-16 09:26:22 -07:00
|
|
|
from django.core.handlers.wsgi import WSGIRequest
|
2011-12-18 23:48:44 -08:00
|
|
|
from django.conf import settings
|
2012-09-06 17:16:01 -07:00
|
|
|
from utils.user_functions import extract_user_agent
|
2013-03-25 11:10:36 -07:00
|
|
|
from apps.statistics.rstats import RStats
|
2010-08-16 12:52:39 -04:00
|
|
|
|
2011-05-08 19:25:58 -04:00
|
|
|
class NullHandler(logging.Handler): #exists in python 3.1
|
|
|
|
def emit(self, record):
|
|
|
|
pass
|
|
|
|
|
2010-08-16 12:52:39 -04:00
|
|
|
def getlogger():
|
|
|
|
logger = logging.getLogger('newsblur')
|
|
|
|
return logger
|
|
|
|
|
2012-08-24 14:34:53 -07:00
|
|
|
def user(u, msg, request=None):
|
2013-01-08 18:33:30 -08:00
|
|
|
if not u:
|
|
|
|
return debug(msg)
|
|
|
|
|
2011-09-16 09:26:22 -07:00
|
|
|
platform = '------'
|
2012-07-13 14:56:12 -07:00
|
|
|
time_elapsed = ""
|
2012-08-24 14:34:53 -07:00
|
|
|
if isinstance(u, WSGIRequest) or request:
|
|
|
|
if not request:
|
|
|
|
request = u
|
|
|
|
u = request.user
|
2012-09-06 17:16:01 -07:00
|
|
|
platform = extract_user_agent(request)
|
2012-07-13 14:56:12 -07:00
|
|
|
|
|
|
|
if hasattr(request, 'start_time'):
|
2012-07-13 17:51:25 -07:00
|
|
|
seconds = time.time() - request.start_time
|
2012-09-19 23:12:26 -07:00
|
|
|
color = '~FK~SB'
|
2012-12-05 14:39:58 -08:00
|
|
|
if seconds >= 1:
|
2012-09-19 23:12:26 -07:00
|
|
|
color = '~FR'
|
2012-12-05 14:39:58 -08:00
|
|
|
elif seconds <= .2:
|
2012-09-19 23:12:26 -07:00
|
|
|
color = '~FB'
|
|
|
|
time_elapsed = "[%s%.4ss~SB] " % (
|
|
|
|
color,
|
2012-07-13 17:51:25 -07:00
|
|
|
seconds,
|
|
|
|
)
|
2012-09-06 17:16:01 -07:00
|
|
|
is_premium = u.is_authenticated() and u.profile.is_premium
|
|
|
|
premium = '*' if is_premium else ''
|
|
|
|
username = cipher(unicode(u)) if settings.CIPHER_USERNAMES else unicode(u)
|
2012-07-13 15:32:27 -07:00
|
|
|
info(' ---> [~FB~SN%-6s~SB] %s[%s%s] %s' % (platform, time_elapsed, username, premium, msg))
|
2013-03-18 15:27:14 -07:00
|
|
|
page_load_paths = [
|
|
|
|
"/reader/feed/",
|
|
|
|
"/social/stories/",
|
|
|
|
"/reader/river_stories/",
|
|
|
|
"/social/river_stories/"
|
|
|
|
]
|
2013-03-18 18:20:45 -07:00
|
|
|
if request:
|
2013-03-29 13:14:19 -07:00
|
|
|
path = RStats.clean_path(request.path)
|
2013-03-18 18:20:45 -07:00
|
|
|
if path in page_load_paths:
|
2013-03-25 11:10:36 -07:00
|
|
|
RStats.add('page_load', duration=seconds)
|
2011-12-18 23:48:44 -08:00
|
|
|
|
|
|
|
def cipher(msg):
|
|
|
|
shift = len(msg)
|
|
|
|
in_alphabet = unicode(string.ascii_lowercase)
|
|
|
|
out_alphabet = in_alphabet[shift:] + in_alphabet[:shift]
|
|
|
|
translation_table = dict((ord(ic), oc) for ic, oc in zip(in_alphabet, out_alphabet))
|
|
|
|
|
|
|
|
return msg.translate(translation_table)
|
2011-02-23 13:46:47 -05:00
|
|
|
|
2010-08-16 12:52:39 -04:00
|
|
|
def debug(msg):
|
|
|
|
logger = getlogger()
|
2010-12-05 16:56:33 -05:00
|
|
|
logger.debug(colorize(msg))
|
2010-08-16 12:52:39 -04:00
|
|
|
|
|
|
|
def info(msg):
|
|
|
|
logger = getlogger()
|
2010-12-05 16:56:33 -05:00
|
|
|
logger.info(colorize(msg))
|
2010-08-16 12:52:39 -04:00
|
|
|
|
|
|
|
def error(msg):
|
|
|
|
logger = getlogger()
|
2010-12-07 23:51:58 -05:00
|
|
|
logger.error(msg)
|
2010-12-05 16:56:33 -05:00
|
|
|
|
|
|
|
def colorize(msg):
|
|
|
|
params = {
|
2010-12-05 16:59:48 -05:00
|
|
|
r'\-\-\->' : '~FB~SB--->~FW',
|
|
|
|
r'\*\*\*>' : '~FB~SB~BB--->~BT~FW',
|
|
|
|
r'\[' : '~SB~FB[~SN~FM',
|
2010-12-05 17:01:14 -05:00
|
|
|
r'AnonymousUser' : '~FBAnonymousUser',
|
2011-02-23 13:46:47 -05:00
|
|
|
r'\*\]' : '~SN~FR*]',
|
2010-12-05 16:59:48 -05:00
|
|
|
r'\]' : '~FB~SB]~FW~SN',
|
2010-12-05 16:56:33 -05:00
|
|
|
}
|
|
|
|
colors = {
|
|
|
|
'~SB' : Style.BRIGHT,
|
|
|
|
'~SN' : Style.NORMAL,
|
|
|
|
'~SK' : Style.BLINK,
|
|
|
|
'~SU' : Style.UNDERLINE,
|
|
|
|
'~ST' : Style.RESET_ALL,
|
|
|
|
'~FK': Fore.BLACK,
|
|
|
|
'~FR': Fore.RED,
|
|
|
|
'~FG': Fore.GREEN,
|
|
|
|
'~FY': Fore.YELLOW,
|
|
|
|
'~FB': Fore.BLUE,
|
|
|
|
'~FM': Fore.MAGENTA,
|
|
|
|
'~FC': Fore.CYAN,
|
|
|
|
'~FW': Fore.WHITE,
|
|
|
|
'~FT': Fore.RESET,
|
|
|
|
'~BK': Back.BLACK,
|
|
|
|
'~BR': Back.RED,
|
|
|
|
'~BG': Back.GREEN,
|
|
|
|
'~BY': Back.YELLOW,
|
|
|
|
'~BB': Back.BLUE,
|
|
|
|
'~BM': Back.MAGENTA,
|
|
|
|
'~BC': Back.CYAN,
|
|
|
|
'~BW': Back.WHITE,
|
|
|
|
'~BT': Back.RESET,
|
|
|
|
}
|
|
|
|
for k, v in params.items():
|
|
|
|
msg = re.sub(k, v, msg)
|
|
|
|
msg = msg + '~ST~FW~BT'
|
2012-07-29 21:07:03 -07:00
|
|
|
# msg = re.sub(r'(~[A-Z]{2})', r'%(\1)s', msg)
|
|
|
|
for k, v in colors.items():
|
|
|
|
msg = msg.replace(k, v)
|
2011-07-27 09:33:34 -07:00
|
|
|
return msg
|
|
|
|
|
|
|
|
'''
|
|
|
|
This module generates ANSI character codes to printing colors to terminals.
|
|
|
|
See: http://en.wikipedia.org/wiki/ANSI_escape_code
|
|
|
|
'''
|
|
|
|
|
|
|
|
COLOR_ESC = '\033['
|
|
|
|
|
|
|
|
class AnsiCodes(object):
|
|
|
|
def __init__(self, codes):
|
|
|
|
for name in dir(codes):
|
|
|
|
if not name.startswith('_'):
|
|
|
|
value = getattr(codes, name)
|
|
|
|
setattr(self, name, COLOR_ESC + str(value) + 'm')
|
|
|
|
|
|
|
|
class AnsiFore:
|
|
|
|
BLACK = 30
|
|
|
|
RED = 31
|
|
|
|
GREEN = 32
|
|
|
|
YELLOW = 33
|
|
|
|
BLUE = 34
|
|
|
|
MAGENTA = 35
|
|
|
|
CYAN = 36
|
|
|
|
WHITE = 37
|
|
|
|
RESET = 39
|
|
|
|
|
|
|
|
class AnsiBack:
|
|
|
|
BLACK = 40
|
|
|
|
RED = 41
|
|
|
|
GREEN = 42
|
|
|
|
YELLOW = 43
|
|
|
|
BLUE = 44
|
|
|
|
MAGENTA = 45
|
|
|
|
CYAN = 46
|
|
|
|
WHITE = 47
|
|
|
|
RESET = 49
|
|
|
|
|
|
|
|
class AnsiStyle:
|
|
|
|
BRIGHT = 1
|
|
|
|
DIM = 2
|
|
|
|
UNDERLINE = 4
|
|
|
|
BLINK = 5
|
|
|
|
NORMAL = 22
|
|
|
|
RESET_ALL = 0
|
|
|
|
|
|
|
|
Fore = AnsiCodes(AnsiFore)
|
|
|
|
Back = AnsiCodes(AnsiBack)
|
|
|
|
Style = AnsiCodes(AnsiStyle)
|