diff --git a/tests/testing_utils.py b/tests/testing_utils.py index ffddab7da..c00fef5fb 100644 --- a/tests/testing_utils.py +++ b/tests/testing_utils.py @@ -1,8 +1,6 @@ """Helpers for tests.""" -import contextlib import functools -import logging import sys from pathlib import Path @@ -12,7 +10,7 @@ from weasyprint.css.targets import TargetCollector from weasyprint.formatting_structure import boxes, build from weasyprint.html import HTML5_UA_STYLESHEET -from weasyprint.logger import LOGGER +from weasyprint.logger import capture_logs from weasyprint.text.fonts import FontConfiguration from weasyprint.urls import path2url @@ -73,35 +71,6 @@ def resource_path(name): TEST_UA_FONT_CONFIG = FontConfiguration() TEST_UA_STYLESHEET = CSS(resource_path('tests_ua.css'), font_config=TEST_UA_FONT_CONFIG) -class CallbackHandler(logging.Handler): - """A logging handler that calls a function for every message.""" - def __init__(self, callback): - logging.Handler.__init__(self) - self.emit = callback - - -@contextlib.contextmanager -def capture_logs(): - """Return a context manager that captures all logged messages.""" - logger = LOGGER - messages = [] - - def emit(record): - if record.name == 'weasyprint.progress': - return - messages.append(f'{record.levelname.upper()}: {record.getMessage()}') - - previous_handlers = logger.handlers - previous_level = logger.level - logger.handlers = [] - logger.addHandler(CallbackHandler(emit)) - logger.setLevel(logging.DEBUG) - try: - yield messages - finally: - logger.handlers = previous_handlers - logger.level = previous_level - def assert_no_logs(function): """Decorator that asserts that nothing is logged in a function.""" diff --git a/weasyprint/logger.py b/weasyprint/logger.py index 498d192cc..66a94edc0 100644 --- a/weasyprint/logger.py +++ b/weasyprint/logger.py @@ -13,6 +13,7 @@ """ +import contextlib import logging LOGGER = logging.getLogger('weasyprint') @@ -21,3 +22,35 @@ LOGGER.addHandler(logging.NullHandler()) PROGRESS_LOGGER = logging.getLogger('weasyprint.progress') + + +class CallbackHandler(logging.Handler): + """A logging handler that calls a function for every message.""" + def __init__(self, callback): + logging.Handler.__init__(self) + self.emit = callback + + +@contextlib.contextmanager +def capture_logs(logger='weasyprint', level=None): + """Return a context manager that captures all logged messages.""" + logger = logging.getLogger(logger) + messages = [] + + def emit(record): + if record.name == 'weasyprint.progress': + return + if level is not None and record.levelno < level: + return + messages.append(f'{record.levelname.upper()}: {record.getMessage()}') + + previous_handlers = logger.handlers + previous_level = logger.level + logger.handlers = [] + logger.addHandler(CallbackHandler(emit)) + logger.setLevel(logging.DEBUG) + try: + yield messages + finally: + logger.handlers = previous_handlers + logger.level = previous_level diff --git a/weasyprint/pdf/fonts.py b/weasyprint/pdf/fonts.py index 17c3549f3..12a8d3d81 100644 --- a/weasyprint/pdf/fonts.py +++ b/weasyprint/pdf/fonts.py @@ -2,6 +2,7 @@ import io from hashlib import md5 +from logging import WARNING from math import ceil import pydyf @@ -9,7 +10,7 @@ from fontTools.ttLib import TTFont, TTLibError, ttFont from fontTools.varLib.mutator import instantiateVariableFont -from ..logger import LOGGER +from ..logger import LOGGER, capture_logs from ..text.constants import PANGO_STRETCH_PERCENT from ..text.ffi import ffi, harfbuzz, harfbuzz_subset, pango, units_to_double from ..text.fonts import get_hb_object_data, get_pango_font_hb_face @@ -257,7 +258,12 @@ def _fonttools_subset(self, cmap, hinting): # Subset font. try: ttfont = TTFont(full_font, fontNumber=self.index) - subsetter.subset(ttfont) + with capture_logs('fontTools', level=WARNING) as logs: + subsetter.subset(ttfont) + for log in logs: + LOGGER.warning( + 'fontTools warning when subsetting "%s": %s', + self.family.decode(), log) except TTLibError: LOGGER.warning('Unable to subset font with fontTools') else: