diff --git a/edu.cuny.hunter.log.core/src/edu/cuny/hunter/log/core/analysis/LogAnalyzer.java b/edu.cuny.hunter.log.core/src/edu/cuny/hunter/log/core/analysis/LogAnalyzer.java index dcb73ac2..77b3f721 100644 --- a/edu.cuny.hunter.log.core/src/edu/cuny/hunter/log/core/analysis/LogAnalyzer.java +++ b/edu.cuny.hunter.log.core/src/edu/cuny/hunter/log/core/analysis/LogAnalyzer.java @@ -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; @@ -20,6 +21,8 @@ public class LogAnalyzer extends ASTVisitor { private Set logInvocationSet = new HashSet<>(); + private static LinkedList boundary; + private boolean test; public LogAnalyzer(boolean b) { @@ -35,12 +38,99 @@ public void analyze() { Map> projectToLoggings = this.getLogInvocationSet().stream() .collect(Collectors.groupingBy(LogInvocation::getExpressionJavaProject, Collectors.toSet())); - this.getLogInvocationSet().forEach(e -> { - e.logInfo(); - }); + HashSet 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 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 buildBoundary(HashSet degreeOfInterests) { + float min = getMinDOI(degreeOfInterests); + float max = getMaxDOI(degreeOfInterests); + LinkedList 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 degreeOfInterests) { + float min = Float.MAX_VALUE; + for (float d : degreeOfInterests) + if (d < min) + min = d; + return min; + } + private float getMaxDOI(HashSet degreeOfInterests) { + float max = Float.MIN_VALUE; + for (float d : degreeOfInterests) + if (d > max) + max = d; + return max; } public Set getLogInvocationSet() {