# Filename: logger.py
# pylint: disable=locally-disabled,C0103
"""
The logging facility.

"""

from itertools import cycle
import socket
import logging
import logging.config

from .tools import colored

__author__ = "Tamas Gal"
__copyright__ = "Copyright 2016, Tamas Gal and the KM3NeT collaboration."
__credits__ = []
__license__ = "MIT"
__maintainer__ = "Tamas Gal"
__email__ = "tgal@km3net.de"
__status__ = "Development"

PRINTER_SYMBOLS = cycle('-*&$#@%:~,<>=+&§')

try:
    logging.config.fileConfig('logging.conf')
except Exception:
    logging.basicConfig()

logging.addLevelName(logging.INFO, "\033[1;32m%s\033[1;0m" %
                     logging.getLevelName(logging.INFO))
logging.addLevelName(logging.DEBUG, "\033[1;34m%s\033[1;0m" %
                     logging.getLevelName(logging.DEBUG))
logging.addLevelName(logging.WARNING, "\033[1;33m%s\033[1;0m" %
                     logging.getLevelName(logging.WARNING))
logging.addLevelName(logging.ERROR, "\033[1;31m%s\033[1;0m" %
                     logging.getLevelName(logging.ERROR))
logging.addLevelName(logging.CRITICAL, "\033[1;101m%s\033[1;0m" %
                     logging.getLevelName(logging.CRITICAL))

ch = logging.StreamHandler()

# pylint: disable=C0103
formatter = logging.Formatter('[%(levelname)s] %(name)s: %(message)s')
# ch.setFormatter(formatter)
# logger.addHandler(ch)


class LogIO(object):
    """Read/write logging information.
    """

    def __init__(self, node, stream,
                 url='pi2089.physik.uni-erlangen.de',
                 port=28777):
        self.node = node
        self.stream = stream
        self.url = url
        self.port = port
        self.sock = None
        self.connect()

    def send(self, message, level='info'):
        message_string = "+log|{0}|{1}|{2}|{3}\r\n" \
                         .format(self.stream, self.node, level, message)
        try:
            self.sock.send(message_string)
        except socket.error:
            print("Lost connection, reconnecting...")
            self.connect()
            self.sock.send(message_string)

    def connect(self):
        self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.sock.connect((self.url, self.port))


def get(name):
    """Helper function to get a logger"""
    return logging.getLogger(name)


def set_level(name, level):
    """Set the log level for given logger"""
    logger = get(name)
    logger.setLevel(level)


def get_printer(name, color='cyan'):
    """Return a function which prints a message with a coloured name prefix"""
    symbol = next(PRINTER_SYMBOLS)

    def printer(text):
        print(colored('[' + symbol + '] ' + name, color) + ': ' + text)

    return printer

@@ -91,31 +91,28 @@
 %22%22%22%0A
-%0A
 from 
-itertools
+hashlib
  import 
 cycl
@@ -107,21 +107,22 @@
  import 
-cycle
+sha256
 %0Aimport 
@@ -420,53 +420,8 @@
 t%22%0A%0A
-PRINTER_SYMBOLS = cycle('-*&$#@%25:~,%3C%3E=+&%C2%A7')%0A%0A
 try:
@@ -2418,14 +2418,28 @@
 lor=
-'cyan'
+None, ansi_code=None
 ):%0A 
@@ -2524,126 +2524,237 @@
     
-symbol = next(PRINTER_SYMBOLS)%0A%0A    def printer(text):%0A        print(colored('%5B' + symbol
+if color is None and ansi_code is None:%0A        ansi_code = int(sha256(name.encode('utf-8')).hexdigest(), 16) %25 230%0A%0A    prefix = colored(name
  + '
-%5D ' + name, color) + ': '
+: ', color=color, ansi_code=ansi_code)%0A%0A    def printer(text):%0A        print(prefix
  + t

Create unique colours from name hash