You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

196 lines
5.3 KiB

11 years ago
import os
11 years ago
import re
11 years ago
import traceback
from couchpotato.api import addApiView
from couchpotato.core.helpers.encoding import toUnicode
11 years ago
from couchpotato.core.helpers.variable import tryInt, splitString
14 years ago
from couchpotato.core.logger import CPLog
from couchpotato.core.plugins.base import Plugin
from couchpotato.environment import Env
11 years ago
14 years ago
log = CPLog(__name__)
class Logging(Plugin):
def __init__(self):
13 years ago
addApiView('logging.get', self.get, docs = {
'desc': 'Get the full log file by number',
'params': {
'nr': {'desc': 'Number of the log to get.'}
},
'return': {'type': 'object', 'example': """{
'success': True,
11 years ago
'log': [{
'time': '03-12 09:12:59',
'type': 'INFO',
'message': 'Log message'
}, ..], //Log file
13 years ago
'total': int, //Total log files available
}"""}
})
addApiView('logging.partial', self.partial, docs = {
'desc': 'Get a partial log',
'params': {
'type': {'desc': 'Type of log', 'type': 'string: all(default), error, info, debug'},
'lines': {'desc': 'Number of lines. Last to first. Default 30'},
},
'return': {'type': 'object', 'example': """{
'success': True,
11 years ago
'log': [{
'time': '03-12 09:12:59',
'type': 'INFO',
'message': 'Log message'
}, ..]
}"""}
})
13 years ago
addApiView('logging.clear', self.clear, docs = {
'desc': 'Remove all the log files'
})
addApiView('logging.log', self.log, docs = {
'desc': 'Log errors',
13 years ago
'params': {
'type': {'desc': 'Type of logging, default "error"'},
11 years ago
'**kwargs': {'type': 'object', 'desc': 'All other params will be printed in the log string.'},
13 years ago
}
})
def get(self, nr = 0, **kwargs):
nr = tryInt(nr)
current_path = None
14 years ago
total = 1
for x in range(0, 50):
path = '%s%s' % (Env.get('log_path'), '.%s' % x if x > 0 else '')
14 years ago
# Check see if the log exists
if not os.path.isfile(path):
total = x - 1
14 years ago
break
# Set current path
if x is nr:
current_path = path
11 years ago
log_content = ''
if current_path:
f = open(current_path, 'r')
11 years ago
log_content = f.read()
11 years ago
logs = self.toList(log_content)
return {
'success': True,
11 years ago
'log': logs,
14 years ago
'total': total,
}
14 years ago
def partial(self, type = 'all', lines = 30, offset = 0, **kwargs):
total_lines = tryInt(lines)
offset = tryInt(offset)
log_lines = []
for x in range(0, 50):
path = '%s%s' % (Env.get('log_path'), '.%s' % x if x > 0 else '')
# Check see if the log exists
if not os.path.isfile(path):
break
f = open(path, 'r')
11 years ago
log_content = toUnicode(f.read())
raw_lines = self.toList(log_content)
raw_lines.reverse()
brk = False
11 years ago
for line in raw_lines:
11 years ago
if type == 'all' or line.get('type') == type.upper():
log_lines.append(line)
if len(log_lines) >= (total_lines + offset):
brk = True
break
if brk:
break
log_lines = log_lines[offset:]
log_lines.reverse()
return {
'success': True,
11 years ago
'log': log_lines,
}
11 years ago
def toList(self, log_content = ''):
10 years ago
logs_raw = re.split(r'\[0m\n', toUnicode(log_content))
11 years ago
logs = []
10 years ago
re_split = r'\x1b'
11 years ago
for log_line in logs_raw:
10 years ago
split = re.split(re_split, log_line)
10 years ago
if split and len(split) == 3:
11 years ago
try:
date, time, log_type = splitString(split[0], ' ')
timestamp = '%s %s' % (date, time)
except:
timestamp = 'UNKNOWN'
log_type = 'UNKNOWN'
message = ''.join(split[1]) if len(split) > 1 else split[0]
message = re.sub('\[\d+m\[', '[', message)
logs.append({
'time': timestamp,
'type': log_type,
'message': message
})
return logs
def clear(self, **kwargs):
14 years ago
for x in range(0, 50):
path = '%s%s' % (Env.get('log_path'), '.%s' % x if x > 0 else '')
if not os.path.isfile(path):
continue
14 years ago
try:
# Create empty file for current logging
if x is 0:
self.createFile(path, '')
else:
os.remove(path)
14 years ago
except:
log.error('Couldn\'t delete file "%s": %s', (path, traceback.format_exc()))
return {
14 years ago
'success': True
}
def log(self, type = 'error', **kwargs):
try:
log_message = 'API log: %s' % kwargs
try:
getattr(log, type)(log_message)
except:
log.error(log_message)
except:
log.error('Couldn\'t log via API: %s', kwargs)
return {
'success': True
}