/*
 * Copyright (c) 2002-2018 "Neo4j,"
 * Neo4j Sweden AB [http://neo4j.com]
 *
 * This file is part of Neo4j.
 *
 * 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.neo4j.driver.internal.logging;

import io.netty.channel.Channel;

import org.neo4j.driver.internal.async.ChannelAttributes;
import org.neo4j.driver.v1.Logger;
import org.neo4j.driver.v1.Logging;

import static java.lang.String.format;

public class ChannelActivityLogger extends ReformattedLogger
{
    private final Channel channel;

    private String localChannelId;
    private String dbConnectionId;

    public ChannelActivityLogger( Channel channel, Logging logging, Class<?> owner )
    {
        this( channel, logging.getLog( owner.getSimpleName() ) );
    }

    private ChannelActivityLogger( Channel channel, Logger delegate )
    {
        super( delegate );
        this.channel = channel;
    }

    @Override
    protected String reformat( String message )
    {
        if ( channel == null )
        {
            return message;
        }

        String localChannelId = getLocalChannelId();
        String dbConnectionId = getDbConnectionId();

        if ( localChannelId != null && dbConnectionId != null )
        {
            return format( "[0x%s][%s] %s", localChannelId, dbConnectionId, message );
        }
        return format( "[0x%s][] %s", localChannelId, message );
    }

    private String getLocalChannelId()
    {
        if ( localChannelId == null )
        {
            localChannelId = channel.id().toString();
        }
        return localChannelId;
    }

    private String getDbConnectionId()
    {
        if ( dbConnectionId == null )
        {
            dbConnectionId = ChannelAttributes.connectionId( channel );
        }
        return dbConnectionId;
    }
}

@@ -1012,17 +1012,16 @@
 hannel;%0A
-%0A
     priv
@@ -1020,24 +1020,30 @@
     private 
+final 
 String local
@@ -1049,24 +1049,25 @@
 lChannelId;%0A
+%0A
     private 
@@ -1385,24 +1385,104 @@
  = channel;%0A
+        this.localChannelId = channel != null ? channel.id().toString() : null;%0A
     %7D%0A%0A    @
@@ -1628,61 +1628,8 @@
  %7D%0A%0A
-        String localChannelId = getLocalChannelId();%0A
     
@@ -1925,203 +1925,8 @@
  %7D%0A%0A
-    private String getLocalChannelId()%0A    %7B%0A        if ( localChannelId == null )%0A        %7B%0A            localChannelId = channel.id().toString();%0A        %7D%0A        return localChannelId;%0A    %7D%0A%0A
     

Initialize channel id in constructor