Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Yiming Tang committed May 3, 2019
1 parent cbc5292 commit 4754c4f
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
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 java.util.stream.Stream;

import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IMethod;
Expand Down Expand Up @@ -35,13 +37,14 @@ public class LogAnalyzer extends ASTVisitor {
private HashSet<LogInvocation> logInvsNotTransformedInIf = new HashSet<LogInvocation>();

/**
* Set of log invocations that their log levels are not lower in catch
* blocks
* Set of log invocations that their log levels are not lower in catch blocks
*/
private HashSet<LogInvocation> logInvsNotLoweredInCatch = new HashSet<LogInvocation>();

private HashSet<LogInvocation> logInvsNotLoweredInIfStatement = new HashSet<LogInvocation>();

private HashSet<LogInvocation> logInvsNotLoweredByKeywords = new HashSet<LogInvocation>();

private HashSet<MethodDeclaration> methodDeclarations = new HashSet<>();

private Set<LogInvocation> logInvocationSet = new HashSet<>();
Expand All @@ -50,6 +53,8 @@ public class LogAnalyzer extends ASTVisitor {

private boolean notLowerLogLevelInCatchBlock = false;

private boolean notLowerLogLevelWithKeyWords = false;

private boolean useLogCategoryWithConfig = false;

private boolean checkIfCondition = false;
Expand All @@ -60,6 +65,12 @@ public class LogAnalyzer extends ASTVisitor {

private LinkedList<Float> boundary;

/**
* A set of keywords in log messages.
*/
final private Set<String> keyWordsInLogMessages = Stream.of("failed", "disabled", "error")
.collect(Collectors.toSet());

private boolean test;

public LogAnalyzer(boolean isTest) {
Expand All @@ -71,11 +82,13 @@ public LinkedList<Float> getBoundary() {
}

public LogAnalyzer(boolean useConfigLogLevelCategory, boolean useLogLevelCategory,
boolean notLowerLogLevelInCatchBlock, boolean checkIfCondition, boolean notLowerLogLevelInIfStatement) {
boolean notLowerLogLevelInCatchBlock, boolean checkIfCondition, boolean notLowerLogLevelInIfStatement,
boolean notLowerLogLevelWithKeyWords) {
this.useLogCategoryWithConfig = useConfigLogLevelCategory;
this.useLogCategory = useLogLevelCategory;
this.notLowerLogLevelInCatchBlock = notLowerLogLevelInCatchBlock;
this.notLowerLogLevelInIfStatement = notLowerLogLevelInIfStatement;
this.notLowerLogLevelWithKeyWords = notLowerLogLevelWithKeyWords;
this.checkIfCondition = checkIfCondition;
}

Expand Down Expand Up @@ -141,6 +154,14 @@ private boolean doAction(LogInvocation logInvocation) {
return false;
}

if (this.notLowerLogLevelWithKeyWords) {
if (Util.isLogMessageWithKeywords(logInvocation.getExpression(), keyWordsInLogMessages)) {
logInvocation.setAction(Action.NONE, null);
this.logInvsNotLoweredByKeywords.add(logInvocation);
return false;
}
}

/**
* Do not change a log level in a logging statement if there exists an
* immediate if statement whose condition contains a log level.
Expand Down Expand Up @@ -530,5 +551,9 @@ public HashSet<LogInvocation> getLogInvsNotLoweredInCatch() {
public HashSet<LogInvocation> getLogInvsNotLoweredInIfStatement() {
return this.logInvsNotLoweredInIfStatement;
}

public HashSet<LogInvocation> getLogInvsNotLoweredByKeywords(){
return this.logInvsNotLoweredByKeywords;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ public class LogRejuvenatingProcessor extends RefactoringProcessor {
*/
private boolean notLowerLogLevelInIfStatement = true;

/**
* Not lower logs with particular keywords in their messages.
*/
private boolean notLowerLogLevelWithKeyWords = true;

/**
* Limit number of commits
*/
Expand All @@ -114,6 +119,7 @@ public class LogRejuvenatingProcessor extends RefactoringProcessor {
private HashSet<LogInvocation> logInvsNotTransformedInIf = new HashSet<LogInvocation>();
private HashSet<LogInvocation> logInvsNotLoweredInCatch = new HashSet<LogInvocation>();
private HashSet<LogInvocation> logInvsNotLoweredInIf = new HashSet<LogInvocation>();
private HashSet<LogInvocation> logInvsNotLoweredWithKeywords = new HashSet<LogInvocation>();

private String repoURL = "";

Expand Down Expand Up @@ -173,8 +179,8 @@ public LogRejuvenatingProcessor(IJavaProject[] javaProjects, final CodeGeneratio

public LogRejuvenatingProcessor(IJavaProject[] javaProjects, boolean useLogLevelCategory,
boolean useConfigLogLevelCategory, boolean useGitHistory, boolean notLowerLogLevelInCatchBlock,
boolean notLowerLogLevelInIfStatement, boolean checkIfCondtion, int NToUseForCommits,
final CodeGenerationSettings settings, Optional<IProgressMonitor> monitor) {
boolean notLowerLogLevelInIfStatement, boolean notLowerLogLevelWithKeywords, boolean checkIfCondtion,
int NToUseForCommits, final CodeGenerationSettings settings, Optional<IProgressMonitor> monitor) {
super(settings);
try {
this.javaProjects = javaProjects;
Expand All @@ -183,6 +189,7 @@ public LogRejuvenatingProcessor(IJavaProject[] javaProjects, boolean useLogLevel
this.useGitHistory = useGitHistory;
this.notLowerLogLevelInCatchBlock = notLowerLogLevelInCatchBlock;
this.notLowerLogLevelInIfStatement = notLowerLogLevelInIfStatement;
this.notLowerLogLevelWithKeyWords = notLowerLogLevelWithKeywords;
this.checkIfCondition = checkIfCondtion;
this.NToUseForCommits = NToUseForCommits;
} finally {
Expand All @@ -192,10 +199,12 @@ public LogRejuvenatingProcessor(IJavaProject[] javaProjects, boolean useLogLevel

public LogRejuvenatingProcessor(IJavaProject[] javaProjects, boolean useLogLevelCategory,
boolean useConfigLogLevelCategory, boolean useGitHistory, boolean notLowerLogLevelInCatchBlock,
boolean notLowerLogLevelInIfStatement, boolean checkIfCondtion, int NToUseForCommits,
final CodeGenerationSettings settings, Optional<IProgressMonitor> monitor, boolean isEvaluation) {
boolean notLowerLogLevelInIfStatement, boolean notLowerLogLevelWithKeywords, boolean checkIfCondtion,
int NToUseForCommits, final CodeGenerationSettings settings, Optional<IProgressMonitor> monitor,
boolean isEvaluation) {
this(javaProjects, useLogLevelCategory, useConfigLogLevelCategory, useGitHistory, notLowerLogLevelInCatchBlock,
notLowerLogLevelInIfStatement, checkIfCondtion, NToUseForCommits, settings, monitor);
notLowerLogLevelInIfStatement, notLowerLogLevelWithKeywords, checkIfCondtion, NToUseForCommits,
settings, monitor);
this.isEvaluation = isEvaluation;
}

Expand All @@ -218,7 +227,8 @@ public RefactoringStatus checkFinalConditions(final IProgressMonitor monitor, fi
for (IJavaProject jproj : this.getJavaProjects()) {

LogAnalyzer analyzer = new LogAnalyzer(this.useLogCategoryWithConfig, this.useLogCategory,
this.notLowerLogLevelInCatchBlock, this.checkIfCondition, this.notLowerLogLevelInIfStatement);
this.notLowerLogLevelInCatchBlock, this.checkIfCondition, this.notLowerLogLevelInIfStatement,
this.notLowerLogLevelWithKeyWords);

// If we are using the git history.
if (this.useGitHistory) {
Expand Down Expand Up @@ -267,6 +277,7 @@ public RefactoringStatus checkFinalConditions(final IProgressMonitor monitor, fi
this.setLogInvsNotLoweredInCatch(analyzer.getLogInvsNotLoweredInCatch());
this.setLogInvsNotTransformedInIf(analyzer.getLogInvsNotTransformedInIf());
this.setLogInvsNotLoweredInIf(analyzer.getLogInvsNotLoweredInIfStatement());
this.setLogInvsNotLoweredWithKeywords(analyzer.getLogInvsNotLoweredByKeywords());

// Get boundary
this.boundary = analyzer.getBoundary();
Expand Down Expand Up @@ -505,4 +516,20 @@ public void setLogInvsNotLoweredInIf(HashSet<LogInvocation> logInvsNotLoweredInI
this.logInvsNotLoweredInIf = logInvsNotLoweredInIf;
}

public boolean isNotLowerLogLevelWithKeyWords() {
return notLowerLogLevelWithKeyWords;
}

public void setNotLowerLogLevelWithKeyWords(boolean notLowerLogLevelWithKeyWords) {
this.notLowerLogLevelWithKeyWords = notLowerLogLevelWithKeyWords;
}

public HashSet<LogInvocation> getLogInvsNotLoweredWithKeywords() {
return logInvsNotLoweredWithKeywords;
}

private void setLogInvsNotLoweredWithKeywords(HashSet<LogInvocation> logInvsNotLoweredWithKeywords) {
this.logInvsNotLoweredWithKeywords = logInvsNotLoweredWithKeywords;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import org.eclipse.mylyn.context.core.ContextCore;
import org.eclipse.mylyn.context.core.IDegreeOfInterest;
import org.eclipse.mylyn.context.core.IInteractionElement;

import edu.cuny.hunter.log.core.refactorings.LogRejuvenatingProcessor;
import edu.cuny.hunter.mylyngit.core.analysis.MylynGitPredictionProvider;
import edu.cuny.hunter.mylyngit.core.utils.NonActiveMylynTaskException;
Expand Down Expand Up @@ -99,6 +98,52 @@ public static void clearTaskContext() throws NonActiveMylynTaskException {
MylynGitPredictionProvider.clearTaskContext();
}

public static boolean isLogMessageWithKeywords(MethodInvocation node, Set<String> keyWordsInLogMessages) {
String methodName = node.getName().toString();

@SuppressWarnings("unchecked")
List<Expression> arguments = node.arguments();
if (arguments.size() == 0)
return false;

if (methodName.equals("log")) {
Expression argument = arguments.get(1);
return havingKeyWord(argument.toString(), keyWordsInLogMessages);
}

if (methodName.equals("logp")) {
Expression argument = arguments.get(3);
return havingKeyWord(argument.toString(), keyWordsInLogMessages);
}
if (methodName.equals("logrb")) {
Expression argument = arguments.get(2);
Expression argument2 = arguments.get(4);
return havingKeyWord(argument.toString(), keyWordsInLogMessages)
|| havingKeyWord(argument2.toString(), keyWordsInLogMessages);
}

// Get rid of all and off here.
if (methodName.equals("config") || methodName.equals("fine") || methodName.equals("finer")
|| methodName.equals("finest") || methodName.equals("info") || methodName.equals("severe")
|| methodName.equals("warning")) {
Expression argument = arguments.get(0);
return havingKeyWord(argument.toString(), keyWordsInLogMessages);
}
return false;
}

/**
* Check whether log messages contain key words.
*/
public static boolean havingKeyWord(String logMessages, Set<String> keyWordsInLogMessages) {
logMessages = logMessages.toLowerCase();
for (String key : keyWordsInLogMessages) {
if (logMessages.contains(key))
return true;
}
return false;
}

/**
* We only focus on the logging level, which is set by the developer. Hence,
* we do not record the logging level which is embedded by the logging
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,15 @@ public class EvaluationHandler extends AbstractHandler {
private static final String EVALUATION_PROPERTIES_FILE_NAME = "eval.properties";
private static final String NOT_LOWER_LOG_LEVEL_IF_STATEMENT_KEY = "edu.cuny.hunter.log.evaluation.notLowerLogLevelInIfStatement";
private static final String NOT_LOWER_LOG_LEVEL_CATCH_BLOCK_KEY = "edu.cuny.hunter.log.evaluation.notLowerLogLevelInCatchBlock";
private static final String NOT_LOWER_LOG_LEVEL_KEYWORDS_KEY = "edu.cuny.hunter.log.evaluation.notLowerLogLevelWithKeywords";
private static final String USE_LOG_CATEGORY_CONFIG_KEY = "edu.cuny.hunter.log.evaluation.useLogCategoryWithConfig";
private static final String CHECK_IF_CONDITION_KEY = "edu.cuny.hunter.log. evaluation.checkIfCondition";
private static final String USE_LOG_CATEGORY_KEY = "edu.cuny.hunter.log.evaluation.useLogCategory";
private static final String USE_GIT_HISTORY_KEY = "edu.cuny.hunter.log.evaluation.useGitHistory";
private static final String N_TO_USE_FOR_COMMITS_KEY = "NToUseForCommits";
private static final int N_TO_USE_FOR_COMMITS_DEFAULT = 100;
private static final boolean NOR_LOWER_LOG_LEVEL_IF_STATEMENT_DEFAULT = false;
private static final boolean NOT_LOWER_LOG_LEVEL_IF_STATEMENT_DEFAULT = false;
private static final boolean NOT_LOWER_LOG_LEVEL_KEYWORDS_DEFAULT = false;
private static final boolean NOT_LOWER_LOG_LEVEL_CATCH_BLOCK_DEFAULT = false;
private static final boolean USE_LOG_CATEGORY_CONFIG_DEFAULT = false;
private static final boolean CHECK_IF_CONDITION_DEFAULT = false;
Expand All @@ -75,6 +77,7 @@ public class EvaluationHandler extends AbstractHandler {
private boolean useLogCategoryWithConfig;
private boolean notLowerLogLevelInCatchBlock;
private boolean notLowerLogLevelInIfStatement;
private boolean notLowerLogLevelWithKeywords;
private boolean checkIfCondtion;

@Override
Expand Down Expand Up @@ -102,9 +105,11 @@ public Object execute(ExecutionEvent event) throws ExecutionException {
"input logging statements", "candidate logging statements", "passing logging statements",
"failures", "transformed logging statements", "log level not lowered in catch blocks",
"log level not lowered in if statements", "log level not transformed due to if condition",
"log level not lowered due to keywords",
"use log category (SEVERE/WARNING/CONFIG)", "use log category (CONFIG)",
"not lower log levels of logs inside of catch blocks",
"not lower log levels of logs inside of if statements",
"not lower log levels in their messages with keywords",
"consider if condition having log level", "time (s)" });

repoPrinter = Util.createCSVPrinter("repos.csv",
Expand Down Expand Up @@ -153,6 +158,7 @@ public Object execute(ExecutionEvent event) throws ExecutionException {
new IJavaProject[] { project }, this.isUseLogCategory(),
this.isUseLogCategoryWithConfig(), this.getValueOfUseGitHistory(),
this.isNotLowerLogLevelInCatchBlock(), this.isNotLowerLogLevelInIfStatement(),
this.isNotLowerLogLevelWithKeywords(),
this.isCheckIfCondition(), NToUseCommit, settings, Optional.ofNullable(monitor),
true);

Expand Down Expand Up @@ -297,8 +303,10 @@ public Object execute(ExecutionEvent event) throws ExecutionException {
logRejuvenatingProcessor.getLogInvsNotLoweredInCatch().size(),
logRejuvenatingProcessor.getLogInvsNotLoweredInIf().size(),
logRejuvenatingProcessor.getLogInvsNotTransformedInIf().size(),
logRejuvenatingProcessor.getLogInvsNotLoweredWithKeywords().size(),
this.isUseLogCategory(), this.isUseLogCategoryWithConfig(),
this.isNotLowerLogLevelInCatchBlock(), this.isNotLowerLogLevelInIfStatement(),
this.isNotLowerLogLevelWithKeywords(),
this.isCheckIfCondition(), resultsTimeCollector.getCollectedTime());

}
Expand Down Expand Up @@ -446,6 +454,7 @@ private void loadSettings(int i) {
this.setUseLogCategoryWithConfig(this.computeLogCategoryWithConfig(i));
this.setNotLowerLogLevelInCatchBlock(this.computeLowerLogLevelInCatchBlock(i));
this.setNotLowerLogLevelInIfStatement(this.getValueOfNotLowerLogLevelInIfStatement());
this.setNotLowerLogLevelWithKeywords(this.getValueOfNotLowerLogLevelWithKeywords());
this.setCheckIfCondition(this.getValueOfCheckIfCondition());
if (this.isUseLogCategory() && this.isUseLogCategoryWithConfig())
throw new IllegalStateException("You cannot choose two log categories in the same time");
Expand Down Expand Up @@ -491,10 +500,19 @@ private boolean getValueOfNotLowerLogLevelInIfStatement() {
String notLowerLogLevelInIfStatement = System.getenv(NOT_LOWER_LOG_LEVEL_IF_STATEMENT_KEY);

if (notLowerLogLevelInIfStatement == null)
return NOR_LOWER_LOG_LEVEL_IF_STATEMENT_DEFAULT;
return NOT_LOWER_LOG_LEVEL_IF_STATEMENT_DEFAULT;
else
return Boolean.valueOf(notLowerLogLevelInIfStatement);
}

private boolean getValueOfNotLowerLogLevelWithKeywords() {
String notLowerLogLevelInIfStatement = System.getenv(NOT_LOWER_LOG_LEVEL_KEYWORDS_KEY);

if (notLowerLogLevelInIfStatement == null)
return NOT_LOWER_LOG_LEVEL_KEYWORDS_DEFAULT;
else
return Boolean.valueOf(notLowerLogLevelWithKeywords);
}

private boolean getValueOfCheckIfCondition() {
String considerIfCondition = System.getenv(CHECK_IF_CONDITION_KEY);
Expand Down Expand Up @@ -544,4 +562,12 @@ private void setNotLowerLogLevelInIfStatement(boolean notLowerLogLevelInIfStatem
public boolean isNotLowerLogLevelInIfStatement() {
return this.notLowerLogLevelInIfStatement;
}

private void setNotLowerLogLevelWithKeywords(boolean notLowerLogLevelWithKeywords) {
this.notLowerLogLevelWithKeywords = notLowerLogLevelWithKeywords;
}

public boolean isNotLowerLogLevelWithKeywords() {
return this.notLowerLogLevelWithKeywords;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ private static class LogInputPage extends UserInputWizardPage {

private static final String NOT_LOWER_LOG_LEVEL_IF_STATEMENT = "notLowerLogLevelInIfStatement";

private static final String NOT_LOWER_LOG_LEVEL_KEY_WORDS = "notLowerLogLevelKeyWords";

private static final String CHECK_IF_CONDITION = "checkIfCondition";

private LogRejuvenatingProcessor processor;
Expand Down Expand Up @@ -142,9 +144,16 @@ public void createControl(Composite parent) {

// set up buttons.
Button checkButton4 = this.addBooleanButton(
"Never lower the logging level of logging statements with particular keywords in their messages.",
NOT_LOWER_LOG_LEVEL_KEY_WORDS, this.getProcessor()::setNotLowerLogLevelWithKeyWords, result,
SWT.CHECK);
checkButton4.setSelection(true);

// set up buttons.
Button checkButton5 = this.addBooleanButton(
"Do not change a log level if its if statement condition contains a log level.", CHECK_IF_CONDITION,
this.getProcessor()::setCheckIfCondition, result, SWT.CHECK);
checkButton4.setSelection(true);
checkButton5.setSelection(true);

Label separator3 = new Label(result, SWT.SEPARATOR | SWT.SHADOW_OUT | SWT.HORIZONTAL);
separator3.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
Expand Down Expand Up @@ -204,6 +213,7 @@ private void loadSettings() {
this.settings.put(CHECK_IF_CONDITION, this.getProcessor().isCheckIfCondition());
this.settings.put(NOT_LOWER_LOG_LEVEL_IF_STATEMENT,
this.getProcessor().isNotLowerLogLevelInIfStatement());
this.settings.put(NOT_LOWER_LOG_LEVEL_KEY_WORDS, this.getProcessor().isNotLowerLogLevelWithKeyWords());
}
this.processor.setParticularConfigLogLevel(this.settings.getBoolean(USE_LOG_CATEGORY_CONFIG));
this.processor.setParticularLogLevel(this.settings.getBoolean(USE_LOG_CATEGORY));
Expand All @@ -212,6 +222,7 @@ private void loadSettings() {
this.processor.setNotLowerLogLevelInCatchBlock(this.settings.getBoolean(NOT_LOWER_LOG_LEVEL_CATCH_BLOCK));
this.processor.setCheckIfCondition(this.settings.getBoolean(CHECK_IF_CONDITION));
this.processor.setNotLowerLogLevelInIfStatement(this.settings.getBoolean(NOT_LOWER_LOG_LEVEL_IF_STATEMENT));
this.processor.setNotLowerLogLevelWithKeyWords(this.settings.getBoolean(NOT_LOWER_LOG_LEVEL_KEY_WORDS));
}

private void setProcessor(LogRejuvenatingProcessor processor) {
Expand Down

0 comments on commit 4754c4f

Please sign in to comment.