Skip to content

Commit

Permalink
Simplify logging (#209)
Browse files Browse the repository at this point in the history
  • Loading branch information
WyattBlue authored Dec 30, 2022
1 parent fa20389 commit 499d562
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 42 deletions.
2 changes: 1 addition & 1 deletion ffmpeg_normalize/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from ._logger import setup_custom_logger
from ._version import __version__

logger = setup_custom_logger("ffmpeg_normalize")
logger = setup_custom_logger()


def create_parser() -> argparse.ArgumentParser:
Expand Down
2 changes: 1 addition & 1 deletion ffmpeg_normalize/_cmd_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from ._errors import FFmpegNormalizeError
from ._logger import setup_custom_logger

logger = setup_custom_logger("ffmpeg_normalize")
logger = setup_custom_logger()

NUL = "NUL" if system() in ("Windows", "cli") else "/dev/null"
DUR_REGEX = re.compile(
Expand Down
2 changes: 1 addition & 1 deletion ffmpeg_normalize/_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from ._logger import setup_custom_logger

logger = setup_custom_logger("ffmpeg_normalize")
logger = setup_custom_logger()


class FFmpegNormalizeError(Exception):
Expand Down
2 changes: 1 addition & 1 deletion ffmpeg_normalize/_ffmpeg_normalize.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
if TYPE_CHECKING:
from ._streams import LoudnessStatisticsWithMetadata

logger = setup_custom_logger("ffmpeg_normalize")
logger = setup_custom_logger()

NORMALIZATION_TYPES = ("ebu", "rms", "peak")
PCM_INCOMPATIBLE_FORMATS = {"flac", "mp3", "mp4", "ogg", "oga", "opus", "webm"}
Expand Down
55 changes: 19 additions & 36 deletions ffmpeg_normalize/_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@

from tqdm import tqdm

loggers: dict[str, logging.Logger] = {}
_global_log: logging.Logger | None = None


# https://stackoverflow.com/questions/38543506/
class TqdmLoggingHandler(logging.StreamHandler):
def __init__(self) -> None:
super().__init__(sys.stderr)

def emit(self, record) -> None:
def emit(self, record: logging.LogRecord) -> None:
try:
msg = self.format(record)
set_mp_lock()
Expand All @@ -37,21 +37,10 @@ def set_mp_lock() -> None:
pass


def setup_custom_logger(name: str) -> logging.Logger:
def setup_custom_logger() -> logging.Logger:
"""
Create a logger with a certain name and level
Grab or create the global logger
"""
global loggers

existing_logger = loggers.get(name)
if existing_logger is not None:
return existing_logger

formatter = logging.Formatter(fmt="%(levelname)s: %(message)s")

# handler = logging.StreamHandler()
handler = TqdmLoggingHandler()
handler.setFormatter(formatter)

# \033[1;30m - black
# \033[1;31m - red
Expand All @@ -62,29 +51,23 @@ def setup_custom_logger(name: str) -> logging.Logger:
# \033[1;36m - cyan
# \033[1;37m - white

if system() not in ["Windows", "cli"]:
logging.addLevelName(
logging.ERROR, f"{logging.getLevelName(logging.ERROR)}"
)
logging.addLevelName(
logging.WARNING,
f"{logging.getLevelName(logging.WARNING)}",
)
logging.addLevelName(
logging.INFO, f"{logging.getLevelName(logging.INFO)}"
)
logging.addLevelName(
logging.DEBUG, f"{logging.getLevelName(logging.DEBUG)}"
)

logger = logging.getLogger(name)
global _global_log
if _global_log is not None:
return _global_log

if system() not in ("Windows", "cli"):
logging.addLevelName(logging.ERROR, "ERROR")
logging.addLevelName(logging.WARNING, "WARNING")
logging.addLevelName(logging.INFO, "INFO")
logging.addLevelName(logging.DEBUG, "DEBUG")

logger = logging.Logger("ffmpeg_normalize")
logger.setLevel(logging.WARNING)

# if (logger.hasHandlers()):
# logger.handlers.clear()
if logger.handlers:
logger.handlers = []
handler = TqdmLoggingHandler()
handler.setFormatter(logging.Formatter(fmt="%(levelname)s: %(message)s"))
logger.addHandler(handler)
loggers.update(dict(name=logger))

_global_log = logger

return logger
2 changes: 1 addition & 1 deletion ffmpeg_normalize/_media_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
if TYPE_CHECKING:
from ffmpeg_normalize import FFmpegNormalize

logger = setup_custom_logger("ffmpeg_normalize")
logger = setup_custom_logger()

AUDIO_ONLY_FORMATS = {"aac", "ast", "flac", "mp3", "mka", "oga", "ogg", "opus", "wav"}
ONE_STREAM = {"aac", "ast", "flac", "mp3", "wav"}
Expand Down
2 changes: 1 addition & 1 deletion ffmpeg_normalize/_streams.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from ._ffmpeg_normalize import FFmpegNormalize
from ._media_file import MediaFile

logger = setup_custom_logger("ffmpeg_normalize")
logger = setup_custom_logger()

class EbuLoudnessStatistics(TypedDict):
input_i: float
Expand Down

0 comments on commit 499d562

Please sign in to comment.