This project has retired. For details please refer to its Attic page.
Source code
001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019
020package org.apache.reef.util.logging;
021
022import java.io.BufferedReader;
023import java.io.FileReader;
024import java.io.IOException;
025import java.util.ArrayList;
026
027/**
028 * Parse logs for reporting
029 */
030public class LogParser {
031
032  public static String endIndicators[] = {
033      LoggingScopeImpl.EXIT_PREFIX + LoggingScopeFactory.BRIDGE_SETUP,
034      LoggingScopeImpl.EXIT_PREFIX + LoggingScopeFactory.EVALUATOR_SUBMIT,
035      LoggingScopeImpl.EXIT_PREFIX + LoggingScopeFactory.EVALUATOR_BRIDGE_SUBMIT,
036      LoggingScopeImpl.EXIT_PREFIX + LoggingScopeFactory.DRIVER_START,
037      LoggingScopeImpl.EXIT_PREFIX + LoggingScopeFactory.EVALUATOR_LAUNCH,
038      LoggingScopeImpl.EXIT_PREFIX + LoggingScopeFactory.EVALUATOR_ALLOCATED,
039      LoggingScopeImpl.EXIT_PREFIX + LoggingScopeFactory.ACTIVE_CONTEXT,
040      LoggingScopeImpl.EXIT_PREFIX + LoggingScopeFactory.HTTP_REQUEST,
041      LoggingScopeImpl.EXIT_PREFIX + LoggingScopeFactory.TASK_COMPLETE
042  };
043
044  public static String startIndicators[] = {
045      LoggingScopeImpl.START_PREFIX + LoggingScopeFactory.DRIVER_START,
046      LoggingScopeImpl.START_PREFIX + LoggingScopeFactory.BRIDGE_SETUP,
047      LoggingScopeImpl.START_PREFIX + LoggingScopeFactory.EVALUATOR_BRIDGE_SUBMIT,
048      LoggingScopeImpl.START_PREFIX + LoggingScopeFactory.EVALUATOR_SUBMIT,
049      LoggingScopeImpl.START_PREFIX + LoggingScopeFactory.EVALUATOR_ALLOCATED,
050      LoggingScopeImpl.START_PREFIX + LoggingScopeFactory.EVALUATOR_LAUNCH,
051      LoggingScopeImpl.START_PREFIX + LoggingScopeFactory.ACTIVE_CONTEXT,
052      LoggingScopeImpl.START_PREFIX + LoggingScopeFactory.HTTP_REQUEST,
053      LoggingScopeImpl.START_PREFIX + LoggingScopeFactory.TASK_COMPLETE
054  };
055
056  private LogParser() {
057  }
058
059  /**
060   * Get lines from a given file with a specified filter, trim the line by removing strings before removeBeforeToken and after removeAfterToken
061   * @param fileName
062   * @param filter
063   * @return
064   * @throws IOException
065   */
066  public static ArrayList<String> getFilteredLinesFromFile(final String fileName, final String filter, final String removeBeforeToken, final String removeAfterToken) throws IOException{
067    final ArrayList<String> filteredLines = new ArrayList<String>();
068    try (final FileReader fr =  new FileReader(fileName)) {
069      try (final BufferedReader in = new BufferedReader(fr)) {
070        String line = "";
071        while ((line = in.readLine()) != null) {
072          if (line.trim().length() == 0) {
073            continue;
074          }
075          if (line.contains(filter)) {
076            String trimedLine;
077            if (removeBeforeToken != null) {
078              final String[] p = line.split(removeBeforeToken);
079              if (p.length > 1) {
080                trimedLine = p[p.length-1];
081              } else {
082                trimedLine = line.trim();
083              }
084            } else {
085              trimedLine = line.trim();
086            }
087            if (removeAfterToken != null) {
088              final String[] p = trimedLine.split(removeAfterToken);
089              if (p.length > 1) {
090                trimedLine = p[0];
091              }
092            }
093            filteredLines.add(trimedLine);
094          }
095        }
096      }
097    }
098    return filteredLines;
099  }
100
101  /**
102   * get lines from given file with specified filter
103   * @param fileName
104   * @param filter
105   * @return
106   * @throws IOException
107   */
108  public static ArrayList<String> getFilteredLinesFromFile(final String fileName, final String filter) throws IOException {
109    return getFilteredLinesFromFile(fileName, filter, null, null);
110  }
111
112  /**
113   * filter array list of lines and get the last portion of the line separated by the token, like ":::"
114   * @param original
115   * @param filter
116   * @return
117   */
118  public static ArrayList<String> filter(final ArrayList<String> original, final String filter, final String token) {
119    final ArrayList<String> result = new ArrayList<String>();
120    for (String line : original) {
121      if (line.contains(filter)) {
122        final String[] p = line.split(token);
123        if (p.length > 1) {
124          result.add(p[p.length-1]);
125        }
126      }
127    }
128    return result;
129  }
130
131  /**
132   * find lines that contain stage indicators. The stageIndicators must be in sequence which appear in the lines.
133   * @param lines
134   * @param stageIndicators
135   * @return
136   */
137  public static ArrayList<String> findStages(final ArrayList<String> lines, final String[] stageIndicators) {
138    ArrayList<String> stages = new ArrayList<String>();
139
140    int i = 0;
141    for (String line: lines) {
142      if (line.contains(stageIndicators[i])){
143        stages.add(stageIndicators[i]);
144        if (i < stageIndicators.length - 1) {
145          i++;
146        }
147      }
148    }
149    return stages;
150  }
151
152  public static ArrayList<String> mergeStages(ArrayList<String> startStages, ArrayList<String> endStages) {
153    ArrayList<String> mergeStage = new ArrayList<String>();
154    for (int i = 0; i < startStages.size(); i++) {
155      String end = startStages.get(i).replace(LoggingScopeImpl.START_PREFIX, LoggingScopeImpl.EXIT_PREFIX);
156      if (endStages.contains(end)) {
157        mergeStage.add(startStages.get(i)  + "   " + end);
158      } else {
159        mergeStage.add(startStages.get(i));
160      }
161    }
162    return mergeStage;
163  }
164}