From c31eb624f5dc7acc07e5f244e8295881bce144bd Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Tue, 12 Feb 2019 18:39:32 -0500 Subject: [PATCH] Truncage large log files --- buildsrht/blueprints/jobs.py | 39 +++++++++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/buildsrht/blueprints/jobs.py b/buildsrht/blueprints/jobs.py index 6916479..fb5a938 100644 --- a/buildsrht/blueprints/jobs.py +++ b/buildsrht/blueprints/jobs.py @@ -215,8 +215,25 @@ def tag_svg(username, path): .filter(Job.tags.ilike(path + "%")) return svg_page(jobs) -def logify(text, task): - text = Markup('
') + escape(text) + Markup('
') +def logify(text, task, log_url): + #log_max = 16384 + log_max = 512 + if len(text) > log_max: + text = text[-log_max:] + try: + text = text[text.index('\n')+1:] + except ValueError: + pass + text = (Markup('
')
+                + Markup(''
+                    'This is a big file! Only the last 16KiB is shown. '
+                    f''
+                        'Click here to download the full log.'
+                    '\n\n')
+                + escape(text)
+                + Markup('
')) + else: + text = Markup('
') + escape(text) + Markup('
') nlines = text.encode().count(b'\n') linenos = Markup('
')
     for no in range(1, nlines + 1):
@@ -234,30 +251,34 @@ def job_by_id(username, job_id):
         abort(404)
     logs = list()
     try:
-        r = requests.get("http://{}/logs/{}/log".format(job.runner, job.id))
+        log_url = "http://{}/logs/{}/log".format(job.runner, job.id)
+        r = requests.get(log_url)
         if r.status_code == 200:
             logs.append({
                 "name": None,
-                "log": logify(r.text, "setup")
+                "log": logify(r.text, "setup", log_url)
             })
     except:
         pass
     for task in sorted(job.tasks, key=lambda t: t.id):
         if task.status == TaskStatus.pending:
             continue
+        log_url = "http://{}/logs/{}/{}/log".format(
+                job.runner, job.id, task.name)
         try:
-            r = requests.get("http://{}/logs/{}/{}/log".format(job.runner,
-                job.id, task.name))
+            r = requests.get(log_url)
         except:
             logs.append({
-                "name": "error",
-                "log": "Error fetching logs for this job"
+                "name": task.name,
+                "log": Markup('
'
+                    f'Error fetching logs for task "{escape(task.name)}"'
+                    '
') }) break if r.status_code == 200: logs.append({ "name": task.name, - "log": logify(r.text, "task-" + task.name) + "log": logify(r.text, "task-" + task.name, log_url) }) return render_template("job.html", job=job, -- 2.38.5