diff options
| author | Benjamin J. Culkin <bjculkin@mix.wvu.edu> | 2018-08-10 10:58:22 -0300 |
|---|---|---|
| committer | Benjamin J. Culkin <bjculkin@mix.wvu.edu> | 2018-08-10 10:58:22 -0300 |
| commit | 2acee8f9acf53fd8c1f97f8d40bf0bd6fd53148a (patch) | |
| tree | 8030a0d292218d1205b9f905945917641dc7eb73 /src | |
| parent | 22ab7a4dd6e608b5ef4d30b1d2ba2816a382dd52 (diff) | |
Update logging
Logging now uses the RGrammarLogging system so as to allow for better
customizability of how the logging works
Diffstat (limited to 'src')
12 files changed, 160 insertions, 57 deletions
diff --git a/src/main/java/bjc/rgens/parser/ConfigLoader.java b/src/main/java/bjc/rgens/parser/ConfigLoader.java index ff0dd73..a9a5876 100644 --- a/src/main/java/bjc/rgens/parser/ConfigLoader.java +++ b/src/main/java/bjc/rgens/parser/ConfigLoader.java @@ -11,6 +11,8 @@ import java.util.Scanner; import bjc.rgens.parser.templates.GrammarTemplate; +import static bjc.rgens.parser.RGrammarLogging.*; + public class ConfigLoader { /** * Load a grammar set from a configuration file. @@ -70,16 +72,13 @@ public class ConfigLoader { throw new GrammarException("Unknown config line type " + type); } } catch(GrammarException gex) { - System.out.printf("ERROR: Line %s of config set %s\n", lno, cfgFile); + System.out.printf("ERROR: Line %s of config set %s (%s)\n", lno, cfgFile, gex.getRootMessage()); - System.err.printf("ERROR: Line %s of config set %s\n", lno, cfgFile); + error(gex, "Line %s of config set %s (%s)", lno, cfgFile, gex.getRootMessage()); gex.printStackTrace(); System.out.println(); System.out.println(); - - System.err.println(); - System.err.println(); } } } @@ -88,7 +87,7 @@ public class ConfigLoader { long cfgDur = endCFGTime - startCFGTime; - System.err.printf("\n\nPERF: Read config file %s in %d ns (%f s)\n", cfgFile, cfgDur, cfgDur / 1000000000.0); + perf("Read config file %s in %d ns (%f s)", cfgFile, cfgDur, cfgDur / 1000000000.0); return cfgSet; } @@ -98,7 +97,7 @@ public class ConfigLoader { * Get the place where the tag ID ends */ if(parts.length < 2) { - throw new GrammarException("Must specify a tag as to what a line is"); + throw new GrammarException("Must specify object tag"); } String tag = parts[1]; @@ -107,7 +106,7 @@ public class ConfigLoader { * ends. */ if (parts.length < 3) { - throw new GrammarException("Must specify a name for a loaded object"); + throw new GrammarException("Must specify a name for a loaded " + tag); } String name = parts[2]; @@ -117,7 +116,11 @@ public class ConfigLoader { break; case "subset": { - /* @TODO implement subset grammars */ + /* + *@TODO Ben Culkin 9/8/17 :SubsetGrammar + * + * Implement subset grammars. + */ throw new GrammarException("Sub-grammar sets aren't implemented yet"); } case "gram": @@ -125,8 +128,7 @@ public class ConfigLoader { loadGrammar(name, parts, cfgSet, set, cfgParent); break; default: - String msg = String.format("Unrecognized tag type '%s'", tag); - throw new GrammarException(msg); + throw new GrammarException(String.format("Unrecognized tag type '%s'", tag)); } } @@ -144,7 +146,7 @@ public class ConfigLoader { Path convPath = cfgParent.resolve(path.toString()); if(Files.isDirectory(convPath)) { - throw new GrammarException("Can't load grammar from directory" + convPath.toString()); + throw new GrammarException(String.format("%s is not a valid grammar file", convPath)); } else { /* Load template file. */ try { @@ -155,7 +157,7 @@ public class ConfigLoader { template.belongsTo = cfgSet; if(template.name == null) { - System.err.printf("\tINFO: Naming unnamed template loaded from %s off config name '%s'\n", + info("Naming unnamed template loaded from path %s off config name '%s'", convPath, name); template.name = name; @@ -167,7 +169,7 @@ public class ConfigLoader { long fileTime = endFileTime - startFileTime; - System.err.printf("\tPERF: Read template %s (from %s) in %d ns (%f s)\n", + perf("Read template %s (from %s) in %d ns (%f s)", template.name, convPath, fileTime, fileTime / 1000000000.0); /* Add grammar to the set. */ @@ -176,17 +178,14 @@ public class ConfigLoader { /* * @NOTE * - * Do we need to do this - * for templates? + * Do we need to do this for templates? * - * Mark where the - * template came - * from. */ + //Mark where the template came from. //set.loadedFrom.put(name, path.toString()); } catch (GrammarException gex) { String msg = String.format("Error loading template file '%s'", path); - throw new GrammarException(msg, gex); + throw new GrammarException(msg, gex, gex.getRootMessage()); } } } @@ -205,7 +204,7 @@ public class ConfigLoader { Path convPath = cfgParent.resolve(path.toString()); if(Files.isDirectory(convPath)) { - throw new GrammarException("Can't load grammar from directory" + convPath.toString()); + throw new GrammarException(String.format("%s is not a valid grammar file", convPath)); } else { /* Load grammar file. */ try { @@ -214,7 +213,7 @@ public class ConfigLoader { BufferedReader fis = Files.newBufferedReader(convPath); RGrammar gram = RGrammarParser.readGrammar(fis); if(gram.name == null) { - System.err.printf("\tINFO: Naming unnamed grammar loaded from %s off config name '%s'\n", + info("Naming unnamed grammar loaded from %s off config name '%s'", convPath, name); gram.name = name; @@ -226,7 +225,7 @@ public class ConfigLoader { long fileTime = endFileTime - startFileTime; - System.err.printf("\tPERF: Read grammar %s (from %s) in %d ns (%f s)\n", + perf("Read grammar %s (from %s) in %d ns (%f s)", gram.name, convPath, fileTime, fileTime / 1000000000.0); /* Add grammar to the set. */ @@ -237,10 +236,14 @@ public class ConfigLoader { * from. */ set.loadedFrom.put(name, path.toString()); - } catch (IOException | GrammarException ex) { + } catch(GrammarException gex) { + String msg = String.format("Error loading grammar '%s'", path); + throw new GrammarException(msg, gex, gex.getRootMessage()); + } catch (IOException ioex) { String msg = String.format("Error loading grammar '%s'", path); - throw new GrammarException(msg, ex); + throw new GrammarException(msg, ioex); } + } } } diff --git a/src/main/java/bjc/rgens/parser/GrammarException.java b/src/main/java/bjc/rgens/parser/GrammarException.java index 9eaa0a1..ea98206 100755 --- a/src/main/java/bjc/rgens/parser/GrammarException.java +++ b/src/main/java/bjc/rgens/parser/GrammarException.java @@ -10,6 +10,8 @@ public class GrammarException extends RuntimeException { /* Serialization ID. */ private static final long serialVersionUID = -7287427479316953668L; + private String rootMessage; + /** * Create a new grammar exception with the specified message. * @@ -33,4 +35,36 @@ public class GrammarException extends RuntimeException { public GrammarException(String msg, Exception cause) { super(msg, cause); } + + /** + * Create a new grammar exception with the specified message. + * + * @param msg + * The message for this exception. + */ + public GrammarException(String msg, String rootMsg) { + super(msg); + + this.rootMessage = rootMsg; + } + + /** + * Create a new grammar exception with the specified message and + * cause. + * + * @param msg + * The message for this exception. + * + * @param cause + * The cause of this exception. + */ + public GrammarException(String msg, Exception cause, String rootMsg) { + super(msg, cause); + + this.rootMessage = rootMsg; + } + + public String getRootMessage() { + return rootMessage == null? getMessage() : rootMessage; + } } diff --git a/src/main/java/bjc/rgens/parser/RGrammar.java b/src/main/java/bjc/rgens/parser/RGrammar.java index 337ab35..5e3142b 100755 --- a/src/main/java/bjc/rgens/parser/RGrammar.java +++ b/src/main/java/bjc/rgens/parser/RGrammar.java @@ -261,7 +261,7 @@ public class RGrammar { start.generate(state); } catch (GrammarException gex) { String msg = String.format("Error in generating case (%s)", start); - throw new GrammarException(msg, gex); + throw new GrammarException(msg, gex, gex.getRootMessage()); } } diff --git a/src/main/java/bjc/rgens/parser/RGrammarBuilder.java b/src/main/java/bjc/rgens/parser/RGrammarBuilder.java index 4d4157e..c94af9a 100755 --- a/src/main/java/bjc/rgens/parser/RGrammarBuilder.java +++ b/src/main/java/bjc/rgens/parser/RGrammarBuilder.java @@ -17,6 +17,8 @@ import java.util.LinkedList; import java.util.Map; import java.util.Set; +import static bjc.rgens.parser.RGrammarLogging.*; + /** * Construct randomized grammars piece by piece. * @@ -74,7 +76,7 @@ public class RGrammarBuilder { if(initialRule != null) { if(!rules.containsKey(initialRule)) { - throw new GrammarException(String.format("Rule '%s' doesn't exist\n", initialRule)); + throw new GrammarException(String.format("Rule '%s' doesn't exist", initialRule)); } } @@ -82,7 +84,7 @@ public class RGrammarBuilder { for(String export : exportedRules) { if(!rules.containsKey(export)) { - throw new GrammarException(String.format("Rule '%s' doesn't exist\n", export)); + throw new GrammarException(String.format("Rule '%s' doesn't exist", export)); } } @@ -262,7 +264,7 @@ public class RGrammarBuilder { newCaseList.add(new Pair<>(cse.getLeft(), new FlatRuleCase(cse.getRight().elementList))); } - System.err.printf("\t\tTRACE: Despacing %d cases of rule %s\n", caseList.getSize(), ruleName); + trace("Despacing %d cases of rule %s", caseList.getSize(), ruleName); rules.get(ruleName).replaceCases(newCaseList); } @@ -317,16 +319,17 @@ public class RGrammarBuilder { Rule rl = rules.get(ruleName); - rl.prob = Rule.ProbType.BINOMIAL; + rl.prob = Rule.ProbType.BINOMIAL; rl.target = target; rl.bound = bound; rl.trials = trials; } + /* * @TODO * - * Actually get this working + * Figure out how this should work, and get this working. */ /* public void regexizeRule(String rule, String pattern) { @@ -355,7 +358,7 @@ public class RGrammarBuilder { * @NOTE * * This should be moved into its own class somewhere, as it is general - * eneough. + * enough. */ private static <T> List<List<T>> powerList(Set<T> elements) { /* @@ -391,7 +394,7 @@ public class RGrammarBuilder { } for(List<T> permute : ListUtils.permuteList(stor)) { - System.err.printf("\t\tTRACE: generated permute "); + //System.err.printf("\t\tTRACE: generated permute "); for(T elm : permute) { System.err.printf("%s ", elm); } diff --git a/src/main/java/bjc/rgens/parser/RGrammarFormatter.java b/src/main/java/bjc/rgens/parser/RGrammarFormatter.java index c571cb7..b4cb00e 100755 --- a/src/main/java/bjc/rgens/parser/RGrammarFormatter.java +++ b/src/main/java/bjc/rgens/parser/RGrammarFormatter.java @@ -10,6 +10,11 @@ import java.util.Map; import java.util.Set; /** + * @TODO Ben Culkin 9/8/18 :GrammarFormatter + * + * Update this to suppor the new features when possible. + */ +/** * Format randomized grammars to strings properly. * * @author EVE diff --git a/src/main/java/bjc/rgens/parser/RGrammarLogging.java b/src/main/java/bjc/rgens/parser/RGrammarLogging.java new file mode 100644 index 0000000..4dba117 --- /dev/null +++ b/src/main/java/bjc/rgens/parser/RGrammarLogging.java @@ -0,0 +1,50 @@ +package bjc.rgens.parser; + +/** + * Handle logging in a centralized way, so as to allow easier collation of the + * output. + * + * @author Ben Culkin + */ +public final class RGrammarLogging { + public static void log(String msg, Object... vars) { + System.err.printf(msg, vars); + } + + public static void logline(String msg, Object... vars) { + log(msg + "\n", vars); + } + + public static void error(Exception ex, String msg, Object... vars) { + logline("ERROR: " + msg, vars); + + ex.printStackTrace(); + + logline(""); + logline(""); + } + + public static void perf(String msg, Object... vars) { + logline("\tPERF: " + msg, vars); + } + + public static void info(String msg, Object... vars) { + logline("INFO: " + msg, vars); + } + + public static void trace(String msg, Object... vars) { + logline("\t\tTRACE: " + msg, vars); + } + + public static void warn(String msg, Object... vars) { + logline("WARN: " + msg, vars); + } + + public static void debug(String msg, Object... vars) { + logline("\tDEBUG: " + msg, vars); + } + + public static void fine(String msg, Object... vars) { + logline("\t\tFINE: " + msg, vars); + } +} diff --git a/src/main/java/bjc/rgens/parser/RGrammarParser.java b/src/main/java/bjc/rgens/parser/RGrammarParser.java index a1fc0e9..dae46fc 100755 --- a/src/main/java/bjc/rgens/parser/RGrammarParser.java +++ b/src/main/java/bjc/rgens/parser/RGrammarParser.java @@ -151,6 +151,7 @@ public class RGrammarParser { String name = body.substring(0, nameIndex).trim(); String patt = body.substring(nameIndex + 1).trim(); + throw new GrammarException("Regexize-rule pragma not yet supported"); //build.regexizeRule(name, patt); }); @@ -215,10 +216,10 @@ public class RGrammarParser { return build.toRGrammar(); } catch (GrammarException gex) { String msg = String.format("Error in block (%s)", reader.getBlock()); - throw new GrammarException(msg, gex); + throw new GrammarException(msg, gex, gex.getRootMessage()); } } catch (Exception ex) { - throw new GrammarException("Unknown error handling block", ex); + throw new GrammarException("Unknown error handling block", ex, ex.getMessage()); } } @@ -301,10 +302,10 @@ public class RGrammarParser { Block pragma = pragmaReader.getBlock(); String msg = String.format("Error in pragma: (%s)", pragma); - throw new GrammarException(msg, gex); + throw new GrammarException(msg, gex, gex.getRootMessage()); } } catch (Exception ex) { - throw new GrammarException("Unknown error handling pragma block", ex); + throw new GrammarException("Unknown error handling pragma block", ex, ex.getMessage()); } } @@ -365,10 +366,10 @@ public class RGrammarParser { } catch (GrammarException gex) { String msg = String.format("Error in rule case (%s)", ruleReader.getBlock()); - throw new GrammarException(msg, gex); + throw new GrammarException(msg, gex, gex.getRootMessage()); } } catch (Exception ex) { - throw new GrammarException("Unknown error handling rule block", ex); + throw new GrammarException("Unknown error handling rule block", ex, ex.getMessage()); } } @@ -453,10 +454,10 @@ public class RGrammarParser { } } catch (GrammarException gex) { throw new GrammarException(String.format("Error in where block (%s)", - whereReader.getBlock()), gex); + whereReader.getBlock()), gex, gex.getRootMessage()); } } catch (Exception ex) { - throw new GrammarException("Unknown error in where block", ex); + throw new GrammarException("Unknown error in where block", ex, ex.getMessage()); } } diff --git a/src/main/java/bjc/rgens/parser/RGrammarSet.java b/src/main/java/bjc/rgens/parser/RGrammarSet.java index 783ca26..30f08a4 100755 --- a/src/main/java/bjc/rgens/parser/RGrammarSet.java +++ b/src/main/java/bjc/rgens/parser/RGrammarSet.java @@ -5,6 +5,8 @@ import java.util.Map; import java.util.TreeMap; import java.util.Set; +import static bjc.rgens.parser.RGrammarLogging.*; + /** * Represents a set of grammars that can share rules via exports. * @@ -26,6 +28,7 @@ public class RGrammarSet { /* Contains which file a grammar was loaded from. */ public Map<String, String> loadedFrom; + /* @NOTE These are replaced by the logging setup */ public static final boolean PERF = true; public static final boolean DEBUG = true; @@ -71,12 +74,12 @@ public class RGrammarSet { /* Process exports from the grammar. */ for (Rule export : gram.getExportedRules()) { if(exportedRules.containsKey(export.name)) - System.err.printf("WARN: Shadowing rule %s in %s from %s\n", export.name, export.belongsTo.name, grammarName); + warn("Shadowing rule %s in %s from %s", export.name, export.belongsTo.name, grammarName); exportedRules.put(export.name, export); if(DEBUG) - System.err.printf("\t\tDEBUG: %s (%d cases) exported from %s\n", export.name, export.getCases().getSize(), grammarName); + debug("%s (%d cases) exported from %s", export.name, export.getCases().getSize(), grammarName); } /* Add exports to grammar. */ diff --git a/src/main/java/bjc/rgens/parser/RGrammarTest.java b/src/main/java/bjc/rgens/parser/RGrammarTest.java index 8193fa3..056ffd9 100755 --- a/src/main/java/bjc/rgens/parser/RGrammarTest.java +++ b/src/main/java/bjc/rgens/parser/RGrammarTest.java @@ -9,6 +9,8 @@ import java.nio.file.Path; import java.nio.file.Paths; import java.util.Random; +import static bjc.rgens.parser.RGrammarLogging.*; + /** * Test for new grammar syntax. * @@ -74,7 +76,7 @@ public class RGrammarTest { long suggDur = endSuggTime - startSuggTime; - System.err.printf("PERF: Generated rule suggestions for %s in %d ns (%f s)\n", gramName, suggDur, suggDur / 1000000000.0); + perf("Generated rule suggestions for %s in %d ns (%f s)", gramName, suggDur, suggDur / 1000000000.0); } System.err.printf("\n\n"); @@ -101,24 +103,20 @@ public class RGrammarTest { } } catch (GrammarException gex) { /* Print out errors with generation. */ - String fmt = "ERROR: Exported rule %s from %s failed (loaded from '%s')\n"; + String fmt = "Exported rule %s from %s failed (loaded from '%s')"; - System.out.printf(fmt, exportName, grammar.name, loadSrc); + System.out.printf("ERROR: " + fmt, exportName, grammar.name, loadSrc); System.out.println(); System.out.println(); - System.err.printf(fmt, exportName, grammar.name, loadSrc); - gex.printStackTrace(); - - System.err.println(); - System.err.println(); + error(gex, fmt, exportName, grammar.name, loadSrc); } } long endGenTime = System.nanoTime(); long genDur = endGenTime - startGenTime; - System.err.printf("PERF: Generated %s 100 times in %d ns (%f s)\n\n\n", exportName, genDur, genDur / 1000000000.0); + perf("Generated %s 100 times in %d ns (%f s)", exportName, genDur, genDur / 1000000000.0); } } } diff --git a/src/main/java/bjc/rgens/parser/Rule.java b/src/main/java/bjc/rgens/parser/Rule.java index ac67158..5c43d5c 100755 --- a/src/main/java/bjc/rgens/parser/Rule.java +++ b/src/main/java/bjc/rgens/parser/Rule.java @@ -7,6 +7,8 @@ import bjc.utils.gen.WeightedRandom; import java.util.Random; +import static bjc.rgens.parser.RGrammarLogging.*; + /** * A rule in a randomized grammar. * @@ -230,7 +232,7 @@ public class Rule { if(doRecur()) { RuleCase cse = getCase(state.rnd); - System.err.printf("\tFINE: Generating %s (from %s)\n", cse, belongsTo.name); + fine("FINE: Generating %s (from %s)", cse, belongsTo.name); belongsTo.generateCase(cse, state); diff --git a/src/main/java/bjc/rgens/parser/elements/CaseElement.java b/src/main/java/bjc/rgens/parser/elements/CaseElement.java index 386e2b4..fa398e7 100755 --- a/src/main/java/bjc/rgens/parser/elements/CaseElement.java +++ b/src/main/java/bjc/rgens/parser/elements/CaseElement.java @@ -7,6 +7,8 @@ import bjc.rgens.parser.GrammarException; import java.util.Arrays; +import static bjc.rgens.parser.RGrammarLogging.*; + /** * A element in a rule case. * @@ -93,7 +95,7 @@ public abstract class CaseElement { char op = varName.charAt(varName.length() - 1); - System.err.printf("\t\tTRACE: Colon definition w/ op %d", (int)op); + trace("Colon definition w/ op %d", (int)op); // Remove the colon, plus any tacked on operator varName = varName.substring(0, varName.length() - 2); @@ -138,7 +140,7 @@ public abstract class CaseElement { return new InlineRuleCaseElement(elms); } else if(rawCase.contains("|")) { - throw new GrammarException("\t\tERROR: Inline rule using | found, they use || now"); + throw new GrammarException("Inline rule using | found, they use || now"); // String[] elms = StringUtils.levelSplit(rawCase, "|").toArray(new String[0]); // return new InlineRuleCaseElement(elms); diff --git a/src/main/java/bjc/rgens/parser/elements/RuleVariableCaseElement.java b/src/main/java/bjc/rgens/parser/elements/RuleVariableCaseElement.java index 29b6fc9..d11915e 100644 --- a/src/main/java/bjc/rgens/parser/elements/RuleVariableCaseElement.java +++ b/src/main/java/bjc/rgens/parser/elements/RuleVariableCaseElement.java @@ -8,6 +8,8 @@ import bjc.rgens.parser.GenerationState; import bjc.rgens.parser.Rule; import bjc.rgens.parser.RGrammar; +import static bjc.rgens.parser.RGrammarLogging.*; + public class RuleVariableCaseElement extends VariableDefCaseElement { public final boolean exhaust; @@ -31,9 +33,9 @@ public class RuleVariableCaseElement extends VariableDefCaseElement { state.rlVars.put(varName, rl); if(exhaust) { - System.err.printf("\t\tFINE: Defined exhausted rulevar '%s' ('%s')\n", varName, varDef); + fine("Defined exhausted rulevar '%s' ('%s')", varName, varDef); } else { - System.err.printf("\t\tFINE: Defined rulevar '%s' ('%s')\n", varName, varDef); + fine("Defined rulevar '%s' ('%s')", varName, varDef); } } } |
