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.config.config_file import ConfigFile
|
||||
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.logger import Logger
|
||||
|
||||
|
|
@ -22,17 +22,23 @@ logger = Logger.get()
|
|||
# Use ytdl-sub dl arguments to use the preset
|
||||
_VIEW_EXTRA_ARGS_FORMATTER = "--preset _view --overrides.url {}"
|
||||
|
||||
def _get_subscription_log_file_path(config: ConfigFile, subscription_name: str, success: bool) -> Path:
|
||||
assert config.config_options.persist_logs, "persist_logs should not be None"
|
||||
def _maybe_write_subscription_log_file(config: ConfigFile, subscription: Subscription, success: bool) -> 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_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_filename = f"{log_time}.{log_subscription_name}.{log_success}.log"
|
||||
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(
|
||||
|
|
@ -71,14 +77,21 @@ def _download_subscriptions_from_yaml_files(
|
|||
subscription.name,
|
||||
)
|
||||
logger.debug("Subscription full yaml:\n%s", subscription.as_yaml())
|
||||
transaction_log = subscription.download(dry_run=args.dry_run)
|
||||
|
||||
try:
|
||||
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))
|
||||
gc.collect() # Garbage collect after each subscription download
|
||||
|
||||
if config.config_options.persist_logs:
|
||||
FileHandler.copy(Logger.debug_log_filename(), )
|
||||
|
||||
_maybe_write_subscription_log_file(config=config, subscription=subscription, success=True)
|
||||
Logger.cleanup() # Cleanup logger after each successful subscription download
|
||||
|
||||
return output
|
||||
|
|
|
|||
|
|
@ -28,17 +28,10 @@ def main():
|
|||
try:
|
||||
_main()
|
||||
Logger.cleanup() # Ran successfully, so we can delete the debug file
|
||||
except ValidationException as validation_exception:
|
||||
logger.error(validation_exception)
|
||||
sys.exit(1)
|
||||
except Exception: # pylint: disable=broad-except
|
||||
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(),
|
||||
except Exception as exc: # pylint: disable=broad-except
|
||||
Logger.log_exit_exception(
|
||||
logger=logger,
|
||||
exception=exc,
|
||||
)
|
||||
sys.exit(1)
|
||||
|
||||
|
|
|
|||
|
|
@ -7,6 +7,8 @@ from dataclasses import dataclass
|
|||
from typing import List
|
||||
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
|
||||
|
||||
|
||||
|
|
@ -90,6 +92,9 @@ class Logger:
|
|||
_DEBUG_LOGGER_FILE = tempfile.NamedTemporaryFile(prefix="ytdl-sub.", delete=False)
|
||||
# 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
|
||||
_LOGGERS: List[logging.Logger] = []
|
||||
|
||||
|
|
@ -194,6 +199,25 @@ class Logger:
|
|||
with contextlib.redirect_stderr(new_target=redirect_stream):
|
||||
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
|
||||
def cleanup(cls, delete_debug_file: bool = True):
|
||||
"""
|
||||
|
|
@ -212,3 +236,4 @@ class Logger:
|
|||
|
||||
if delete_debug_file:
|
||||
FileHandler.delete(cls.debug_log_filename())
|
||||
cls._LOGGED_EXIT_EXCEPTION = False
|
||||
|
|
|
|||
Loading…
Reference in a new issue