diff --git a/couchpotato/core/helpers/request.py b/couchpotato/core/helpers/request.py index 72c9b9ed..e00880b0 100644 --- a/couchpotato/core/helpers/request.py +++ b/couchpotato/core/helpers/request.py @@ -59,7 +59,7 @@ def getParam(attr, default = None): try: return toUnicode(unquote_plus(getattr(flask.request, 'args').get(attr, default))).encode('utf-8') except: - return None + return default def padded_jsonify(callback, *args, **kwargs): content = str(callback) + '(' + json.dumps(dict(*args, **kwargs)) + ')' diff --git a/couchpotato/core/plugins/log/main.py b/couchpotato/core/plugins/log/main.py index 8da0c3d4..a29f1e12 100644 --- a/couchpotato/core/plugins/log/main.py +++ b/couchpotato/core/plugins/log/main.py @@ -21,6 +21,17 @@ class Logging(Plugin): 'success': True, 'log': string, //Log file '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, + 'log': string, //Log file }"""} }) addApiView('logging.clear', self.clear, docs = { @@ -64,6 +75,46 @@ class Logging(Plugin): 'total': total, }) + def partial(self): + + log_type = getParam('type', 'all') + total_lines = getParam('lines', 30) + + 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 + + reversed_lines = [] + f = open(path, 'r') + reversed_lines = f.read().split('[0m\n') + reversed_lines.reverse() + + brk = False + for line in reversed_lines: + + #print '%s ' % log_type in line.lower() + if log_type == 'all' or '%s ' % log_type.upper() in line: + log_lines.append(line) + + if len(log_lines) >= total_lines: + brk = True + break + + if brk: + break + + log_lines.reverse() + return jsonified({ + 'success': True, + 'log': '[0m\n'.join(log_lines), + }) + def clear(self): for x in range(0, 50):