Fix log viewing

This commit is contained in:
Aaron Kimbre 2022-02-11 21:43:38 -06:00
parent 506d4ac090
commit f0cbcee100

View File

@ -1,4 +1,4 @@
from flask import render_template, Blueprint, redirect, request, send_from_directory, make_response, send_file
from flask import render_template, Blueprint, redirect, request, send_from_directory, make_response, send_file, current_app
from flask_user import login_required, current_user
import json, glob, os
from wand import image
@ -17,7 +17,6 @@ char_info_schema = CharacterInfoSchema()
def index():
"""Home/Index Page"""
if current_user.is_authenticated:
account_data = Account.query.filter(Account.id == current_user.id).first()
return render_template(
@ -46,32 +45,6 @@ def favicon():
@gm_level(9)
def logs():
with open('nexus_dashboard.log', 'r') as file:
logs = tail(file, 100)
logs = '</br>'.join(file.read().split('\n')[-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 '</br>'.join(all_read_text.splitlines()[-total_lines_wanted:])