From 121c407cdbc9d8ad10f361bbee936a8f623207ec Mon Sep 17 00:00:00 2001 From: Aaron Kimbre Date: Fri, 11 Feb 2022 21:08:06 -0600 Subject: [PATCH] simple web log viewing for admins --- app/main.py | 35 +++++++++++++++++++++++++++++++++ app/templates/main/logs.html.j2 | 13 ++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 app/templates/main/logs.html.j2 diff --git a/app/main.py b/app/main.py index 0bdc5e1..5253184 100644 --- a/app/main.py +++ b/app/main.py @@ -6,6 +6,7 @@ from wand import image from app.models import Account, AccountInvitation, CharacterInfo from app.schemas import AccountSchema, CharacterInfoSchema from app.luclient import query_cdclient +from app import gm_level main_blueprint = Blueprint('main', __name__) @@ -40,3 +41,37 @@ def favicon(): 'favicon.ico', mimetype='image/vnd.microsoft.icon' ) + +@main_blueprint.route('/logs') +@gm_level(9) +def logs(): + with open('nexus_dashboard.log', 'r') as file: + logs = tail(file, 100) + return render_template('main/logs.html.j2', logs=logs) + +def tail( f, lines=20 ): + total_lines_wanted = lines + + BLOCK_SIZE = 1024 + f.seek(0, 2) + block_end_byte = f.tell() + lines_to_go = total_lines_wanted + block_number = -1 + blocks = [] # blocks of size BLOCK_SIZE, in reverse order starting + # from the end of the file + while lines_to_go > 0 and block_end_byte > 0: + if (block_end_byte - BLOCK_SIZE > 0): + # read the last block we haven't yet read + f.seek(block_number*BLOCK_SIZE, 2) + blocks.append(f.read(BLOCK_SIZE)) + else: + # file too small, start from begining + f.seek(0,0) + # only read what was not read + blocks.append(f.read(block_end_byte)) + lines_found = blocks[-1].count('\n') + lines_to_go -= lines_found + block_end_byte -= BLOCK_SIZE + block_number -= 1 + all_read_text = ''.join(reversed(blocks)) + return '
'.join(all_read_text.splitlines()[-total_lines_wanted:]) diff --git a/app/templates/main/logs.html.j2 b/app/templates/main/logs.html.j2 new file mode 100644 index 0000000..8880ccc --- /dev/null +++ b/app/templates/main/logs.html.j2 @@ -0,0 +1,13 @@ +{% extends 'base.html.j2' %} + +{% block title %}LOGS{% endblock %} + +{% block content_before %} + LOGS - {{ config.APP_NAME }} +{% endblock %} + +{% block content_override %} + + {{ logs }} + +{% endblock %}