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