1 package org.openecomp.mso.filesearching;
4 import java.io.FileNotFoundException;
5 import java.io.FileOutputStream;
6 import java.io.FileWriter;
7 import java.io.IOException;
8 import java.io.PrintWriter;
9 import java.io.UnsupportedEncodingException;
10 import java.util.NoSuchElementException;
11 import java.util.Scanner;
13 public class LogFileSearching {
15 private static PrintWriter writer;
17 public static void initFile(String filePath) {
20 // This is to reopen an existing file
21 writer = new PrintWriter(new FileOutputStream(filePath,true));
22 } catch ( IOException e) {
23 System.out.println("Exception caught when trying to open the file /tmp/mso-log-checker.log to dump the result");
29 public static void closeFile() {
37 public static boolean searchInFile(final String needle, final File file) throws FileNotFoundException {
38 Scanner logScanner = new Scanner(file);
40 writer.println("Searching pattern " + needle + " in " + file.getAbsolutePath());
41 //System.out.println("Searching pattern " + needle + " in " + file.getAbsolutePath());
43 String filedata = logScanner.useDelimiter("\\Z").next();
48 while (index < filedata.length() && (index = filedata.indexOf(needle, index)) >= 0) {
51 int separatorIndex = filedata.indexOf(System.getProperty("line.separator"), index);
52 if (separatorIndex >=0){
53 writer.println("FOUND:" + filedata.substring(index, separatorIndex));
54 //System.out.println("FOUND:"
55 // + filedata.substring(index, separatorIndex));
57 writer.println("FOUND:" + filedata.substring(index, filedata.length()-1));
58 //System.out.println("FOUND:"
59 // + filedata.substring(index, filedata.length()-1));
61 index += needle.length();
63 writer.println("TOTAL:" + occurrences + " FOUND");
64 //System.out.println("TOTAL:" + occurrences + " FOUND");
65 if (occurrences > 0) {
72 } catch (NoSuchElementException e) {
73 writer.println("TOTAL:0 FOUND");
74 //System.out.println("TOTAL:0 FOUND");
81 public static boolean searchInDirectory(final String needle, final File directory) throws FileNotFoundException {
84 String[] dirFiles = directory.list();
86 if (dirFiles != null) {
88 for (String dir : dirFiles) {
89 res = res || searchInDirectory(needle, new File(directory.getAbsolutePath() + "/" + dir));
93 return LogFileSearching.searchInFile(needle, directory);
98 public static boolean searchInDirectoryForCommonIssues(final String[] needles, final File directory) throws FileNotFoundException {
99 String[] defaultPatternsToUse = {"ClassNotFound","NullPointer","RuntimeException","IllegalStateException","FATAL"};
101 if (needles != null && needles.length>0) {
102 defaultPatternsToUse=needles;
105 boolean returnValue=false;
106 for (String needle:defaultPatternsToUse) {
107 returnValue |= LogFileSearching.searchInDirectory(needle,directory);