From 09929151b74be5af560e357a7ceb96c714330a9e Mon Sep 17 00:00:00 2001 From: Mounika Chadalavada Date: Fri, 22 Sep 2023 13:36:03 -0700 Subject: [PATCH 1/4] Add logic to remove null key-value pairs in trace json --- .../SystematicTesting/TestingEngine.cs | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/Src/PChecker/CheckerCore/SystematicTesting/TestingEngine.cs b/Src/PChecker/CheckerCore/SystematicTesting/TestingEngine.cs index a72cfbdd8..5396815ca 100644 --- a/Src/PChecker/CheckerCore/SystematicTesting/TestingEngine.cs +++ b/Src/PChecker/CheckerCore/SystematicTesting/TestingEngine.cs @@ -591,6 +591,36 @@ public string GetReport() return TestReport.GetText(_checkerConfiguration, "..."); } + /// + /// Returns an object where the keys with null values are removed + /// + public object RecursivelyRemoveNullValueKeys(object obj) { + if (obj == null) { + return null; + } + if (obj is Dictionary dictionary) { + var newDictionary = new Dictionary(); + foreach (var item in dictionary) { + var newVal = RecursivelyRemoveNullValueKeys(item.Value); + if (newVal != null) + newDictionary[item.Key] = newVal; + } + return newDictionary; + } + else if (obj is List list) { + var newList = new List(); + foreach (var item in list) { + var newItem = RecursivelyRemoveNullValueKeys(item); + if (newItem != null) + newList.Add(newItem); + } + return newList; + } + else { + return obj; + } + } + /// /// Tries to emit the testing traces, if any. /// @@ -640,6 +670,11 @@ public void TryEmitTraces(string directory, string file) var jsonPath = directory + file + "_" + index + ".trace.json"; Logger.WriteLine($"..... Writing {jsonPath}"); + // Remove the null objects from payload recursively for each log event + for(int i=0; i Date: Fri, 22 Sep 2023 13:56:26 -0700 Subject: [PATCH 2/4] Modify logic to replace null with string null --- .../CheckerCore/SystematicTesting/TestingEngine.cs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Src/PChecker/CheckerCore/SystematicTesting/TestingEngine.cs b/Src/PChecker/CheckerCore/SystematicTesting/TestingEngine.cs index 5396815ca..b86a3852a 100644 --- a/Src/PChecker/CheckerCore/SystematicTesting/TestingEngine.cs +++ b/Src/PChecker/CheckerCore/SystematicTesting/TestingEngine.cs @@ -592,16 +592,16 @@ public string GetReport() } /// - /// Returns an object where the keys with null values are removed + /// Returns an object where the value null is replaced with "null" /// - public object RecursivelyRemoveNullValueKeys(object obj) { + public object RecursivelyReplaceNullWithString(object obj) { if (obj == null) { - return null; + return "null"; } if (obj is Dictionary dictionary) { var newDictionary = new Dictionary(); foreach (var item in dictionary) { - var newVal = RecursivelyRemoveNullValueKeys(item.Value); + var newVal = RecursivelyReplaceNullWithString(item.Value); if (newVal != null) newDictionary[item.Key] = newVal; } @@ -610,7 +610,7 @@ public object RecursivelyRemoveNullValueKeys(object obj) { else if (obj is List list) { var newList = new List(); foreach (var item in list) { - var newItem = RecursivelyRemoveNullValueKeys(item); + var newItem = RecursivelyReplaceNullWithString(item); if (newItem != null) newList.Add(newItem); } @@ -672,7 +672,7 @@ public void TryEmitTraces(string directory, string file) // Remove the null objects from payload recursively for each log event for(int i=0; i Date: Thu, 30 Nov 2023 11:59:02 -0800 Subject: [PATCH 3/4] Add option to enable/disable logs in pobserve mode --- Src/PCompiler/CompilerCore/Backend/Java/Constants.cs | 3 ++- .../CompilerCore/Backend/Java/MachineGenerator.cs | 10 +++++++++- Src/PCompiler/CompilerCore/CompilerConfiguration.cs | 6 +++++- Src/PCompiler/CompilerCore/ICompilerConfiguration.cs | 1 + Src/PCompiler/PCommandLine/Options/PCompilerOptions.cs | 6 ++++++ 5 files changed, 23 insertions(+), 3 deletions(-) diff --git a/Src/PCompiler/CompilerCore/Backend/Java/Constants.cs b/Src/PCompiler/CompilerCore/Backend/Java/Constants.cs index b8c883993..e1188713c 100644 --- a/Src/PCompiler/CompilerCore/Backend/Java/Constants.cs +++ b/Src/PCompiler/CompilerCore/Backend/Java/Constants.cs @@ -27,7 +27,8 @@ internal static class Constants private static readonly string[] JreDefaultImports = { "java.io.Serializable", - "java.util.*" + "java.util.*", + "java.util.logging.*" }; /// diff --git a/Src/PCompiler/CompilerCore/Backend/Java/MachineGenerator.cs b/Src/PCompiler/CompilerCore/Backend/Java/MachineGenerator.cs index 5eb9c5ecb..985447938 100644 --- a/Src/PCompiler/CompilerCore/Backend/Java/MachineGenerator.cs +++ b/Src/PCompiler/CompilerCore/Backend/Java/MachineGenerator.cs @@ -16,9 +16,11 @@ internal class MachineGenerator : JavaSourceGenerator private Machine _currentMachine; // Some generated code is machine-dependent, so stash the current machine here. private HashSet _calledStaticFunctions = new HashSet(); // static functions allowed + private bool debug = false; internal MachineGenerator(ICompilerConfiguration job, string filename) : base(job, filename) { + debug = job.Debug; } /// @@ -34,6 +36,12 @@ internal MachineGenerator(ICompilerConfiguration job, string filename) : base(jo protected override void GenerateCodeImpl() { WriteLine($"public class {Constants.MachineNamespaceName} {{"); + WriteLine($"private static Logger logger = Logger.getLogger({Constants.MachineNamespaceName}.class.getName());"); + if (debug) { + WriteLine($"static {{ logger.setLevel(Level.ALL); }};"); + } else { + WriteLine($"static {{ logger.setLevel(Level.OFF); }};"); + } foreach (var m in GlobalScope.Machines) { @@ -462,7 +470,7 @@ private void WriteStmt(IPStmt stmt) break; case PrintStmt printStmt: - Write("System.out.println("); + Write("logger.info("); WriteExpr(printStmt.Message); WriteLine(");"); break; diff --git a/Src/PCompiler/CompilerCore/CompilerConfiguration.cs b/Src/PCompiler/CompilerCore/CompilerConfiguration.cs index 9afd92bef..09423c90b 100644 --- a/Src/PCompiler/CompilerCore/CompilerConfiguration.cs +++ b/Src/PCompiler/CompilerCore/CompilerConfiguration.cs @@ -23,9 +23,10 @@ public CompilerConfiguration() OutputLanguages = new List{CompilerOutput.CSharp}; Backend = null; ProjectDependencies = new List(); + Debug = false; } public CompilerConfiguration(ICompilerOutput output, DirectoryInfo outputDir, IList outputLanguages, IList inputFiles, - string projectName, DirectoryInfo projectRoot = null, IList projectDependencies = null, string pObservePackageName = null) + string projectName, DirectoryInfo projectRoot = null, IList projectDependencies = null, string pObservePackageName = null, bool debug = false) { if (!inputFiles.Any()) { @@ -55,6 +56,7 @@ public CompilerConfiguration(ICompilerOutput output, DirectoryInfo outputDir, IL OutputLanguages = outputLanguages; Backend = null; ProjectDependencies = projectDependencies ?? new List(); + Debug = debug; } public ICompilerOutput Output { get; set; } @@ -70,6 +72,7 @@ public CompilerConfiguration(ICompilerOutput output, DirectoryInfo outputDir, IL public ITranslationErrorHandler Handler { get; set; } public IList ProjectDependencies { get; set; } + public bool Debug { get; set; } public void Copy(CompilerConfiguration parsedConfig) { @@ -85,6 +88,7 @@ public void Copy(CompilerConfiguration parsedConfig) PObservePackageName = parsedConfig.PObservePackageName; OutputLanguages = parsedConfig.OutputLanguages; ProjectRootPath = parsedConfig.ProjectRootPath; + Debug = parsedConfig.Debug; } } } \ No newline at end of file diff --git a/Src/PCompiler/CompilerCore/ICompilerConfiguration.cs b/Src/PCompiler/CompilerCore/ICompilerConfiguration.cs index 36df61c9a..1aa20f9dc 100644 --- a/Src/PCompiler/CompilerCore/ICompilerConfiguration.cs +++ b/Src/PCompiler/CompilerCore/ICompilerConfiguration.cs @@ -19,5 +19,6 @@ public interface ICompilerConfiguration IList ProjectDependencies { get; } ILocationResolver LocationResolver { get; } ITranslationErrorHandler Handler { get; } + bool Debug { get; } } } \ No newline at end of file diff --git a/Src/PCompiler/PCommandLine/Options/PCompilerOptions.cs b/Src/PCompiler/PCommandLine/Options/PCompilerOptions.cs index 3953a69cf..fef0113e8 100644 --- a/Src/PCompiler/PCommandLine/Options/PCompilerOptions.cs +++ b/Src/PCompiler/PCommandLine/Options/PCompilerOptions.cs @@ -44,6 +44,9 @@ internal PCompilerOptions() modes.IsHidden = true; Parser.AddArgument("pobserve-package", "po", "PObserve package name").IsHidden = true; + + var debug = Parser.AddArgument("debug", "d", "Enable or Disable debug logs in pobserve mode (Disabled by default)"); + debug.AllowedValues = new List() { "true", "false" }; } /// @@ -157,6 +160,9 @@ private static void UpdateConfigurationWithParsedArgument(CompilerConfiguration case "projname": compilerConfiguration.ProjectName = (string)option.Value; break; + case "debug": + compilerConfiguration.Debug = bool.Parse((string)option.Value); + break; case "mode": compilerConfiguration.OutputLanguages = new List(); switch (((string)option.Value).ToLowerInvariant()) From 2d26902df9e99658775f6bd84e32569ef272bca8 Mon Sep 17 00:00:00 2001 From: Mounika Chadalavada Date: Fri, 1 Dec 2023 12:22:15 -0800 Subject: [PATCH 4/4] Make debug option boolean type --- Src/PCompiler/PCommandLine/Options/PCompilerOptions.cs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Src/PCompiler/PCommandLine/Options/PCompilerOptions.cs b/Src/PCompiler/PCommandLine/Options/PCompilerOptions.cs index fef0113e8..3c03f7187 100644 --- a/Src/PCompiler/PCommandLine/Options/PCompilerOptions.cs +++ b/Src/PCompiler/PCommandLine/Options/PCompilerOptions.cs @@ -45,8 +45,7 @@ internal PCompilerOptions() Parser.AddArgument("pobserve-package", "po", "PObserve package name").IsHidden = true; - var debug = Parser.AddArgument("debug", "d", "Enable or Disable debug logs in pobserve mode (Disabled by default)"); - debug.AllowedValues = new List() { "true", "false" }; + Parser.AddArgument("debug", "d", "Enable debug logs", typeof(bool)).IsHidden = true; } /// @@ -161,7 +160,7 @@ private static void UpdateConfigurationWithParsedArgument(CompilerConfiguration compilerConfiguration.ProjectName = (string)option.Value; break; case "debug": - compilerConfiguration.Debug = bool.Parse((string)option.Value); + compilerConfiguration.Debug = true; break; case "mode": compilerConfiguration.OutputLanguages = new List();