Skip to content

Commit

Permalink
Fixes, and redirect warnings to Logger
Browse files Browse the repository at this point in the history
  • Loading branch information
Grub4K committed Jul 5, 2023
1 parent b20cac1 commit e889cb8
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 17 deletions.
12 changes: 7 additions & 5 deletions yt_dlp/YoutubeDL.py
Expand Up @@ -30,6 +30,7 @@
from .extractor import gen_extractor_classes, get_info_extractor
from .extractor.common import UnsupportedURLIE
from .extractor.openload import PhantomJSwrapper
from .output.helper import _wrap_download_retcode, redirect_warnings
from .output.logger import _Logger, _Styles
from .plugins import directories as plugin_directories
from .postprocessor import _PLUGIN_CLASSES as plugin_pps
Expand Down Expand Up @@ -589,7 +590,10 @@ def __init__(self, params=None, auto_init=True):
self._playlist_level = 0
self._playlist_urls = set()
self.cache = Cache(self)

self.logger = _Logger(self.params)
redirect_warnings(self.logger)
_wrap_download_retcode(self, self.logger)
self._out_files = self.logger._out_files # compat
self._allow_colors = self.logger._allow_colors # compat

Expand All @@ -613,7 +617,7 @@ def __init__(self, params=None, auto_init=True):
f'{self._format_err("DO NOT", self.Styles.ERROR)} open a bug report')

if self.params.get('bidi_workaround'):
from .. import _IN_CLI
from . import _IN_CLI

name = '--bidi-workaround' if _IN_CLI else 'bidi_workaround parameter'
try:
Expand Down Expand Up @@ -818,7 +822,7 @@ def add_postprocessor_hook(self, ph):
pp.add_progress_hook(ph)

def _write_string(self, message, out=None, only_once=False):
self.logger._log(out, message, once=only_once)
self.logger._log(out, message, once=only_once, newline=False)

def to_stdout(self, message):
"""Print message to stdout"""
Expand Down Expand Up @@ -879,9 +883,7 @@ def trouble(self, message=None, *, tb=None, is_error=True):
@param tb If given, is additional traceback information
@param is_error Whether to raise error according to ignorerrors
"""
result = self.logger.error(message, tb=tb, is_error=is_error, prefix=False)
if result is not None:
self._download_retcode = result
self.logger.error(message, tb=tb, is_error=is_error, prefix=False)

Styles = _Styles

Expand Down
40 changes: 40 additions & 0 deletions yt_dlp/output/helper.py
@@ -0,0 +1,40 @@
import functools
import inspect
import warnings


def redirect_warnings(logger, keep_filter=False):
"""Redirect all messages from the `warnings` module to a `Logger`"""
if not keep_filter:
warnings.simplefilter('always')

_old_showwarning = warnings.showwarning

@functools.wraps(warnings.showwarning)
def showwarning(message, category, filename, lineno, file=None, line=None):
if file is not None:
_old_showwarning(message, category, filename, lineno, file, line)
return

module = inspect.getmodule(None, filename)
if module:
filename = module.__name__
message = f'{category.__name__}({filename}:{lineno}): {message}'
if category is DeprecationWarning:
logger.deprecation_warning(message, stacklevel=1)
else:
logger.warning(message)

warnings.showwarning = showwarning


def _wrap_download_retcode(ydl, logger):
_old_error = logger.error

@functools.wraps(logger.error)
def error_wrapper(*args, **kwargs):
result = _old_error(*args, **kwargs)
if result is not None:
ydl._download_retcode = result

logger.error = error_wrapper
27 changes: 15 additions & 12 deletions yt_dlp/output/logger.py
Expand Up @@ -6,6 +6,7 @@

from ..minicurses import format_text
from ..utils import (
DownloadError,
Namespace,
Popen,
compat_os_name,
Expand All @@ -14,7 +15,6 @@
variadic,
windows_enable_vt_mode,
write_string,
DownloadError
)
from ..utils.traversal import traverse_obj

Expand Down Expand Up @@ -81,14 +81,14 @@ def process_color_policy(stream):
def _log(self, output, message, *, newline=True, once=False, prefix=None):
assert isinstance(message, str)

if prefix is not None:
message = ' '.join((*map(str, variadic(prefix)), message))

if once:
if message in self.message_cache:
return
self.message_cache.add(message)

if prefix is not None:
message = ' '.join((*map(str, variadic(prefix)), message))

if self._bidi_initialized:
message = self._apply_bidi_workaround(message)

Expand Down Expand Up @@ -146,7 +146,9 @@ def warning(self, message, once=False):
self._logger.warning(message)

elif not self._no_warnings:
self.stderr(f'{self._format_err("WARNING:", _Styles.WARNING)} {message}', once=once)
self._log(
self._out_files.error, message, once=once,
prefix=self._format_err("WARNING:", _Styles.WARNING))

def deprecation_warning(self, message, *, stacklevel=0):
deprecation_warning(
Expand All @@ -156,7 +158,9 @@ def deprecated_feature(self, message):
if self._logger:
self._logger.warning(f'Deprecated Feature: {message}')
return
self.stderr(f'{self._format_err("Deprecated Feature:", _Styles.ERROR)} {message}', once=True)
self._log(
self._out_files.error, message, once=True,
prefix=self._format_err("Deprecated Feature:", _Styles.ERROR))

def stderr(self, message, once=False):
if self._logger:
Expand All @@ -166,14 +170,13 @@ def stderr(self, message, once=False):
self._log(self._out_files.error, message, once=once)

def error(self, message, *, tb=None, is_error=True, prefix=True):
if prefix:
message = f'{self._format_err("ERROR:", _Styles.ERROR)} {message}'

if message is not None:
self.stderr(message)
self._log(
self._out_files.error, message,
prefix=self._format_err("ERROR:", _Styles.ERROR) if prefix else None)
if self._verbosity == 'verbose':
if tb is None:
if sys.exc_info()[0]: # if .trouble has been called from an except block
if sys.exc_info()[0]: # called from an except block
tb = ''
if hasattr(sys.exc_info()[1], 'exc_info') and sys.exc_info()[1].exc_info[0]:
tb += ''.join(traceback.format_exception(*sys.exc_info()[1].exc_info))
Expand All @@ -182,7 +185,7 @@ def error(self, message, *, tb=None, is_error=True, prefix=True):
tb_data = traceback.format_list(traceback.extract_stack())
tb = ''.join(tb_data)
if tb:
self.stderr(tb)
self._log(self._out_files.error, tb)
if not is_error:
return
if not self._ignoreerrors:
Expand Down

0 comments on commit e889cb8

Please sign in to comment.