View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.classic.multiJVM;
15  
16  import java.io.BufferedReader;
17  import java.io.FileReader;
18  import java.util.regex.Matcher;
19  import java.util.regex.Pattern;
20  
21  public class Checker {
22  
23      static long LEN;
24      static String FILENAME;
25  
26      static void usage(String msg) {
27          System.err.println(msg);
28          System.err.println("Usage: java " + Checker.class.getName() + " runLength filename stamp0 stamp1 ..stampN\n"
29                          + "   runLength (integer) the number of logs to generate perthread\n" + "    filename (string) the filename where to write\n"
30                          + "   stamp0 JVM instance stamp0\n" + "   stamp1 JVM instance stamp1\n");
31          System.exit(1);
32      }
33  
34      public static void main(String[] argv) throws Exception {
35          if (argv.length < 3) {
36              usage("Wrong number of arguments.");
37          }
38  
39          LEN = Integer.parseInt(argv[0]);
40          FILENAME = argv[1];
41  
42          for (int i = 2; i < argv.length; i++) {
43              check(argv[i], FILENAME, true);
44          }
45      }
46  
47      static void check(String stamp, String filename, boolean safetyMode) throws Exception {
48  
49          try (FileReader fr = new FileReader(FILENAME); BufferedReader br = new BufferedReader(fr)) {
50  
51              String regExp = "^" + stamp + " DEBUG - " + LoggingThread.msgLong + " (\\d+)$";
52              Pattern p = Pattern.compile(regExp);
53  
54              String line;
55              int expected = 0;
56              while ((line = br.readLine()) != null) {
57                  Matcher m = p.matcher(line);
58                  if (m.matches()) {
59                      String g = m.group(1);
60                      int num = Integer.parseInt(g);
61                      if (num != expected) {
62                          System.err.println("ERROR: out of sequence line: ");
63                          System.err.println(line);
64                          return;
65                      }
66                      expected++;
67                  }
68              }
69  
70              if (expected != LEN) {
71                  System.err.println("ERROR: For JVM stamp " + stamp + " found " + expected + " was expecting " + LEN);
72              } else {
73                  System.out.println("For JVM stamp " + stamp + " found " + LEN + " lines in correct sequence");
74              }
75          }
76      }
77  }