import util from 'util';
import winston from 'winston';
import settings from '../config/settings';

// https://code.google.com/p/v8/wiki/JavaScriptStackTraceApi
const getStackTrace = () => {
    let obj = {};
    Error.captureStackTrace(obj, getStackTrace);
    return (obj.stack || '').split('\n');
};

const prefix = [];
const logger = new winston.Logger({
    exitOnError: false,
    level: settings.winston.level,
    transports: [
        new winston.transports.Console({
            colorize: true,
            timestamp: true,
            handleExceptions: true,
            json: false
        })
    ]
});

export default {
    logger,
    log: function(...args) {
        let level = args.shift();
        let stackTrace = getStackTrace()[2];
        logger.log(level, util.format.apply(util.format, prefix.concat(args).concat(stackTrace)));
    },
    raw: function(...args) {
        let level = args.shift();
        logger.log(level, prefix.concat(args).join(' '));
    },
    silly: function(...args) {
        let stackTrace = getStackTrace()[2];
        logger.silly(util.format.apply(util.format, prefix.concat(args).concat(stackTrace)));
    },
    debug: function(...args) {
        let stackTrace = getStackTrace()[2];
        logger.debug(util.format.apply(util.format, prefix.concat(args).concat(stackTrace)));
    },
    verbose: function(...args) {
        let stackTrace = getStackTrace()[2];
        logger.verbose(util.format.apply(util.format, prefix.concat(args).concat(stackTrace)));
    },
    info: function(...args) {
        let stackTrace = getStackTrace()[2];
        logger.info(util.format.apply(util.format, prefix.concat(args).concat(stackTrace)));
    },
    warn: function(...args) {
        let stackTrace = getStackTrace()[2];
        logger.warn(util.format.apply(util.format, prefix.concat(args).concat(stackTrace)));
    },
    error: function(...args) {
        let stackTrace = getStackTrace()[2];
        logger.error(util.format.apply(util.format, prefix.concat(args).concat(stackTrace)));
    }
};

@@ -188,18 +188,20 @@
 %3E %7B%0A    
-le
+cons
 t obj = 
@@ -303,27 +303,8 @@
 %7D;%0A%0A
-const prefix = %5B%5D;%0A
 cons
@@ -650,34 +650,36 @@
 args) %7B%0A        
-le
+cons
 t level = args.s
@@ -686,34 +686,36 @@
 hift();%0A        
-le
+cons
 t stackTrace = g
@@ -790,35 +790,20 @@
 format, 
-prefix.concat(
 args
-)
 .concat(
@@ -861,18 +861,20 @@
         
-le
+cons
 t level 
@@ -915,35 +915,20 @@
 (level, 
-prefix.concat(
 args
-)
 .join(' 
@@ -978,33 +978,105 @@
         
-let stackTrace = 
+if (logger.levels%5Blogger.level%5D %3E= logger.levels.debug) %7B%0A            args = args.concat(
 getStack
@@ -1077,33 +1077,44 @@
 tStackTrace()%5B2%5D
-;
+);%0A        %7D
 %0A        logger.
@@ -1134,133 +1134,155 @@
 rmat
-.apply(util.format, prefix.concat(args).concat(stackTrace)));%0A    %7D,%0A    debug: function(...args) %7B%0A        let stackTrace = 
+(...args));%0A    %7D,%0A    debug: function(...args) %7B%0A        if (logger.levels%5Blogger.level%5D %3E= logger.levels.debug) %7B%0A            args = args.concat(
 getS
@@ -1287,33 +1287,44 @@
 tStackTrace()%5B2%5D
-;
+);%0A        %7D
 %0A        logger.
@@ -1344,135 +1344,157 @@
 rmat
-.apply(util.format, prefix.concat(args).concat(stackTrace)));%0A    %7D,%0A    verbose: function(...args) %7B%0A        let stackTrace = 
+(...args));%0A    %7D,%0A    verbose: function(...args) %7B%0A        if (logger.levels%5Blogger.level%5D %3E= logger.levels.debug) %7B%0A            args = args.concat(
 getS
@@ -1499,34 +1499,45 @@
 tStackTrace()%5B2%5D
+)
 ;%0A
+        %7D%0A
         logger.v
@@ -1558,132 +1558,154 @@
 rmat
-.apply(util.format, prefix.concat(args).concat(stackTrace)));%0A    %7D,%0A    info: function(...args) %7B%0A        let stackTrace = 
+(...args));%0A    %7D,%0A    info: function(...args) %7B%0A        if (logger.levels%5Blogger.level%5D %3E= logger.levels.debug) %7B%0A            args = args.concat(
 getS
@@ -1710,34 +1710,45 @@
 tStackTrace()%5B2%5D
+)
 ;%0A
+        %7D%0A
         logger.i
@@ -1766,132 +1766,154 @@
 rmat
-.apply(util.format, prefix.concat(args).concat(stackTrace)));%0A    %7D,%0A    warn: function(...args) %7B%0A        let stackTrace = 
+(...args));%0A    %7D,%0A    warn: function(...args) %7B%0A        if (logger.levels%5Blogger.level%5D %3E= logger.levels.debug) %7B%0A            args = args.concat(
 getS
@@ -1918,34 +1918,45 @@
 tStackTrace()%5B2%5D
+)
 ;%0A
+        %7D%0A
         logger.w
@@ -1974,133 +1974,155 @@
 rmat
-.apply(util.format, prefix.concat(args).concat(stackTrace)));%0A    %7D,%0A    error: function(...args) %7B%0A        let stackTrace = 
+(...args));%0A    %7D,%0A    error: function(...args) %7B%0A        if (logger.levels%5Blogger.level%5D %3E= logger.levels.debug) %7B%0A            args = args.concat(
 getS
@@ -2127,34 +2127,45 @@
 tStackTrace()%5B2%5D
+)
 ;%0A
+        %7D%0A
         logger.e
@@ -2184,66 +2184,16 @@
 rmat
-.apply(util.format, prefix.concat(args).concat(stackTrace)
+(...args
 ));%0A

Refactor log library