001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.classic.multiJVM;
015
016import java.io.BufferedReader;
017import java.io.FileReader;
018import java.util.regex.Matcher;
019import java.util.regex.Pattern;
020
021public class Checker {
022
023    static long LEN;
024    static String FILENAME;
025
026    static void usage(String msg) {
027        System.err.println(msg);
028        System.err.println("Usage: java " + Checker.class.getName() + " runLength filename stamp0 stamp1 ..stampN\n"
029                        + "   runLength (integer) the number of logs to generate perthread\n" + "    filename (string) the filename where to write\n"
030                        + "   stamp0 JVM instance stamp0\n" + "   stamp1 JVM instance stamp1\n");
031        System.exit(1);
032    }
033
034    public static void main(String[] argv) throws Exception {
035        if (argv.length < 3) {
036            usage("Wrong number of arguments.");
037        }
038
039        LEN = Integer.parseInt(argv[0]);
040        FILENAME = argv[1];
041
042        for (int i = 2; i < argv.length; i++) {
043            check(argv[i], FILENAME, true);
044        }
045    }
046
047    static void check(String stamp, String filename, boolean safetyMode) throws Exception {
048
049        try (FileReader fr = new FileReader(FILENAME); BufferedReader br = new BufferedReader(fr)) {
050
051            String regExp = "^" + stamp + " DEBUG - " + LoggingThread.msgLong + " (\\d+)$";
052            Pattern p = Pattern.compile(regExp);
053
054            String line;
055            int expected = 0;
056            while ((line = br.readLine()) != null) {
057                Matcher m = p.matcher(line);
058                if (m.matches()) {
059                    String g = m.group(1);
060                    int num = Integer.parseInt(g);
061                    if (num != expected) {
062                        System.err.println("ERROR: out of sequence line: ");
063                        System.err.println(line);
064                        return;
065                    }
066                    expected++;
067                }
068            }
069
070            if (expected != LEN) {
071                System.err.println("ERROR: For JVM stamp " + stamp + " found " + expected + " was expecting " + LEN);
072            } else {
073                System.out.println("For JVM stamp " + stamp + " found " + LEN + " lines in correct sequence");
074            }
075        }
076    }
077}