from functools import wraps
import os
import datetime
import loguru


# 单例类的装饰器
def singleton_class_decorator(cls):
    """
    装饰器，单例类的装饰器
    """
    _instance = {}

    @wraps(cls)
    def wrapper_class(*args, **kwargs):
        if cls not in _instance:
            _instance[cls] = cls(*args, **kwargs)
        return _instance[cls]

    return wrapper_class


@singleton_class_decorator
class Logger:
    def __init__(self):
        self.logger_add()

    def get_project_path(self, project_path=None):
        if project_path is None:
            project_path = os.path.realpath('..')
        return project_path

    def get_log_path(self):
        project_path = self.get_project_path()
        project_log_dir = os.path.join(project_path, 'log')
        # 统一使用 runtime.log 作为日志文件名
        project_log_filename = 'runtime.log'  
        project_log_path = os.path.join(project_log_dir, project_log_filename)
        return project_log_path

    def logger_add(self):
        loguru.logger.add(
            sink=self.get_log_path(),
            rotation="50 MB",  # 每个日志文件最大10MB
            retention=5,  # 最多保留5个日志文件
            compression=None,
            encoding="utf-8",
            enqueue=True
        )

    @property
    def get_logger(self):
        return loguru.logger


# 实例化日志类
logger = Logger().get_logger


if __name__ == '__main__':
    logger.debug('调试代码')
    logger.info('输出信息')
    logger.success('输出成功')
    logger.warning('错误警告')
    logger.error('代码错误')
    logger.critical('崩溃输出')

    logger.info('----原始测试----')
