# Path: paegan/logger/multi_process_logging.py
# class MultiProcessingLogHandler(logging.Handler):
#     def __init__(self, name, queue):
#         logging.Handler.__init__(self)
# 
#         self._handler = FileHandler(name)
#         self.queue = queue
# 
#         t = threading.Thread(target=self.receive)
#         t.daemon = True
#         t.start()
# 
#     def setFormatter(self, fmt):
#         logging.Handler.setFormatter(self, fmt)
#         self._handler.setFormatter(fmt)
# 
#     def receive(self):
#         while True:
#             try:
#                 record = self.queue.get()
#                 if record == StopIteration:
#                     break
#                 self._handler.emit(record)
#             except (KeyboardInterrupt, SystemExit):
#                 raise
#             except EOFError:
#                 break
#             except Exception:
#                 traceback.print_exc(file=sys.stderr)
#                 break
# 
#         return
# 
#     def send(self, s):
#         self.queue.put_nowait(s)
# 
#     def _format_record(self, record):
#         # ensure that exc_info and args
#         # have been stringified.  Removes any chance of
#         # unpickleable things inside and possibly reduces
#         # message size sent over the pipe
#         if record.args:
#             record.msg = record.msg % record.args
#             record.args = None
#         if record.exc_info:
#             dummy = self.format(record)
#             record.exc_info = None
# 
#         return record
# 
#     def emit(self, record):
#         try:
#             s = self._format_record(record)
#             self.send(s)
#         except (KeyboardInterrupt, SystemExit):
#             raise
#         except Exception:
#             self.handleError(record)
# 
#     def close(self):
#         self.queue.put_nowait(StopIteration)
#         self._handler.close()
#         logging.Handler.close(self)
# 
# Path: paegan/logger/progress_handler.py
# class ProgressHandler(logging.Handler):
#     def __init__(self, progress_deque):
#         logging.Handler.__init__(self)
#         self.addFilter(OnlyProgressFilter())
#         self._progress_deque = progress_deque
# 
#     def send(self, s):
#         self._progress_deque.append(s)
# 
#     def emit(self, record):
#         try:
#             s = self._format_record(record)
#             self.send(s)
#         except (KeyboardInterrupt, SystemExit):
#             raise
#         except Exception:
#             self.handleError(record)
# 
#     def _format_record(self, record):
#         # format log into tuple of:
#         # (datetime, progress, message)
# 
#         # Get a timezone aware datetime from time.time()
#         dt = datetime.fromtimestamp(record.created).replace(tzinfo=pytz.timezone(time.strftime("%Z",time.gmtime()))).astimezone(pytz.utc)
# 
#         msg = record.msg
# 
#         if isinstance(msg, list):
#             msg = tuple(msg)
# 
#         if isinstance(msg, tuple):
#             return tuple([dt]) + msg
#         elif isinstance(msg, str) or isinstance(msg, unicode):
#             return (dt, -1, unicode(msg))
#         elif isinstance(msg, float) or isinstance(msg, int):
#             return (dt, msg, None)
#         else:
#             return (dt, None, None)
# 
#     def close(self):
#         self._progress_deque.append(StopIteration)
#         logging.Handler.close(self)


import logging
import collections
import multiprocessing
import threading
from paegan.logger.multi_process_logging import MultiProcessingLogHandler
from paegan.logger.progress_handler import ProgressHandler


class EasyLogger(object):
    def __init__(self, logpath, level=None):

        if level is None:
            level = logging.PROGRESS

        self.logpath = logpath
        self.logger = multiprocessing.get_logger()

        self.queue = multiprocessing.Queue(-1)

        # Close any existing handlers
        self.close_handlers()

        # Remove any existing handlers
        self.logger.handlers = []
        self.logger.setLevel(level)


# Path: paegan/logger/multi_process_logging.py
# class MultiProcessingLogHandler(logging.Handler):
#     def __init__(self, name, queue):
#         logging.Handler.__init__(self)
# 
#         self._handler = FileHandler(name)
#         self.queue = queue
# 
#         t = threading.Thread(target=self.receive)
#         t.daemon = True
#         t.start()
# 
#     def setFormatter(self, fmt):
#         logging.Handler.setFormatter(self, fmt)
#         self._handler.setFormatter(fmt)
# 
#     def receive(self):
#         while True:
#             try:
#                 record = self.queue.get()
#                 if record == StopIteration:
#                     break
#                 self._handler.emit(record)
#             except (KeyboardInterrupt, SystemExit):
#                 raise
#             except EOFError:
#                 break
#             except Exception:
#                 traceback.print_exc(file=sys.stderr)
#                 break
# 
#         return
# 
#     def send(self, s):
#         self.queue.put_nowait(s)
# 
#     def _format_record(self, record):
#         # ensure that exc_info and args
#         # have been stringified.  Removes any chance of
#         # unpickleable things inside and possibly reduces
#         # message size sent over the pipe
#         if record.args:
#             record.msg = record.msg % record.args
#             record.args = None
#         if record.exc_info:
#             dummy = self.format(record)
#             record.exc_info = None
# 
#         return record
# 
#     def emit(self, record):
#         try:
#             s = self._format_record(record)
#             self.send(s)
#         except (KeyboardInterrupt, SystemExit):
#             raise
#         except Exception:
#             self.handleError(record)
# 
#     def close(self):
#         self.queue.put_nowait(StopIteration)
#         self._handler.close()
#         logging.Handler.close(self)
# 
# Path: paegan/logger/progress_handler.py
# class ProgressHandler(logging.Handler):
#     def __init__(self, progress_deque):
#         logging.Handler.__init__(self)
#         self.addFilter(OnlyProgressFilter())
#         self._progress_deque = progress_deque
# 
#     def send(self, s):
#         self._progress_deque.append(s)
# 
#     def emit(self, record):
#         try:
#             s = self._format_record(record)
#             self.send(s)
#         except (KeyboardInterrupt, SystemExit):
#             raise
#         except Exception:
#             self.handleError(record)
# 
#     def _format_record(self, record):
#         # format log into tuple of:
#         # (datetime, progress, message)
# 
#         # Get a timezone aware datetime from time.time()
#         dt = datetime.fromtimestamp(record.created).replace(tzinfo=pytz.timezone(time.strftime("%Z",time.gmtime()))).astimezone(pytz.utc)
# 
#         msg = record.msg
# 
#         if isinstance(msg, list):
#             msg = tuple(msg)
# 
#         if isinstance(msg, tuple):
#             return tuple([dt]) + msg
#         elif isinstance(msg, str) or isinstance(msg, unicode):
#             return (dt, -1, unicode(msg))
#         elif isinstance(msg, float) or isinstance(msg, int):
#             return (dt, msg, None)
#         else:
#             return (dt, None, None)
# 
#     def close(self):
#         self._progress_deque.append(StopIteration)
#         logging.Handler.close(self)


# Path: paegan/logger/easy_logger.py
import logging
import collections
import multiprocessing
import threading
from paegan.logger.multi_process_logging import MultiProcessingLogHandler
from paegan.logger.progress_handler import ProgressHandler



class EasyLogger(object):
    def __init__(self, logpath, level=None):

        if level is None:
            level = logging.PROGRESS

        self.logpath = logpath
        self.logger = multiprocessing.get_logger()

        self.queue = multiprocessing.Queue(-1)

        # Close any existing handlers
        self.close_handlers()

        # Remove any existing handlers
        self.logger.handlers = []
        self.logger.setLevel(level)


        handler = MultiProcessingLogHandler(logpath, self.queue)