mmmmm
This commit is contained in:
parent
476a780834
commit
5c2050ee29
3 changed files with 51 additions and 20 deletions
|
|
@ -12,7 +12,7 @@ from ytdl_sub.cli.download_args_parser import DownloadArgsParser
|
||||||
from ytdl_sub.cli.main_args_parser import parser
|
from ytdl_sub.cli.main_args_parser import parser
|
||||||
from ytdl_sub.config.config_file import ConfigFile
|
from ytdl_sub.config.config_file import ConfigFile
|
||||||
from ytdl_sub.subscriptions.subscription import Subscription
|
from ytdl_sub.subscriptions.subscription import Subscription
|
||||||
from ytdl_sub.utils.file_handler import FileHandlerTransactionLog
|
from ytdl_sub.utils.file_handler import FileHandlerTransactionLog, FileHandler
|
||||||
from ytdl_sub.utils.file_lock import working_directory_lock
|
from ytdl_sub.utils.file_lock import working_directory_lock
|
||||||
from ytdl_sub.utils.logger import Logger
|
from ytdl_sub.utils.logger import Logger
|
||||||
|
|
||||||
|
|
@ -22,17 +22,23 @@ logger = Logger.get()
|
||||||
# Use ytdl-sub dl arguments to use the preset
|
# Use ytdl-sub dl arguments to use the preset
|
||||||
_VIEW_EXTRA_ARGS_FORMATTER = "--preset _view --overrides.url {}"
|
_VIEW_EXTRA_ARGS_FORMATTER = "--preset _view --overrides.url {}"
|
||||||
|
|
||||||
def _get_subscription_log_file_path(config: ConfigFile, subscription_name: str, success: bool) -> Path:
|
def _maybe_write_subscription_log_file(config: ConfigFile, subscription: Subscription, success: bool) -> None:
|
||||||
assert config.config_options.persist_logs, "persist_logs should not be None"
|
# If persisting logs is disabled, do nothing
|
||||||
|
if not config.config_options.persist_logs:
|
||||||
|
return
|
||||||
|
|
||||||
|
# If persisting successful logs is disabled, do nothing
|
||||||
|
if success and not config.config_options.persist_logs.keep_successful_logs:
|
||||||
|
return
|
||||||
|
|
||||||
log_time = datetime.now().strftime("%Y-%m-%d-%H%M%S")
|
log_time = datetime.now().strftime("%Y-%m-%d-%H%M%S")
|
||||||
log_subscription_name = sanitize_filename(subscription_name).lower().replace(" ", "_")
|
log_subscription_name = sanitize_filename(subscription.name).lower().replace(" ", "_")
|
||||||
log_success = "success" if success else "error"
|
log_success = "success" if success else "error"
|
||||||
|
|
||||||
log_filename = f"{log_time}.{log_subscription_name}.{log_success}.log"
|
log_filename = f"{log_time}.{log_subscription_name}.{log_success}.log"
|
||||||
persist_log_path = Path(config.config_options.persist_logs.logs_directory) / log_filename
|
persist_log_path = Path(config.config_options.persist_logs.logs_directory) / log_filename
|
||||||
|
|
||||||
return persist_log_path
|
FileHandler.copy(Logger.debug_log_filename(), persist_log_path)
|
||||||
|
|
||||||
|
|
||||||
def _download_subscriptions_from_yaml_files(
|
def _download_subscriptions_from_yaml_files(
|
||||||
|
|
@ -71,14 +77,21 @@ def _download_subscriptions_from_yaml_files(
|
||||||
subscription.name,
|
subscription.name,
|
||||||
)
|
)
|
||||||
logger.debug("Subscription full yaml:\n%s", subscription.as_yaml())
|
logger.debug("Subscription full yaml:\n%s", subscription.as_yaml())
|
||||||
|
|
||||||
|
try:
|
||||||
transaction_log = subscription.download(dry_run=args.dry_run)
|
transaction_log = subscription.download(dry_run=args.dry_run)
|
||||||
|
except Exception as exc:
|
||||||
|
Logger.log_exit_exception(logger=logger, exception=exc)
|
||||||
|
_maybe_write_subscription_log_file(
|
||||||
|
config=config, subscription=subscription,
|
||||||
|
success=False
|
||||||
|
)
|
||||||
|
raise
|
||||||
|
|
||||||
output.append((subscription, transaction_log))
|
output.append((subscription, transaction_log))
|
||||||
gc.collect() # Garbage collect after each subscription download
|
gc.collect() # Garbage collect after each subscription download
|
||||||
|
|
||||||
if config.config_options.persist_logs:
|
_maybe_write_subscription_log_file(config=config, subscription=subscription, success=True)
|
||||||
FileHandler.copy(Logger.debug_log_filename(), )
|
|
||||||
|
|
||||||
Logger.cleanup() # Cleanup logger after each successful subscription download
|
Logger.cleanup() # Cleanup logger after each successful subscription download
|
||||||
|
|
||||||
return output
|
return output
|
||||||
|
|
|
||||||
|
|
@ -28,17 +28,10 @@ def main():
|
||||||
try:
|
try:
|
||||||
_main()
|
_main()
|
||||||
Logger.cleanup() # Ran successfully, so we can delete the debug file
|
Logger.cleanup() # Ran successfully, so we can delete the debug file
|
||||||
except ValidationException as validation_exception:
|
except Exception as exc: # pylint: disable=broad-except
|
||||||
logger.error(validation_exception)
|
Logger.log_exit_exception(
|
||||||
sys.exit(1)
|
logger=logger,
|
||||||
except Exception: # pylint: disable=broad-except
|
exception=exc,
|
||||||
logger.exception("An uncaught error occurred:")
|
|
||||||
logger.error(
|
|
||||||
"Version %s\nPlease upload the error log file '%s' and make a Github "
|
|
||||||
"issue at https://github.com/jmbannon/ytdl-sub/issues with your config and "
|
|
||||||
"command/subscription yaml file to reproduce. Thanks for trying ytdl-sub!",
|
|
||||||
__local_version__,
|
|
||||||
Logger.debug_log_filename(),
|
|
||||||
)
|
)
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,8 @@ from dataclasses import dataclass
|
||||||
from typing import List
|
from typing import List
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
|
||||||
|
from ytdl_sub import __local_version__
|
||||||
|
from ytdl_sub.utils.exceptions import ValidationException
|
||||||
from ytdl_sub.utils.file_handler import FileHandler
|
from ytdl_sub.utils.file_handler import FileHandler
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -90,6 +92,9 @@ class Logger:
|
||||||
_DEBUG_LOGGER_FILE = tempfile.NamedTemporaryFile(prefix="ytdl-sub.", delete=False)
|
_DEBUG_LOGGER_FILE = tempfile.NamedTemporaryFile(prefix="ytdl-sub.", delete=False)
|
||||||
# pylint: enable=R1732
|
# pylint: enable=R1732
|
||||||
|
|
||||||
|
# Whether the final exception lines were added to the debug log
|
||||||
|
_LOGGED_EXIT_EXCEPTION: bool = False
|
||||||
|
|
||||||
# Keep track of all Loggers created
|
# Keep track of all Loggers created
|
||||||
_LOGGERS: List[logging.Logger] = []
|
_LOGGERS: List[logging.Logger] = []
|
||||||
|
|
||||||
|
|
@ -194,6 +199,25 @@ class Logger:
|
||||||
with contextlib.redirect_stderr(new_target=redirect_stream):
|
with contextlib.redirect_stderr(new_target=redirect_stream):
|
||||||
yield
|
yield
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def log_exit_exception(cls, logger: logging.Logger, exception: Exception, filename: Optional[str] = None):
|
||||||
|
if not cls._LOGGED_EXIT_EXCEPTION:
|
||||||
|
# Log validation exceptions as-is
|
||||||
|
if isinstance(exception, ValidationException):
|
||||||
|
logger.error(exception)
|
||||||
|
# For other uncaught errors, log as bug:
|
||||||
|
else:
|
||||||
|
logger.exception("An uncaught error occurred:")
|
||||||
|
logger.error(
|
||||||
|
"Version %s\nPlease upload the error log file '%s' and make a Github "
|
||||||
|
"issue at https://github.com/jmbannon/ytdl-sub/issues with your config and "
|
||||||
|
"command/subscription yaml file to reproduce. Thanks for trying ytdl-sub!",
|
||||||
|
__local_version__,
|
||||||
|
filename if filename else Logger.debug_log_filename(),
|
||||||
|
)
|
||||||
|
|
||||||
|
cls._LOGGED_EXIT_EXCEPTION = True
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def cleanup(cls, delete_debug_file: bool = True):
|
def cleanup(cls, delete_debug_file: bool = True):
|
||||||
"""
|
"""
|
||||||
|
|
@ -212,3 +236,4 @@ class Logger:
|
||||||
|
|
||||||
if delete_debug_file:
|
if delete_debug_file:
|
||||||
FileHandler.delete(cls.debug_log_filename())
|
FileHandler.delete(cls.debug_log_filename())
|
||||||
|
cls._LOGGED_EXIT_EXCEPTION = False
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue