#!/usr/bin/python
# -*- coding:utf-8 -*-
"""
"""
import os
import logging
import logging.handlers
import inspect


def deco_log(func):
    """ Decorator adds caller's information.

    """

    def _deco_log(self, msg):
        stack_frames = inspect.stack()
        stack_frame = stack_frames[1]
        prefix = '{0}:{1}, {2}()'.format(
            os.path.basename(stack_frame[1]),  # src file
            stack_frame[2],  # src line
            stack_frame[3]  # caller
        )
        decorated_msg = '{0}, {1}'.format(prefix, msg)
        func(self, decorated_msg)

    return _deco_log


class MyRotatingFileHandler(logging.handlers.RotatingFileHandler):
    def doRollover(self):
        """
        Do a rollover, as described in __init__().
        """
        if self.stream:
            self.stream.close()
            self.stream = None

        if self.backupCount > 0:
            for i in range(self.backupCount - 1, 0, -1):
                sfn = "%s.%d" % (self.baseFilename, i)
                dfn = "%s.%d" % (self.baseFilename, i + 1)
                try:
                    if os.path.exists(dfn):
                        os.remove(dfn)
                except Exception as e:
                    print('#1, pid={}, exception={}, remove({})'.format(os.getpid(), e, dfn))
                    raise e

                try:
                    if os.path.exists(sfn):
                        os.rename(sfn, dfn)
                except Exception as e:
                    print('#2, pid={}, exception={}, rename({}->{})'.format(os.getpid(), e, sfn, dfn))
                    raise e

            dfn = self.baseFilename + ".1"
            try:
                if os.path.exists(dfn):
                    os.remove(dfn)
            except Exception as e:
                print('#3, pid={}, exception={}, remove({})'.format(os.getpid(), e, dfn))
                raise e

            # Issue 18940: A file may not have been created if delay is True.
            try:
                if os.path.exists(self.baseFilename):
                    os.rename(self.baseFilename, dfn)
            except Exception as e:
                print('#4, pid={}, exception={}, rename({}->{})'.format(os.getpid(), e, self.baseFilename, dfn))
                raise e

        if not self.delay:
            self.stream = self._open()


class ACBMLogBase:
    """ logger base class

    """

    def __init__(self):
        # default logger
        self._logger = None

        # _levelNames 문자열 값을 변경한다. (4글자로 맞춤)
        # `ERROR` -> `ERR `
        # `DEBUG` -> `DEBG`
        # `WARNING` -> `WARN`
        logging._levelToName[logging.DEBUG] = 'DEBG'
        logging._levelToName[logging.ERROR] = 'ERR '
        logging._levelToName[logging.WARNING] = 'WARN'

    @deco_log
    def debug(self, msg):
        self._logger.debug(msg)

    @deco_log
    def error(self, msg):
        self._logger.error(msg)

    @deco_log
    def info(self, msg):
        self._logger.info(msg)

    @deco_log
    def warn(self, msg):
        self._logger.warning(msg)


if __name__ == "__main__":
    pass