#
# -*- coding: utf-8 -*-

import biocase.configuration
import logging, logging.handlers
import os.path as           _p
import sys, os
from datetime        import datetime

cfg = biocase.configuration.Cfg()
log = logging.getLogger()
log.setLevel(logging.DEBUG)


def getDebugFormatter():
    if sys.version_info[1]<5:
        f_debug   = logging.Formatter('%(levelname)-7s %(relativeCreated)-4d  [%(name)s] %(message)s')
    else:
        f_debug   = logging.Formatter('%(levelname)-7s %(relativeCreated)-4d  %(name)-24s ln%(lineno)d %(module)s.%(funcName)s(): %(message)s')
    return f_debug

def getErrorFormatter():
    if sys.version_info[1]<5:
        f_debug   = logging.Formatter('%(levelname)-7s %(asctime)s  [%(name)s] %(message)s')
    else:
        f_debug   = logging.Formatter('%(levelname)-7s %(asctime)s  %(name)-24s ln%(lineno)d %(module)s.%(funcName)s(): %(message)s')
    return f_debug



def initWebappLogging():
    # always define a Handler which writes ERRORS messages
    error_fh = logging.handlers.RotatingFileHandler(_p.join(cfg.logLocator, 'webapp_error.log'), maxBytes=cfg.logging.error_maxbytes, backupCount=cfg.logging.error_backupcount)
    error_fh.setLevel(logging.ERROR)
    error_fh.setFormatter(getErrorFormatter())
    log.addHandler(error_fh)
    # only use those handlers if debugging is turned on in server configs
    if cfg.server.debug:
        # create debug file handler for all entries 
        debug_fh = logging.handlers.RotatingFileHandler(filename=_p.join(cfg.logLocator, "webapp_debug.log"), maxBytes=cfg.logging.debug_maxbytes, backupCount=cfg.logging.debug_backupcount)
        debug_fh.setLevel(logging.DEBUG) # will be changed by PSF setting in pywrapper._setDatasource()
        log.addHandler(debug_fh)
        log.info("\n\n===== NEW WEBAPP REQUEST  %s ====="%str(datetime.now()))
        debug_fh.setFormatter(getDebugFormatter())


def initWrapperLogging(dsa):
    # SETUP LOGGERS
    # always define a Handler which writes ERRORS messages
    error_fh = logging.handlers.RotatingFileHandler(_p.join(cfg.logLocator, 'pywrapper_error.log'), maxBytes=cfg.logging.error_maxbytes, backupCount=cfg.logging.error_backupcount)
    error_fh.setLevel(logging.ERROR)
    error_fh.setFormatter(getErrorFormatter())
    log.addHandler(error_fh)
    # create REQUEST file handler
    log_request   = logging.getLogger('pywrapper.protocol.request')
    f_requests = logging.Formatter('[%(asctime)s] %(message)s', '%Y-%m-%d %H:%M:%S')
    f_requests.formatException = None # dont show tracebacks
    request_fh = logging.handlers.RotatingFileHandler(_p.join(cfg.logLocator, "pywrapper_request.log"), maxBytes=cfg.logging.request_maxbytes, backupCount=cfg.logging.request_backupcount)
    request_fh.setFormatter(f_requests)
    request_fh.setLevel(logging.INFO)
    log_request.addHandler(request_fh)
    # only use those handlers if DEBUGGING is turned on in server configs
    if cfg.server.debug:
        # create debug file handler for all entries
        debug_fh = logging.handlers.RotatingFileHandler(filename=_p.join(cfg.logLocator, "debug_%s.log"%dsa), maxBytes=cfg.logging.debug_maxbytes, backupCount=cfg.logging.debug_backupcount)
        #debug_fh = logging.FileHandler(filename=_p.join(cfg.logLocator, "debug_%s.log"%dsa), mode="w")
        debug_fh.setLevel(logging.DEBUG) # will be changed by PSF setting in pywrapper._setDatasource()
        log.addHandler(debug_fh)
        # set formatter after this init marker. looks better!
        log.info("\n\n===== NEW PYWRAPPER REQUEST  %s ====="%str(datetime.now()))
        debug_fh.setFormatter(getDebugFormatter())

