diff --git a/README.md b/README.md index d33c6fa..8f3d88b 100644 --- a/README.md +++ b/README.md @@ -40,16 +40,16 @@ rm .git/hooks/pre-commit ## Getting started with the large language model (LLM) ## -### Download the language model (follow one of these instructions) ### -Download the bin-file directly from [Hugging Face](https://huggingface.co/): +### Download the language model ### +* Either download the bin-file directly from [Hugging Face](https://huggingface.co/): [ggml-model-gpt4all-falcon-q4_0.bin](https://huggingface.co/nomic-ai/gpt4all-falcon-ggml/resolve/main/ggml-model-gpt4all-falcon-q4_0.bin?download=true) (4.06 GB) -Choose downloads from the website: +* Or choose downloads from the website: [https://huggingface.co/nomic-ai/gpt4all-falcon-ggml/tree/main](https://huggingface.co/nomic-ai/gpt4all-falcon-ggml/tree/main) -You may also download any other model of your taste and use that one instead. +* Or you may also download any [other model](https://huggingface.co/models) of your taste and use that one instead. -### Set up the configuration ### +### Setting up the configuration ### #### Add model and set the configuration to use LLM #### @@ -61,4 +61,47 @@ You may also download any other model of your taste and use that one instead. Run `Main.java` in the project. #### Results #### -The results will be shown in the `/results/` folder, +The results will be shown in the `/results/` folder. By default, the result naming is `result_{nr}` where `{nr}` is represented by a number that increases for each execution. + +## Configuration file ## +You can edit some configuration in `config.properties`. + +
+ + Default configuration (click to expand) + + +###### *config.properties* + ```shell +LOGGING_LEVEL=INFO + # This is related to TEMP_FILE below. +TEMP_FILE_ENABLED=false + # If enabled, this file will be deleted and replaced every run instead of using the results files. +TEMP_FILE=debugFile.txt + # Directory of where the result files are saved. They range from 0 to 99. +PATH_TO_RESULTS=results + # Set this property to a path that should be excluded. It's regex based, so writing [file1, file2] would exclude any file containing file1 and file2. +EXCLUDED_PATHS=[] + # Prefix for the result files. +RESULT_NAME_PREFIX=result_{nr} + # The LLM located in project root. +LLM_FILE=models/ggml-model-gpt4all-falcon-q4_0.bin + # Run with LLM - needs a model defined and will take more computing power and time to run. +RUN_WITH_LLM=false + # Directory path to where the code is to be scanned. +PATH_TO_CODE=src/test/resources/ + ``` +
+ +#### LOGGING_LEVEL +The different options are the following: + +| | FATAL | ERROR | WARNING | INFO | DEBUG | TRACE | +|----------|:-----:|:-----:|:-------:|:----:|:-----:|:-----:| +| OFF/NONE | | | | | | | +| FATAL | X | | | | | | +| ERROR | X | X | | | | | +| WARNING | X | X | X | | | | +| INFO | X | X | X | X | | | +| DEBUG | X | X | X | X | X | | +| TRACE | X | X | X | X | X | X | diff --git a/config.properties b/config.properties index 006ee33..65255e7 100644 --- a/config.properties +++ b/config.properties @@ -12,6 +12,6 @@ RESULT_NAME_PREFIX=result_{nr} # The LLM located in project root. LLM_FILE=model/ggml-model-gpt4all-falcon-q4_0.bin # Run with LLM - needs a model defined and will take more computing power and time to run. -RUN_WITH_LLM=true +RUN_WITH_LLM=false # Directory path to where the code is to be scanned. PATH_TO_CODE=src/test/resources/ diff --git a/src/main/java/CodeCheck/ConfigInterface.java b/src/main/java/CodeCheck/ConfigInterface.java index f0596a7..136d63e 100644 --- a/src/main/java/CodeCheck/ConfigInterface.java +++ b/src/main/java/CodeCheck/ConfigInterface.java @@ -13,6 +13,9 @@ public interface ConfigInterface { class Config { enum LoggingLevel { NONE(0), + N(NONE.lvl), + OFF(NONE.lvl), + O(OFF.lvl), ERROR(1), ERR(ERROR.lvl), E(ERROR.lvl), @@ -28,7 +31,7 @@ enum LoggingLevel { private final int lvl; - public Boolean logOn(LoggingLevel lvl) { + public boolean logOn(LoggingLevel lvl) { return lvl.lvl <= this.lvl; } diff --git a/src/main/java/CodeCheck/LLM.java b/src/main/java/CodeCheck/LLM.java index 08f8f79..ced3f9f 100644 --- a/src/main/java/CodeCheck/LLM.java +++ b/src/main/java/CodeCheck/LLM.java @@ -52,7 +52,7 @@ public void cleanModel() { return true; }); - int[] formattedTime = Log.getFormattedDurationTime(startTime); + int[] formattedTime = Util.getFormattedDurationTime(startTime); Log.log(String.format("This LLM run completed in %02d hours, %02d minutes, %02d seconds and %02d milliseconds.", formattedTime[0], formattedTime[1], formattedTime[2], formattedTime[3])); } diff --git a/src/main/java/CodeCheck/Log.java b/src/main/java/CodeCheck/Log.java index 1154fc0..13c60a4 100644 --- a/src/main/java/CodeCheck/Log.java +++ b/src/main/java/CodeCheck/Log.java @@ -5,13 +5,18 @@ public interface Log { // Styling - String ANSI_RESET = "\u001B[0m"; - String ANSI_RED = "\u001B[31m"; - String ANSI_GREEN = "\u001B[32m"; - String ANSI_YELLOW = "\u001B[33m"; - String ANSI_BLUE = "\u001B[34m"; - String ANSI_PURPLE = "\u001B[35m"; - + enum Color { + NONE { @Override public String toString() { return ""; } }, + RESET { @Override public String toString() { return WHITE.toString(); } }, + WHITE { @Override public String toString() { return "\u001B[0m"; } }, + RED { @Override public String toString() { return "\u001B[31m"; } }, + GREEN { @Override public String toString() { return "\u001B[32m"; } }, + YELLOW { @Override public String toString() { return "\u001B[033m"; } }, + BLUE { @Override public String toString() { return "\u001B[34m"; } }, + PURPLE { @Override public String toString() { return "\u001B[35m"; } }, + BLACK { @Override public String toString() { return "\u001B[36m"; } }, + CYAN { @Override public String toString() { return "\u001B[37m"; } }; + } static void log(String msg) { log(msg, ConfigInterface.Config.LoggingLevel.INFO); @@ -33,10 +38,14 @@ static void trace(String msg) { log(msg, ConfigInterface.Config.LoggingLevel.TRACE); } - private static void log(String msg, ConfigInterface.Config.LoggingLevel lvl) { + static void log(String msg, ConfigInterface.Config.LoggingLevel lvl) { ConfigInterface.Config.LoggingLevel logLvl = ConfigInterface.conf.getLogLvl(); if (logLvl.logOn(lvl)) { - logReduced("[%s] ".formatted(LocalDateTime.now()) + "[" + lvl.name() + "] " + msg + System.lineSeparator(), lvl); + logReduced("%s [%s] %s".formatted( + LocalDateTime.now(), + lvl.name(), + msg + System.lineSeparator()), + lvl); } } @@ -49,17 +58,17 @@ static void log(String msg, ConfigInterface.Config.LoggingLevel level, Object... } static void logReduced(String msg, ConfigInterface.Config.LoggingLevel level, boolean newLine) { - String colorLevel = switch (level) { - case NONE -> ""; - case INFO, I -> ANSI_RESET; - case DEBUG, D -> ANSI_GREEN; - case TRACE, T -> ANSI_BLUE; - case WARNING, WARN, W -> ANSI_YELLOW; - case ERROR, ERR, E -> ANSI_RED; + Color colorLevel = switch (level) { + case NONE, N, OFF, O -> Color.NONE; // No change in color. + case ERROR, ERR, E -> Color.RED; + case WARNING, WARN, W -> Color.YELLOW; + case INFO, I -> Color.WHITE; + case DEBUG, D -> Color.GREEN; + case TRACE, T -> Color.BLUE; }; - if (newLine) System.out.println(colorLevel + msg + ANSI_RESET); - else System.out.print(colorLevel + msg + ANSI_RESET); + if (newLine) System.out.println(colorLevel + msg + Color.RESET); + else System.out.print(colorLevel + msg + Color.RESET); } static void logReduced(String msg, ConfigInterface.Config.LoggingLevel level) { @@ -74,18 +83,13 @@ static void logReduced(String msg, Object... formatting) { logReduced(String.format(msg, formatting), ConfigInterface.Config.LoggingLevel.INFO, false); } - /** - * Calculate the duration from the start time and the current time. - * - * @param startTime Input the start time. - * @return An integer array of time the duration in HH:MM:SS:MS. 0 = h, 1 = m, 2 = s, 3 = ms - */ - static int[] getFormattedDurationTime(long startTime) { - long millis = (System.currentTimeMillis() - startTime); - int seconds = (int) millis / 1000; - int minutes = (seconds % 3600) / 60; - int hours = seconds / 3600; - - return new int[]{hours, minutes, seconds % 60, (int) millis % 1000}; + static void logColor(String msg, Color color) { + System.out.printf( + "%s%s [%s] %s%n", + color, + LocalDateTime.now(), + ConfigInterface.Config.LoggingLevel.INFO, + msg + ); } } diff --git a/src/main/java/CodeCheck/Util.java b/src/main/java/CodeCheck/Util.java index 40850ed..053ce3b 100644 --- a/src/main/java/CodeCheck/Util.java +++ b/src/main/java/CodeCheck/Util.java @@ -41,4 +41,19 @@ static void write(String filePath, String msg, boolean append) { e.printStackTrace(); } } + + /** + * Calculate the duration from the start time and the current time. + * + * @param startTime Input the start time. + * @return An integer array of time the duration in HH:MM:SS:MS. 0 = h, 1 = m, 2 = s, 3 = ms + */ + static int[] getFormattedDurationTime(long startTime) { + long millis = (System.currentTimeMillis() - startTime); + int seconds = (int) millis / 1000; + int minutes = (seconds % 3600) / 60; + int hours = seconds / 3600; + + return new int[]{hours, minutes, seconds % 60, (int) millis % 1000}; + } } diff --git a/src/main/java/CodeCheck/WriteObjectToFile.java b/src/main/java/CodeCheck/WriteObjectToFile.java index 0caf2d5..09f0811 100644 --- a/src/main/java/CodeCheck/WriteObjectToFile.java +++ b/src/main/java/CodeCheck/WriteObjectToFile.java @@ -1,8 +1,6 @@ package CodeCheck; import java.io.File; -import java.io.FileWriter; -import java.io.IOException; public class WriteObjectToFile { @@ -21,10 +19,15 @@ public WriteObjectToFile() throws Exception { Log.log("Debug mode enabled, deleting the temporary file %s before continuing... ".formatted(PATH_TO_RESULTS + File.separator + TEMP_FILE)); } - // Create results directory if it doesn't exist. - if (!(new File(PATH_TO_RESULTS).exists())) { - new File(PATH_TO_RESULTS).mkdir(); - Log.log("Created directory %s...", PATH_TO_RESULTS); + // Create directory if it doesn't exist. + File file = new File(PATH_TO_RESULTS); + if (!file.isDirectory()) { + if (file.mkdirs()) { + Log.logColor("Created directory %s...".formatted(file.getAbsolutePath()), Log.Color.PURPLE); + } else { + Log.error("Could not create directory %s...".formatted(file.getAbsolutePath())); + throw new Exception("Could not create directory %s...".formatted(file.getAbsolutePath())); + } } // Look for the next file to write towards.