/*
 * Copyright 2014 Higher Frequency Trading
 *
 * http://www.higherfrequencytrading.com
 *
 * 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 net.openhft.chronicle.queue;

import net.openhft.chronicle.bytes.Bytes;
import net.openhft.chronicle.queue.impl.single.SingleChronicleQueueBuilder;
import net.openhft.chronicle.wire.DocumentContext;

import java.io.File;
import java.io.IOException;

/**
 * Display records in a Chronicle in a text form.
 *
 * @author peter.lawrey
 */
public enum ChronicleReader {
    ;

    public static void main(String... args) throws IOException, InterruptedException {
        if (args.length < 1) {
            System.err.println("Usage: java " + ChronicleReader.class.getName() + " {chronicle-base-path} [from-index]");
            System.exit(-1);
        }

        String basePath = args[0];
        ChronicleQueue ic = SingleChronicleQueueBuilder.binary(new File(basePath)).build();
        ExcerptTailer tailer = ic.createTailer();
        if (args.length > 1) {
            long index = Long.parseLong(args[1]);
            while (!tailer.moveToIndex(index))
                Thread.sleep(50);
        }

        //noinspection InfiniteLoopStatement
        while (true) {
            try (DocumentContext dc = tailer.readingDocument()) {
                if (!dc.isPresent()) {
                    Thread.sleep(50);
                    continue;
                }
                System.out.print(Long.toHexString(dc.index()) + ": ");
                Bytes<?> bytes = dc.wire().bytes();
                byte b0 = bytes.readByte(0);
                if (b0 < 0) {
                    System.out.println(bytes.toHexString());
                } else {
                    System.out.println(bytes.toString());
                }
            }
            System.out.println();
        }
    }
}

@@ -832,31 +832,121 @@
 le.wire.
-DocumentContext
+BinaryWire;%0Aimport net.openhft.chronicle.wire.DocumentContext;%0Aimport net.openhft.chronicle.wire.TextWire
 ;%0A%0Aimpor
@@ -1335,16 +1335,24 @@
 se-path%7D
+ %7Bregex%7D
  %5Bfrom-i
@@ -1435,16 +1435,337 @@
 rgs%5B0%5D;%0A
+        String regex = args.length %3E 0 ? args%5B1%5D : %22%22;%0A        long index = args.length %3E 1 ? Long.decode(args%5B2%5D) : 0;%0A        tailFileFrom(basePath, regex, index, false);%0A    %7D%0A%0A    public static void tailFileFrom(String basePath, String regex, long index, boolean stopAtEnd) throws IOException, InterruptedException %7B%0A
         
@@ -1910,31 +1910,25 @@
     if (
-args.length %3E 1
+index %3E 0
 ) %7B%0A    
@@ -1939,43 +1939,55 @@
     
-long index = Long.parseLong(args%5B1%5D
+System.out.println(%22Waiting for index %22 + index
 );%0A 
@@ -2031,16 +2031,18 @@
 (index))
+ %7B
 %0A       
@@ -2057,32 +2057,33 @@
  Thread.sleep(50
+0
 );%0A        %7D%0A%0A  
@@ -2069,34 +2069,98 @@
 p(500);%0A        
-%7D%0A
+    %7D%0A        %7D%0A%0A        Bytes bytes2 = Bytes.elasticByteBuffer();
 %0A        //noins
@@ -2309,32 +2309,98 @@
 .isPresent()) %7B%0A
+                    if (stopAtEnd)%0A                        break;%0A
                 
@@ -2502,16 +2502,23 @@
 t.print(
+%220x%22 + 
 Long.toH
@@ -2644,100 +2644,242 @@
 yte(
-0);%0A                if (b0 %3C 0) %7B%0A                    System.out.println(bytes.toHex
+bytes.readPosition());%0A                String text;%0A                if (b0 %3C 0) %7B%0A                    bytes2.clear();%0A                    new BinaryWire(bytes).copyTo(new TextWire(bytes2));%0A                    text = bytes2.to
 String()
 );%0A 
@@ -2866,33 +2866,32 @@
 ytes2.toString()
-)
 ;%0A              
@@ -2921,35 +2921,23 @@
         
-System.out.println(
+text = 
 bytes.to
@@ -2944,17 +2944,16 @@
 String()
-)
 ;%0A      
@@ -2968,34 +2968,88 @@
   %7D%0A            
-%7D%0A
+    if (regex.isEmpty() %7C%7C text.matches(regex))%0A        
             Syst
@@ -3063,18 +3063,36 @@
 println(
-);
+text);%0A            %7D
 %0A       

Add a reader for Single Queues