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"
30                  + "    filename (string) the filename where to write\n" + "   stamp0 JVM instance stamp0\n"
31                  + "   stamp1 JVM instance stamp1\n");
32          System.exit(1);
33      }
34  
35      public static void main(String[] argv) throws Exception {
36          if (argv.length < 3) {
37              usage("Wrong number of arguments.");
38          }
39  
40          LEN = Integer.parseInt(argv[0]);
41          FILENAME = argv[1];
42  
43          for (int i = 2; i < argv.length; i++) {
44              check(argv[i], FILENAME, true);
45          }
46      }
47  
48      static void check(String stamp, String filename, boolean safetyMode) throws Exception {
49  
50          try (FileReader fr = new FileReader(FILENAME); BufferedReader br = new BufferedReader(fr)) {
51  
52              String regExp = "^" + stamp + " DEBUG - " + LoggingThread.msgLong + " (\\d+)$";
53              Pattern p = Pattern.compile(regExp);
54  
55              String line;
56              int expected = 0;
57              while ((line = br.readLine()) != null) {
58                  Matcher m = p.matcher(line);
59                  if (m.matches()) {
60                      String g = m.group(1);
61                      int num = Integer.parseInt(g);
62                      if (num != expected) {
63                          System.err.println("ERROR: out of sequence line: ");
64                          System.err.println(line);
65                          return;
66                      }
67                      expected++;
68                  }
69              }
70  
71              if (expected != LEN) {
72                  System.err.println("ERROR: For JVM stamp " + stamp + " found " + expected + " was expecting " + LEN);
73              } else {
74                  System.out.println("For JVM stamp " + stamp + " found " + LEN + " lines in correct sequence");
75              }
76          }
77      }
78  }