Skip to content

Commit

Permalink
Merge pull request #685 from mchadalavada/master
Browse files Browse the repository at this point in the history
Add option to enable/disable debug logs in pobserve mode
  • Loading branch information
mchadalavada authored Dec 1, 2023
2 parents 19f9e97 + 2d26902 commit e49a7b1
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 3 deletions.
3 changes: 2 additions & 1 deletion Src/PCompiler/CompilerCore/Backend/Java/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ internal static class Constants
private static readonly string[] JreDefaultImports =
{
"java.io.Serializable",
"java.util.*"
"java.util.*",
"java.util.logging.*"
};

/// <summary>
Expand Down
10 changes: 9 additions & 1 deletion Src/PCompiler/CompilerCore/Backend/Java/MachineGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Function> _calledStaticFunctions = new HashSet<Function>(); // static functions allowed
private bool debug = false;

internal MachineGenerator(ICompilerConfiguration job, string filename) : base(job, filename)
{
debug = job.Debug;
}

/// <summary>
Expand All @@ -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)
{
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 5 additions & 1 deletion Src/PCompiler/CompilerCore/CompilerConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ public CompilerConfiguration()
OutputLanguages = new List<CompilerOutput>{CompilerOutput.CSharp};
Backend = null;
ProjectDependencies = new List<string>();
Debug = false;
}
public CompilerConfiguration(ICompilerOutput output, DirectoryInfo outputDir, IList<CompilerOutput> outputLanguages, IList<string> inputFiles,
string projectName, DirectoryInfo projectRoot = null, IList<string> projectDependencies = null, string pObservePackageName = null)
string projectName, DirectoryInfo projectRoot = null, IList<string> projectDependencies = null, string pObservePackageName = null, bool debug = false)
{
if (!inputFiles.Any())
{
Expand Down Expand Up @@ -55,6 +56,7 @@ public CompilerConfiguration(ICompilerOutput output, DirectoryInfo outputDir, IL
OutputLanguages = outputLanguages;
Backend = null;
ProjectDependencies = projectDependencies ?? new List<string>();
Debug = debug;
}

public ICompilerOutput Output { get; set; }
Expand All @@ -70,6 +72,7 @@ public CompilerConfiguration(ICompilerOutput output, DirectoryInfo outputDir, IL
public ITranslationErrorHandler Handler { get; set; }

public IList<string> ProjectDependencies { get; set; }
public bool Debug { get; set; }

public void Copy(CompilerConfiguration parsedConfig)
{
Expand All @@ -85,6 +88,7 @@ public void Copy(CompilerConfiguration parsedConfig)
PObservePackageName = parsedConfig.PObservePackageName;
OutputLanguages = parsedConfig.OutputLanguages;
ProjectRootPath = parsedConfig.ProjectRootPath;
Debug = parsedConfig.Debug;
}
}
}
1 change: 1 addition & 0 deletions Src/PCompiler/CompilerCore/ICompilerConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ public interface ICompilerConfiguration
IList<string> ProjectDependencies { get; }
ILocationResolver LocationResolver { get; }
ITranslationErrorHandler Handler { get; }
bool Debug { get; }
}
}
5 changes: 5 additions & 0 deletions Src/PCompiler/PCommandLine/Options/PCompilerOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ internal PCompilerOptions()
modes.IsHidden = true;

Parser.AddArgument("pobserve-package", "po", "PObserve package name").IsHidden = true;

Parser.AddArgument("debug", "d", "Enable debug logs", typeof(bool)).IsHidden = true;
}

/// <summary>
Expand Down Expand Up @@ -157,6 +159,9 @@ private static void UpdateConfigurationWithParsedArgument(CompilerConfiguration
case "projname":
compilerConfiguration.ProjectName = (string)option.Value;
break;
case "debug":
compilerConfiguration.Debug = true;
break;
case "mode":
compilerConfiguration.OutputLanguages = new List<CompilerOutput>();
switch (((string)option.Value).ToLowerInvariant())
Expand Down

0 comments on commit e49a7b1

Please sign in to comment.