Skip to content
Snippets Groups Projects
Commit c3a46c82 authored by Jonathan Weth's avatar Jonathan Weth :keyboard:
Browse files

Fine tuning of debug logs

parent 42125e09
No related branches found
No related tags found
1 merge request!86Merge school-apps
# Generated by Django 2.2.1 on 2019-05-23 14:27
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('debug', '0001_initial'),
]
operations = [
migrations.AddField(
model_name='debuglog',
name='log_text',
field=models.TextField(blank=True, verbose_name='Log-Text (falls nicht Datei)'),
),
migrations.AlterField(
model_name='debuglog',
name='filename',
field=models.FilePathField(blank=True, match='.*.log', path='/home/wethjo/dev/school-apps/schoolapps/latex',
verbose_name='Dateiname zur Logdatei (falls nicht Log-Text)'),
),
]
import os
import traceback
from django.db import models
from django.utils import timezone
......@@ -20,6 +21,9 @@ class DebugLogGroup(models.Model):
return self.name or self.id
def is_successful(self):
"""
:return: Were all operations in this group successful?
"""
successful = True
for log in self.logs.all():
if not log.is_successful():
......@@ -40,7 +44,8 @@ class DebugLog(models.Model):
# Data
return_code = models.IntegerField(blank=True, null=True, verbose_name="UNIX-Rückgabecode")
filename = models.FilePathField(path=DEBUG_LOG_DIR, match=".*.log",
verbose_name="Dateiname zur Logdatei")
verbose_name="Dateiname zur Logdatei (falls nicht Log-Text)", blank=True)
log_text = models.TextField(verbose_name="Log-Text (falls nicht Datei)", blank=True)
updated_at = models.DateTimeField(blank=False, default=timezone.now, verbose_name="Aktualisierungszeitpunkt")
class Meta:
......@@ -51,25 +56,46 @@ class DebugLog(models.Model):
return self.name or self.id
def get_file_content(self):
"""
:return: The log text (file or DB)
"""
if self.filename:
print(self.filename)
f = open(os.path.join(DEBUG_LOG_DIR, self.filename), "r")
content = f.read()
f.close()
return content
elif self.log_text:
return self.log_text
else:
return ""
def is_successful(self):
"""
:return: Was the last operation successful?
"""
return self.return_code == 0
def get_log_group_by_id(id):
"""
Get a log group from DB by given id
:param id: ID of log group
:return:
"""
p, _ = DebugLogGroup.objects.get_or_create(id=id)
return p
def register_log_with_filename(id, group_id, filename, return_code):
"""
Register a operation in debugging tool with a log file
:param id: id of operation
:param group_id: id of group
:param filename: file path (based on latex dir)
:param return_code: UNIX return code
"""
p, _ = DebugLog.objects.get_or_create(id=id)
group = get_log_group_by_id(group_id)
p.group = group
......@@ -77,3 +103,36 @@ def register_log_with_filename(id, group_id, filename, return_code):
p.filename = filename
p.updated_at = timezone.now()
p.save()
def register_return_0(id, group_id):
"""
Register a operation in debugging tool with an return code of 0 (success) and no log text/log file
:param id: id of operation
:param group_id: id of group
"""
p, _ = DebugLog.objects.get_or_create(id=id)
group = get_log_group_by_id(group_id)
p.group = group
p.return_code = 0
p.log_text = ""
p.updated_at = timezone.now()
p.save()
def register_traceback(id, group_id):
"""
Register a operation in debugging tool with an return code of 1 (error) and a log text
:param id: id of operation
:param group_id: id of group
"""
msg = traceback.format_exc()
p, _ = DebugLog.objects.get_or_create(id=id)
group = get_log_group_by_id(group_id)
p.group = group
p.return_code = 1
p.log_text = msg
p.updated_at = timezone.now()
p.save()
{% include 'partials/header.html' %}
{% load material_form %}
{% load martortags %}
{% load staticfiles %}
<link rel="stylesheet" href="{% static "css/highlight.min.css" %}">
<script src="{% static "js/highlight.min.js" %}"></script>
<script>hljs.initHighlightingOnLoad();</script>
<style>
.debug-li {
line-height: 2;
}
<link rel="stylesheet"
href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.15.6/styles/default.min.css">
<script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.15.6/highlight.min.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
.debug-icon {
font-size: 2.5rem;
}
</style>
<main>
<a class="btn-flat waves-effect waves-teal right btn-flat-medium" href="{% url "debug_logs" %}"><i
class="material-icons refresh center">refresh</i></a>
<h4>Debuggingtool</h4>
{% for group in groups %}
<a href="#{{ group.id }}" class="btn-flat waves-effect waves-katharineum"><i class="material-icons left">arrow_forward</i> {{ group.name }}
</a>
{% endfor %}
<h5>Schnellüberblick</h5>
<div class="row">
{% for group in groups %}
<div class="col s12 m4">
<div class="card">
<div class="card-content">
<i class="material-icons right {{ group.is_successful|yesno:"green,red" }}-text debug-icon">{{ group.is_successful|yesno:"check_circle,error" }}</i>
<span class="card-title">{{ group.name }}</span>
<ul>
{% for log in group.logs.all %}
<li class="debug-li">
<i class="material-icons right {{ log.is_successful|yesno:"green,red" }}-text">{{ log.is_successful|yesno:"check_circle,error" }}</i>
{{ log.name }}
</li>
{% endfor %}
</ul>
</div>
<div class="card-action">
<a href="#{{ group.id }}">
Log anzeigen
</a>
</div>
</div>
</div>
{% endfor %}
</div>
<h5>Logs</h5>
{% for group in groups %}
<div class="card" id="{{ group.id }}">
<div class="card-content">
......
......@@ -435,4 +435,12 @@ i.collapsible-trigger {
.waves-effect.waves-katharineum .waves-ripple {
background-color: rgba(218, 31, 61, 0.65);
}
.card .card-action a:not(.btn):not(.btn-large):not(.btn-small):not(.btn-large):not(.btn-floating) {
color: #da1f3d;
}
.card .card-action a:not(.btn):not(.btn-large):not(.btn-small):not(.btn-large):not(.btn-floating):hover {
color: #ea4661;
}
\ No newline at end of file
.hljs{display:block;overflow-x:auto;padding:.5em;background:#F0F0F0}.hljs,.hljs-subst{color:#444}.hljs-comment{color:#888888}.hljs-keyword,.hljs-attribute,.hljs-selector-tag,.hljs-meta-keyword,.hljs-doctag,.hljs-name{font-weight:bold}.hljs-type,.hljs-string,.hljs-number,.hljs-selector-id,.hljs-selector-class,.hljs-quote,.hljs-template-tag,.hljs-deletion{color:#880000}.hljs-title,.hljs-section{color:#880000;font-weight:bold}.hljs-regexp,.hljs-symbol,.hljs-variable,.hljs-template-variable,.hljs-link,.hljs-selector-attr,.hljs-selector-pseudo{color:#BC6060}.hljs-literal{color:#78A960}.hljs-built_in,.hljs-bullet,.hljs-code,.hljs-addition{color:#397300}.hljs-meta{color:#1f7199}.hljs-meta-string{color:#4d99bf}.hljs-emphasis{font-style:italic}.hljs-strong{font-weight:bold}
\ No newline at end of file
This diff is collapsed.
import os
import subprocess
from debug.models import register_log_with_filename
from schoolapps.settings import BASE_DIR
......@@ -21,9 +22,10 @@ def convert_markdown_2_latex(s):
os.path.join(BASE_DIR, "latex", "m2l.md"))
process = subprocess.Popen(bash_command.split(), stdout=subprocess.PIPE)
output = process.communicate()[0]
print("[MD TO LATEX]", output)
print("[RETURN CODE]", process.returncode)
# register_log_with_filename("m2l", "m2l", "m2l.log", process.returncode)
del output
# Register log file in debugging tool
register_log_with_filename("m2l", "m2l", "m2l.log", process.returncode)
# Read converted latex from file
tex_file = open(os.path.join(BASE_DIR, "latex", "m2l.tex"), "r", encoding="utf8")
......
......@@ -23,9 +23,10 @@ def generate_pdf(tex, filename):
filename))
process = subprocess.Popen(bash_command.split(), stdout=subprocess.PIPE)
output = process.communicate()[0]
del output
# Register log file in debugging tool
register_log_with_filename("latex_{}".format(filename), "latex", "{}.log".format(filename), process.returncode)
print("[LATEX]", output)
def generate_class_tex(subs, date, header_info, hints=None):
......
import datetime
import os
import traceback
from PyPDF2 import PdfFileMerger
from django.contrib.auth.decorators import login_required, permission_required
......@@ -7,6 +8,7 @@ from django.http import Http404, FileResponse
from django.shortcuts import render, redirect, get_object_or_404
from django.utils import timezone
from debug.models import register_traceback, register_return_0
from schoolapps.settings import SHORT_WEEK_DAYS, LONG_WEEK_DAYS
from timetable.filters import HintFilter
from timetable.forms import HintForm
......@@ -299,16 +301,23 @@ def sub_pdf(request):
generate_pdf(tex, "class{}".format(i))
# Merge PDFs
merger = PdfFileMerger()
class0 = open(os.path.join(BASE_DIR, "latex", "class0.pdf"), "rb")
class1 = open(os.path.join(BASE_DIR, "latex", "class1.pdf"), "rb")
merger.append(fileobj=class0)
merger.append(fileobj=class1)
# Write merged PDF to class.pdf
output = open(os.path.join(BASE_DIR, "latex", "class.pdf"), "wb")
merger.write(output)
output.close()
try:
merger = PdfFileMerger()
class0 = open(os.path.join(BASE_DIR, "latex", "class0.pdf"), "rb")
class1 = open(os.path.join(BASE_DIR, "latex", "class1.pdf"), "rb")
merger.append(fileobj=class0)
merger.append(fileobj=class1)
# Write merged PDF to class.pdf
output = open(os.path.join(BASE_DIR, "latex", "class.pdf"), "wb")
merger.write(output)
output.close()
# Register successful merge in debugging tool
register_return_0("merge_class", "pypdf2")
except Exception:
# Register exception in debugging tool
register_traceback("merge_class", "pypdf2")
# Read and response PDF
file = open(os.path.join(BASE_DIR, "latex", "class.pdf"), "rb")
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment