Skip to content

Commit

Permalink
Merge pull request #50 from saledouble/master_new
Browse files Browse the repository at this point in the history
Mismatch
  • Loading branch information
Grace Tang committed Jun 27, 2018
2 parents 76ce48f + f9f0d7b commit 802a52e
Showing 1 changed file with 95 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package edu.cuny.hunter.log.core.analysis;

import java.util.HashSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.dom.ASTVisitor;
import org.eclipse.jdt.core.dom.MethodInvocation;

import edu.cuny.hunter.log.core.utils.LoggerNames;
import edu.cuny.hunter.log.core.utils.Util;

Expand All @@ -20,6 +21,8 @@ public class LogAnalyzer extends ASTVisitor {

private Set<LogInvocation> logInvocationSet = new HashSet<>();

private static LinkedList<Float> boundary;

private boolean test;

public LogAnalyzer(boolean b) {
Expand All @@ -35,12 +38,99 @@ public void analyze() {
Map<IJavaProject, Set<LogInvocation>> projectToLoggings = this.getLogInvocationSet().stream()
.collect(Collectors.groupingBy(LogInvocation::getExpressionJavaProject, Collectors.toSet()));

this.getLogInvocationSet().forEach(e -> {
e.logInfo();
});
HashSet<Float> degreeOfInterests = new HashSet<>();
for (LogInvocation logInvocation : this.logInvocationSet) {
logInvocation.logInfo();
degreeOfInterests.add(logInvocation.getDegreeOfInterestValue());
}

// build boundary
boundary = buildBoundary(degreeOfInterests);
// check whether action is needed
for (LogInvocation logInvocation : this.logInvocationSet)
if (this.doAction(logInvocation))
// TODO: add more log messages here
LOGGER.info("Refactoring happens!");

}

private boolean doAction(LogInvocation logInvocation) {
Level currentLogLevel = logInvocation.getLogLevel();
Level suggestedLogLevel = getSuggestedLogLevel(boundary, logInvocation.getDegreeOfInterestValue());

// TODO: do action here
if (currentLogLevel == suggestedLogLevel)
return false;
if (suggestedLogLevel == null || currentLogLevel == null)
return false;
return true;
}

/**
* Get the suggested log level based on boundary.
*
* @param boundary
* @param DOI
* @return the suggested log level
*/
private static Level getSuggestedLogLevel(LinkedList<Float> boundary, float DOI) {
if (boundary == null)
return null;
if (DOI >= boundary.get(0) && DOI < boundary.get(1))
return Level.FINEST;
if (DOI < boundary.get(2))
return Level.FINER;
if (DOI < boundary.get(3))
return Level.FINE;
if (DOI < boundary.get(4))
return Level.CONFIG;
if (DOI < boundary.get(5))
return Level.INFO;
if (DOI < boundary.get(6))
return Level.WARNING;
if (DOI <= boundary.get(7))
return Level.SEVERE;
return null;
}

/**
* Build a list of boundary. The DOI values could be divided into 7 groups by
* this boundary. 7 groups are corresponding to 7 logging levels
*
* @param degreeOfInterests
* @return a list of boundary
*/
private LinkedList<Float> buildBoundary(HashSet<Float> degreeOfInterests) {
float min = getMinDOI(degreeOfInterests);
float max = getMaxDOI(degreeOfInterests);
LinkedList<Float> boundary = new LinkedList<>();
if (min <= max) {
float interval = (max - min) / 7;
IntStream.range(0, 8).forEach(i -> boundary.add(min + i * interval));
return boundary;
} else
return null;
}

// TODO: analyze logging here.
/**
* Get the minimum of DOIs
*
* @param degreeOfInterests
*/
private float getMinDOI(HashSet<Float> degreeOfInterests) {
float min = Float.MAX_VALUE;
for (float d : degreeOfInterests)
if (d < min)
min = d;
return min;
}

private float getMaxDOI(HashSet<Float> degreeOfInterests) {
float max = Float.MIN_VALUE;
for (float d : degreeOfInterests)
if (d > max)
max = d;
return max;
}

public Set<LogInvocation> getLogInvocationSet() {
Expand Down

0 comments on commit 802a52e

Please sign in to comment.