File size: 5,945 Bytes
06ba7ea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import sys
import logging
import colorlog
import time
from pathlib import Path
from datetime import datetime
from typing import Optional, Union, Dict, Any, Callable
from logging.handlers import RotatingFileHandler
from functools import wraps, lru_cache
from contextlib import contextmanager
from proglog import ProgressBarLogger

@contextmanager
def silence_logging():
    logging.disable()
    try:
        yield
    finally:
        logging.disable(logging.NOTSET)

# Log format
LOG_FORMAT = "%(asctime)s - %(name)s - %(levelname)s - %(message)s (%(filename)s:%(lineno)d)"
# Log colors mapping
LOG_COLOR_MAP = {
    "DEBUG": "cyan",
    "INFO": "green",
    "WARNING": "yellow",
    "ERROR": "red",
    "CRITICAL": "bold_red",
}
# Log levels mapping
LOG_LEVEL_MAP = {
    "debug": logging.DEBUG,
    "info": logging.INFO,
    "warning": logging.WARNING,
    "error": logging.ERROR,
    "critical": logging.CRITICAL
}


@lru_cache(maxsize=128)
def get_logger(
    name: Optional[str] = None,
) -> logging.Logger:
    """Get a configured color logger

    Args:
        name: Logger name

    Returns:
        Logger instance
    """
    # Get calling module name
    if name is None:
        frame = sys._getframe(1)
        name = frame.f_globals.get("__name__", "__main__")

    # Logger config
    level = "debug"
    do_console = True
    do_file = False
    log_dir = "logs"
    date_format = "%Y-%m-%d %H:%M:%S"

    # Create logger
    logger = logging.getLogger(name)

    # Set logging level
    level = LOG_LEVEL_MAP.get(level.lower(), logging.INFO)
    logger.setLevel(level)
    logger.propagate = False  # Prevent propagation to root logger
    logger.handlers.clear()  # Clear existing handlers

    # Add console handler
    if do_console:
        console_handler = colorlog.StreamHandler()
        console_handler.setLevel(level)
        colored_formatter = colorlog.ColoredFormatter(
            f"%(log_color)s{LOG_FORMAT}",
            datefmt=date_format,
            log_colors=LOG_COLOR_MAP
        )
        console_handler.setFormatter(colored_formatter)
        logger.addHandler(console_handler)

    # Add file handler
    if do_file:
        # Create log directory
        log_path = Path(log_dir)
        log_path.mkdir(parents=True, exist_ok=True)

        # Create log filename
        module_name = name.split(".")[-1]
        timestamp = datetime.now().strftime("%Y-%m-%d")
        filename_template = "{timestamp}.log"
        log_file = log_path / filename_template.format(
            module=module_name,
            timestamp=timestamp
        )

        # Setup file handler
        file_handler = RotatingFileHandler(
            log_file,
            maxBytes=10 * 1024 * 1024,
            backupCount=5,
            encoding="utf-8"
        )
        file_handler.setLevel(level)
        file_formatter = logging.Formatter(LOG_FORMAT, datefmt=date_format)
        file_handler.setFormatter(file_formatter)
        logger.addHandler(file_handler)

    return logger


def log_exception(func=None, logger=None, level=logging.ERROR):
    def decorator(fn):
        nonlocal logger
        if logger is None:
            logger = get_logger(name=fn.__module__)

        @wraps(fn)
        def wrapper(*args, **kwargs):
            try:
                return fn(*args, **kwargs)
            except Exception as e:
                logger.log(level, f"Exception in {fn.__name__}: {str(e)}", exc_info=True)
                raise  # Re-raise exception
        return wrapper

    # Support direct @log_exception usage
    if func is not None:
        return decorator(func)
    return decorator


def log_time(func=None, logger=None, level=logging.DEBUG):
    def decorator(fn):
        nonlocal logger
        if logger is None:
            logger = get_logger(name=fn.__module__)

        @wraps(fn)
        def wrapper(*args, **kwargs):
            start_time = time.perf_counter()
            result = fn(*args, **kwargs)
            elapsed_time = time.perf_counter() - start_time
            logger.log(level, f"Function {fn.__name__} ellapsed: {elapsed_time:.3f}s")
            return result
        return wrapper

    if func is not None:
        return decorator(func)
    return decorator

from proglog import TqdmProgressBarLogger

class MCPMoviePyLogger(TqdmProgressBarLogger):
    def __init__(self, report: Callable[[float, Optional[float], Optional[str]], None]):
        super().__init__(logged_bars="all", leave_bars=False, print_messages=True)
        self._report = report
        self._last_ts = 0.0
        self._last_p = -1.0
        self._seen = set()

    def bars_callback(self, bar, attr, value, old_value=None):
        super().bars_callback(bar, attr, value, old_value)
        if bar not in ("frame_index", "t", "chunk"):
            return
        if attr != "index":
            return
        st = self.bars.get(bar) or {}
        idx, tot = st.get("index"), st.get("total")
        if idx is None or not tot:
            return
        p = float(idx) / float(tot)
        p = max(0.0, min(1.0, p))

        now = time.monotonic()
        if p < 1.0 and (now - self._last_ts) < 0.2 and (p - self._last_p) < 0.002:
            return
        self._last_ts, self._last_p = now, p

        self._report(float(idx), float(tot), f"rendering {p*100:.1f}%")

if __name__ == "__main__":

    # Create logger with configuration
    logger = get_logger()
    logger.debug("Debug message")
    logger.info("Info message")

    # Test logging decorators
    @log_exception
    @log_time
    def sample_function(x, y):
        import time
        time.sleep(0.1)
        return x + y

    # Test function logging
    result = sample_function(10, 20)
    logger.info(f"Function result: {result}")

    # Test exception logging
    @log_exception
    def dumb_func():
        return 1 / 0

    try:
        dumb_func()
    except ZeroDivisionError:
        logger.info("Exception was logged")