ghostscript duplicate filter: filter within a window of previous messages

This commit is contained in:
James R. Barlow
2023-11-09 22:32:39 -08:00
parent 290aa28108
commit e7fa97731f
2 changed files with 32 additions and 8 deletions
+13 -6
View File
@@ -8,6 +8,7 @@ from __future__ import annotations
import logging
import os
import re
from collections import deque
from io import BytesIO
from os import fspath
from pathlib import Path
@@ -29,6 +30,9 @@ COLOR_CONVERSION_STRATEGIES = frozenset(
'UseDeviceIndependentColor',
]
)
# Ghostscript executable - gswin32c is not supported
GS = 'gswin64c' if os.name == 'nt' else 'gs'
log = logging.getLogger(__name__)
@@ -37,20 +41,23 @@ class DuplicateFilter(logging.Filter):
"""Filter out duplicate log messages."""
def __init__(self, logger: logging.Logger):
self.last: logging.LogRecord | None = None
self.count = 0
self.window: deque[str] = deque([], maxlen=5)
self.logger = logger
self.levelno = logging.DEBUG
self.count = 0
def filter(self, record):
if self.last and record.msg == self.last.msg:
if record.msg in self.window:
self.count += 1
self.levelno = record.levelno
return False
else:
if self.count >= 1:
rep_msg = f"(previous message repeated {self.count} times)"
rep_msg = f"(suppressed {self.count} repeated lines)"
self.count = 0 # Avoid infinite recursion
self.logger.log(self.last.levelno, rep_msg)
self.last = record
self.logger.log(self.levelno, rep_msg)
self.window.clear()
self.window.append(record.msg)
return True
+19 -2
View File
@@ -162,9 +162,9 @@ class TestDuplicateFilter:
assert len(caplog.records) == 5
assert caplog.records[0].msg == "test error message"
assert caplog.records[1].msg == "(previous message repeated 2 times)"
assert caplog.records[1].msg == "(suppressed 2 repeated lines)"
assert caplog.records[2].msg == "another error message"
assert caplog.records[3].msg == "(previous message repeated 1 times)"
assert caplog.records[3].msg == "(suppressed 1 repeated lines)"
assert caplog.records[4].msg == "yet another error message"
def test_filter_does_not_affect_unique_messages(
@@ -179,3 +179,20 @@ class TestDuplicateFilter:
assert caplog.records[0].msg == "test error message"
assert caplog.records[1].msg == "another error message"
assert caplog.records[2].msg == "yet another error message"
def test_filter_alt_messages(self, duplicate_filter_logger, caplog):
log = duplicate_filter_logger
log.error("test error message")
log.error("another error message")
log.error("test error message")
log.error("another error message")
log.error("test error message")
log.error("test error message")
log.error("another error message")
log.error("yet another error message")
assert len(caplog.records) == 4
assert caplog.records[0].msg == "test error message"
assert caplog.records[1].msg == "another error message"
assert caplog.records[2].msg == "(suppressed 5 repeated lines)"
assert caplog.records[3].msg == "yet another error message"