/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.apache.ignite.testframework;

import org.apache.ignite.IgniteLogger;
import org.apache.ignite.internal.util.typedef.internal.U;
import org.jetbrains.annotations.Nullable;

/**
 * Logger which logs to string buffer.
 */
public class GridStringLogger implements IgniteLogger {
    /** */
    private static final int MAX = 1024 * 11;

    /** */
    private static final int CHAR_CNT = 1024 * 10;

    /** */
    private StringBuilder buf = new StringBuilder(MAX);

    /** */
    private final boolean dbg;

    /** */
    private final IgniteLogger echo;

    /**
     *
     */
    public GridStringLogger() {
        this(false);
    }

    /**
     * @param dbg Debug flag.
     */
    public GridStringLogger(boolean dbg) {
        this(dbg, null);
    }

    /**
     * @param dbg Debug flag.
     * @param echo Logger to echo all messages.
     */
    public GridStringLogger(boolean dbg, @Nullable IgniteLogger echo) {
        this.dbg = dbg;
        this.echo = echo;
    }

    /**
     * @param msg Message to log.
     */
    private void log(String msg) {
        buf.append(msg).append(U.nl());

        if (echo != null)
            echo.info("[GridStringLogger echo] " + msg);

        if (buf.length() > CHAR_CNT) {
            if (echo != null)
                echo.warning("Cleaning GridStringLogger history.");

            buf.delete(0, buf.length() - CHAR_CNT);
        }

        assert buf.length() <= CHAR_CNT;
    }

    /** {@inheritDoc} */
    @Override public IgniteLogger getLogger(Object ctgr) {
        return this;
    }

    /** {@inheritDoc} */
    @Override public void trace(String msg) {
        log(msg);
    }

    /** {@inheritDoc} */
    @Override public void debug(String msg) {
        log(msg);
    }

    /** {@inheritDoc} */
    @Override public void info(String msg) {
        log(msg);
    }

    /** {@inheritDoc} */
    @Override public void warning(String msg) {
        log(msg);
    }

    /** {@inheritDoc} */
    @Override public void warning(String msg, @Nullable Throwable e) {
        log(msg);

        if (e != null)
            log(e.toString());
    }

    /** {@inheritDoc} */
    @Override public void error(String msg) {
        log(msg);
    }

    /** {@inheritDoc} */
    @Override public void error(String msg, @Nullable Throwable e) {
        log(msg);

        if (e != null)
            log(e.toString());
    }

    /** {@inheritDoc} */
    @Override public boolean isTraceEnabled() {
        return dbg;
    }

    /** {@inheritDoc} */
    @Override public boolean isDebugEnabled() {
        return dbg;
    }

    /** {@inheritDoc} */
    @Override public boolean isInfoEnabled() {
        return true;
    }

    /** {@inheritDoc} */
    @Override public boolean isQuiet() {
        return false;
    }

    /** {@inheritDoc} */
    @Nullable @Override public String fileName() {
        return null;
    }

    /**
     * Resets logger.
     */
    public void reset() {
        buf.setLength(0);
    }

    /** {@inheritDoc} */
    @Override public String toString() {
        return buf.toString();
    }
}
@@ -1083,32 +1083,73 @@
 Logger %7B%0A    /**
+ Initial string builder capacity in bytes
  */%0A    private 
@@ -1165,19 +1165,23 @@
 nal int 
-MAX
+INITIAL
  = 1024 
@@ -1186,10 +1186,10 @@
 4 * 
-11
+33
 ;%0A%0A 
@@ -1186,32 +1186,80 @@
 4 * 33;%0A%0A    /**
+ Maximum characters to be kept in string builder
  */%0A    private 
@@ -1293,18 +1293,18 @@
  1024 * 
-10
+32
 ;%0A%0A    /
@@ -1297,32 +1297,67 @@
 4 * 32;%0A%0A    /**
+ Builder to accumulate log messages
  */%0A    private 
@@ -1398,11 +1398,15 @@
 der(
-MAX
+INITIAL
 );%0A%0A

Fix for test IgniteDiagnosticMessagesMultipleConnectionsTest.testLongRunning