mirror of
https://github.com/facebookresearch/pytorch3d.git
synced 2025-08-02 11:52:50 +08:00
Summary: Only import it if you ask for it. Reviewed By: kjchalup Differential Revision: D38327167 fbshipit-source-id: 3f05231f26eda582a63afc71b669996342b0c6f9
31 lines
882 B
Python
31 lines
882 B
Python
# Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
# All rights reserved.
|
|
#
|
|
# This source code is licensed under the BSD-style license found in the
|
|
# LICENSE file in the root directory of this source tree.
|
|
|
|
import contextlib
|
|
import logging
|
|
import re
|
|
|
|
|
|
@contextlib.contextmanager
|
|
def intercept_logs(logger_name: str, regexp: str):
|
|
# Intercept logs that match a regexp, from a given logger.
|
|
intercepted_messages = []
|
|
logger = logging.getLogger(logger_name)
|
|
|
|
class LoggerInterceptor(logging.Filter):
|
|
def filter(self, record):
|
|
message = record.getMessage()
|
|
if re.search(regexp, message):
|
|
intercepted_messages.append(message)
|
|
return True
|
|
|
|
interceptor = LoggerInterceptor()
|
|
logger.addFilter(interceptor)
|
|
try:
|
|
yield intercepted_messages
|
|
finally:
|
|
logger.removeFilter(interceptor)
|