public void logNewElection(
        final int memberId,
        final long leadershipTermId,
        final long logPosition,
        final long appendPosition,
        final String reason)
    {
        final int length = ClusterEventEncoder.newElectionLength(reason);
        final int captureLength = captureLength(length);
        final int encodedLength = encodedLength(captureLength);
        final ManyToOneRingBuffer ringBuffer = this.ringBuffer;
        final int index = ringBuffer.tryClaim(NEW_ELECTION.toEventCodeId(), encodedLength);

        if (index > 0)
        {
            try
            {
                ClusterEventEncoder.encodeNewElection(
                    (UnsafeBuffer)ringBuffer.buffer(),
                    index,
                    captureLength,
                    length,
                    memberId,
                    leadershipTermId,
                    logPosition,
                    appendPosition,
                    reason);
            }
            finally
            {
                ringBuffer.commit(index);
            }
        }
    }
@Test
    void logNewElection()
    {
        final int memberId = 42;
        final long leadershipTermId = 8L;
        final long logPosition = 9827342L;
        final long appendPosition = 342384382L;
        final String reason = "why an election was started";
        final int offset = 16;
        logBuffer.putLong(CAPACITY + TAIL_POSITION_OFFSET, offset);

        final int encodedLength = newElectionLength(reason);

        logger.logNewElection(memberId, leadershipTermId, logPosition, appendPosition, reason);

        verifyLogHeader(
            logBuffer,
            offset,
            NEW_ELECTION.toEventCodeId(),
            encodedLength,
            encodedLength);

        final int index = encodedMsgOffset(offset) + LOG_HEADER_LENGTH;
        assertEquals(leadershipTermId, logBuffer.getLong(index, LITTLE_ENDIAN));
        assertEquals(logPosition, logBuffer.getLong(index + SIZE_OF_LONG, LITTLE_ENDIAN));
        assertEquals(appendPosition, logBuffer.getLong(index + 2 * SIZE_OF_LONG, LITTLE_ENDIAN));
        assertEquals(memberId, logBuffer.getInt(index + (3 * SIZE_OF_LONG), LITTLE_ENDIAN));
        assertEquals(reason, logBuffer.getStringAscii(index + (3 * SIZE_OF_LONG) + SIZE_OF_INT, LITTLE_ENDIAN));

        final StringBuilder sb = new StringBuilder();
        ClusterEventDissector.dissectNewElection(NEW_ELECTION, logBuffer, encodedMsgOffset(offset), sb);

        final String expectedMessagePattern =
            "\\[[0-9]+\\.[0-9]+] CLUSTER: NEW_ELECTION \\[59/59]: memberId=42 " +
            "leadershipTermId=8 logPosition=9827342 appendPosition=342384382 reason=why an election was started";

        assertThat(sb.toString(), Matchers.matchesPattern(expectedMessagePattern));
    }