#!/usr/bin/env python
# -*- coding: utf8 - *-
"""Log utilities for tmuxp.

tmuxp.log
~~~~~~~~~
:copyright: Copyright 2013 Tony Narlock.
:license: BSD, see LICENSE for details

"""

from __future__ import absolute_import, division, print_function, with_statement
import logging
import time
from ._vendor.colorama import init
from ._vendor.colorama import Fore, Back, Style

LEVEL_COLORS = {
    'DEBUG': Fore.BLUE,  # Blue
    'INFO': Fore.GREEN,  # Green
    'WARNING': Fore.YELLOW,
    'ERROR': Fore.RED,
    'CRITICAL': Fore.RED
}


def default_log_template(self, record):
    """Return the prefix for the log message. Template for Formatter.

    :param: record: :py:class:`logging.LogRecord` object. this is passed in
    from inside the :py:meth:`logging.Formatter.format` record.

    """

    tpl = ''
    tpl += Style.RESET_ALL
    tpl += (
        LEVEL_COLORS.get(record.levelname) + Style.BRIGHT +
        '(%(levelname)s)'
        + Style.RESET_ALL + ' '
    )
    tpl += (
        '[' + Fore.BLACK + Style.DIM + Style.BRIGHT +
        '%(asctime)s'
        + Fore.RESET + Style.RESET_ALL + ']'
    )
    tpl += (
        ' ' + Fore.WHITE + Style.DIM + Style.BRIGHT +
        '%(name)s'
        + Fore.RESET + Style.RESET_ALL + ' '
    )
    tpl += Style.RESET_ALL

    return tpl


class LogFormatter(logging.Formatter):

    template = default_log_template

    def __init__(self, color=True, *args, **kwargs):
        logging.Formatter.__init__(self, *args, **kwargs)

    def format(self, record):
        try:
            record.message = record.getMessage()
        except Exception as e:
            record.message = "Bad message (%r): %r" % (e, record.__dict__)

        date_format = '%H:%m:%S'
        record.asctime = time.strftime(
            date_format, self.converter(record.created)
        )

        prefix = self.template(record) % record.__dict__

        formatted = prefix + " " + record.message
        return formatted.replace("\n", "\n    ")


def debug_log_template(self, record):
    """ Return the prefix for the log message. Template for Formatter.

    :param: record: :py:class:`logging.LogRecord` object. this is passed in
    from inside the :py:meth:`logging.Formatter.format` record.

    """

    tpl = ''
    tpl += Style.RESET_ALL
    tpl += (
        LEVEL_COLORS.get(record.levelname) + Style.BRIGHT +
        '(%(levelname)1.1s)'
        + Style.RESET_ALL + ' '
    )
    tpl += (
        '[' + Fore.BLACK + Style.DIM + Style.BRIGHT +
        '%(asctime)s'
        + Fore.RESET + Style.RESET_ALL + ']'
    )
    tpl += (
        ' ' + Fore.WHITE + Style.DIM + Style.BRIGHT +
        '%(name)s'
        + Fore.RESET + Style.RESET_ALL + ' '
    )
    tpl += (
        Fore.GREEN + Style.BRIGHT +
        '%(module)s.%(funcName)s()'
    )
    tpl += (
        Fore.BLACK + Style.DIM + Style.BRIGHT + ':' + Style.RESET_ALL +
        Fore.CYAN + '%(lineno)d'
    )
    tpl += Style.RESET_ALL

    return tpl


class DebugLogFormatter(LogFormatter):

    """Provides greater technical details than standard log Formatter."""

    template = debug_log_template

@@ -793,34 +793,22 @@
 %22%22%0A%0A    
-tpl = ''%0A    tpl +
+reset 
 = Style.
@@ -813,37 +813,42 @@
 e.RESET_ALL%0A    
-tpl +
+levelname 
 = (%0A        LEVE
@@ -955,37 +955,40 @@
 + ' '%0A    )%0A    
-tpl +
+asctime 
 = (%0A        '%5B' 
@@ -1098,37 +1098,37 @@
 + '%5D'%0A    )%0A    
-tpl +
+name 
 = (%0A        ' ' 
@@ -1235,32 +1235,33 @@
 + ' '%0A    )%0A
+%0A
     tpl 
 += Style.RES
@@ -1248,34 +1248,60 @@
     tpl 
-+
 = 
-Style.RESET_ALL
+reset + levelname + asctime + name + reset
 %0A%0A    re
@@ -2266,26 +2266,14 @@
     
-tpl = ''%0A    tpl +
+reset 
 = St
@@ -2286,29 +2286,34 @@
 SET_ALL%0A    
-tpl +
+levelname 
 = (%0A        
@@ -2427,37 +2427,40 @@
 + ' '%0A    )%0A    
-tpl +
+asctime 
 = (%0A        '%5B' 
@@ -2570,37 +2570,37 @@
 + '%5D'%0A    )%0A    
-tpl +
+name 
 = (%0A        ' ' 
@@ -2707,37 +2707,48 @@
 + ' '%0A    )%0A    
-tpl +
+module_funcName 
 = (%0A        Fore
@@ -2817,21 +2817,23 @@
   )%0A    
-tpl +
+lineno 
 = (%0A    
@@ -2939,24 +2939,25 @@
 '%0A    )%0A
+%0A
     tpl 
 += Style
@@ -2952,26 +2952,79 @@
 tpl 
-+
 = 
-Style.RESET_ALL
+reset + levelname + asctime + name + module_funcName + lineno + reset
 %0A%0A  

Update log string concatenation