/*
 * Copyright 2015-2016 Red Hat, Inc, and individual contributors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jboss.hal.client.logging;

import java.util.Date;
import java.util.logging.LogRecord;

import com.google.gwt.logging.impl.FormatterImpl;
import com.google.gwt.logging.impl.StackTracePrintStream;
import org.jboss.hal.ballroom.Format;

import static com.google.common.base.Strings.padEnd;
import static org.jboss.hal.core.Strings.abbreviate;
import static org.jboss.hal.core.Strings.abbreviateFqClassName;
import static org.jboss.hal.core.Strings.abbreviateMiddle;

class LogFormatter extends FormatterImpl {

    private static final int LEVEL_LENGTH = 4;
    private static final int LOGGER_LENGTH = 40;

    @Override
    public String format(LogRecord record) {
        StringBuilder builder = new StringBuilder();
        String timestamp = Format.timestamp(new Date(record.getMillis()));
        String level = abbreviateMiddle(padEnd(record.getLevel().getName(), LEVEL_LENGTH, ' '), LEVEL_LENGTH);
        String logger = abbreviate(padEnd(abbreviateFqClassName(record.getLoggerName()), LOGGER_LENGTH, ' '),
                LOGGER_LENGTH - 4, LOGGER_LENGTH);
        builder.append(timestamp)
                .append(" ")
                .append(level)
                .append(" ")
                .append(logger)
                .append(" ")
                .append(record.getMessage());
        if (record.getThrown() != null) {
            builder.append("\n");
            record.getThrown().printStackTrace(new StackTracePrintStream(builder));
        }
        return builder.toString();
    }
}

@@ -678,24 +678,56 @@
 .util.Date;%0A
+import java.util.logging.Level;%0A
 import java.
@@ -750,16 +750,16 @@
 Record;%0A
-
 %0Aimport 
@@ -951,16 +951,64 @@
 padEnd;%0A
+import static elemental2.dom.DomGlobal.console;%0A
 import s
@@ -1508,119 +1508,8 @@
 ));%0A
-        String level = abbreviateMiddle(padEnd(record.getLevel().getName(), LEVEL_LENGTH, ' '), LEVEL_LENGTH);%0A
     
@@ -1761,37 +1761,38 @@
 evel
-)%0A                .append(%22 %22
+(record.getLevel().intValue())
 )%0A  
@@ -2066,16 +2066,16 @@
       %7D%0A
-
         
@@ -2106,11 +2106,355 @@
 );%0A    %7D
+%0A%0A    public String level(int level) %7B%0A        if (level %3E= Level.SEVERE.intValue()) %7B%0A            return %22ERROR %22;%0A        %7D else if (level %3E= Level.WARNING.intValue()) %7B%0A            return %22WARN  %22;%0A        %7D else if (level %3E= Level.INFO.intValue()) %7B%0A            return %22INFO  %22;%0A        %7D else %7B%0A            return %22DEBUG %22;%0A        %7D%0A    %7D
 %0A%7D%0A

Tweak LogFormat