import logging
import time
from os import path
from base_framework.base_config.current_pth import *
from concurrent_log_handler import ConcurrentRotatingFileHandler
import platform


time_flag = time.time()
d = path.dirname(__file__)
parent_path = os.path.dirname(d)
parent_path = os.path.dirname(parent_path)
# obj_tool = Tools()

if platform.system() == 'Darwin':
    LOG_FILENAME = log_path + '/' + 'run.log'
else:
# Windows路径
    LOG_FILENAME = log_path + '\\' + 'run.log'
if not os.path.exists(log_path):
    try:
        os.mkdir(log_path)
    except Exception as e:
        print('日志文件夹创建失败：{}'.format(e))

# MAC 路径
# LOG_FILENAME = log_path + '/' + 'run.log'
# LOG_FILENAME = (os.path.split(os.path.realpath(__file__)))[0] + '\\' + 'log\\run.log'


# Color escape string
COLOR_RED = '\033[1;31m'
COLOR_GREEN = '\033[1;32m'
COLOR_YELLOW = '\033[1;33m'
COLOR_BLUE = '\033[1;34m'
COLOR_PURPLE = '\033[1;35m'
COLOR_CYAN = '\033[1;36m'
COLOR_GRAY = '\033[1;37m'
COLOR_WHITE = '\033[1;38m'
COLOR_RESET = '\033[1;0m'

# Define log color
LOG_COLORS = {
    'DEBUG': COLOR_BLUE + '%s' + COLOR_RESET,
    'INFO': COLOR_GREEN + '%s' + COLOR_RESET,
    'WARNING': COLOR_YELLOW + '%s' + COLOR_RESET,
    'ERROR': COLOR_RED + '%s' + COLOR_RESET,
    'CRITICAL': COLOR_RED + '%s' + COLOR_RESET,
    'EXCEPTION': COLOR_RED + '%s' + COLOR_RESET,
}


class log_colour(logging.Formatter):

    def __init__(self, fmt=None, datefmt=None):
        logging.Formatter.__init__(self, fmt, datefmt)

    def format(self, record):
        level_name = record.levelname
        msg = logging.Formatter.format(self, record)
        return LOG_COLORS.get(level_name, '%s') % msg


def get_logger():
    logger = logging.Logger('Automation')
    logger.setLevel(0)
    terminal = logging.StreamHandler()
    terminal.setLevel(logging.DEBUG)
    # log_file = logging.handlers.TimedRotatingFileHandler(filename=LOG_FILENAME, when="S", backupCount=3,
    #                                              encoding='utf-8', interval=5)
    log_file = ConcurrentRotatingFileHandler(
        LOG_FILENAME, maxBytes=5 * 1024 * 1024, backupCount=4, encoding="utf-8")
    log_file.setLevel(logging.INFO)

    formatter_ter = log_colour(
        '%(asctime)s [tid:%(thread)d] %(filename)s[line:%(lineno)d] %(levelname)s %(message)s')
    formatter_log = logging.Formatter(
        '%(asctime)s [tid:%(thread)d pid:%(process)d] %(filename)s[line:%(lineno)d] %(levelname)s %(message)s')

    terminal.setFormatter(formatter_ter)
    log_file.setFormatter(formatter_log)

    logger.addHandler(terminal)
    logger.addHandler(log_file)

    return logger


def set_log_file(logfilename):
    global LOG_FILENAME
    LOG_FILENAME = logfilename
