#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
Stashr - Custom Log Handling
"""

"""
MIT License

Copyright (c) 2020 Andrew Vanderbye

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""

"""-------------------------------------------------------------------------------------------
-- IMPORTS
-------------------------------------------------------------------------------------------"""

""" --- HUEY IMPORT --- """
""" --- PYTHON IMPORTS --- """
import os, datetime, logging

""" --- STASHR DEPENDENCY IMPORTS --- """
""" --- STASHR CORE IMPORTS --- """
from stashr import paths

""" --- CONFIG IMPORTS --- """
from stashr.config import stashrconfig

"""-------------------------------------------------------------------------------------------
-- CUSTOM LOGGERS
-------------------------------------------------------------------------------------------"""

def stashr_logger(name):

    log_location = os.path.join(paths.base_path, 'log')
    log_file = os.path.join(log_location, 'log-{0}.txt'.format(datetime.date.today()))

    if not os.path.exists(log_location):
        os.mkdir(log_location)

    if not os.path.exists(log_file):
        file = open(log_file, 'w')
        file.close

    formatter = logging.Formatter(fmt='%(asctime)s - %(levelname)s - %(module)s - %(message)s')

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

    logger = logging.getLogger(name)
    logger.setLevel(stashrconfig['APP']['log_level'])
    logger.addHandler(handler)

    logging.basicConfig(filename=log_file, format='%(asctime)s - %(levelname)s - %(module)s - %(message)s')

    return logger

def disable_huey_logger():
    huey_logger = logging.getLogger("huey")
    huey_logger.setLevel(logging.CRITICAL)
    if huey_logger.handlers:
        huey_logger.handlers.pop()