From 1e5d6110c264af7c4a51927685e5d50637d9b9e9 Mon Sep 17 00:00:00 2001 From: Dmitry Vasilevsky Date: Thu, 28 Jan 2021 13:25:19 -0800 Subject: [PATCH 1/3] Making stack trace printout optional --- src/Simulation/Common/SimulatorBase.cs | 14 ++++++++------ src/Simulation/Simulators.Tests/StackTraceTests.cs | 13 +++++++++++++ 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/src/Simulation/Common/SimulatorBase.cs b/src/Simulation/Common/SimulatorBase.cs index 7d216c00edf..48b80a0e07c 100644 --- a/src/Simulation/Common/SimulatorBase.cs +++ b/src/Simulation/Common/SimulatorBase.cs @@ -43,6 +43,7 @@ public abstract class SimulatorBase : AbstractFactory, IOperat public abstract string Name { get; } + public bool EnableStackTracePrinting { get; set; } = true; /// /// If the execution finishes in failure, this method returns the call-stack of the Q# operations @@ -125,12 +126,13 @@ public override AbstractCallable CreateInstance(Type t) protected void WriteStackTraceToLog(Exception exception, IEnumerable callStack) { OnLog?.Invoke($"Unhandled exception. {exception.GetType().FullName}: {exception.Message}"); - var first = true; - foreach (var sf in callStack) - { - var msg = (first ? " ---> " : " at ") + sf.ToStringWithBestSourceLocation(); - OnLog?.Invoke(msg); - first = false; + if (EnableStackTracePrinting) { + var first = true; + foreach (var sf in callStack) { + var msg = (first ? " ---> " : " at ") + sf.ToStringWithBestSourceLocation(); + OnLog?.Invoke(msg); + first = false; + } } OnLog?.Invoke(""); } diff --git a/src/Simulation/Simulators.Tests/StackTraceTests.cs b/src/Simulation/Simulators.Tests/StackTraceTests.cs index c3fead7b586..e429475cfb9 100644 --- a/src/Simulation/Simulators.Tests/StackTraceTests.cs +++ b/src/Simulation/Simulators.Tests/StackTraceTests.cs @@ -381,6 +381,19 @@ public void ErrorLogTest() Assert.StartsWith(" at Microsoft.Quantum.Simulation.Simulators.Tests.Circuits.AlwaysFail4 on", logs[5]); Assert.Equal("", logs[6]); } + + logs.Clear(); + sim.EnableStackTracePrinting = false; + try + { + QVoid res = sim.Execute(QVoid.Instance); + } + catch (ExecutionFailException) + { + Assert.Equal(2, logs.Count); + Assert.StartsWith("Unhandled exception. Microsoft.Quantum.Simulation.Core.ExecutionFailException: Always fail", logs[0]); + Assert.Equal("", logs[1]); + } } } } \ No newline at end of file From c1b60fe7dbce1528b2b389e8e53e8407f7e610a2 Mon Sep 17 00:00:00 2001 From: Dmitry Vasilevsky Date: Thu, 28 Jan 2021 13:31:34 -0800 Subject: [PATCH 2/3] Adjusted brace indentation --- src/Simulation/Common/SimulatorBase.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Simulation/Common/SimulatorBase.cs b/src/Simulation/Common/SimulatorBase.cs index 48b80a0e07c..21b4097e5c5 100644 --- a/src/Simulation/Common/SimulatorBase.cs +++ b/src/Simulation/Common/SimulatorBase.cs @@ -126,9 +126,11 @@ public override AbstractCallable CreateInstance(Type t) protected void WriteStackTraceToLog(Exception exception, IEnumerable callStack) { OnLog?.Invoke($"Unhandled exception. {exception.GetType().FullName}: {exception.Message}"); - if (EnableStackTracePrinting) { + if (EnableStackTracePrinting) + { var first = true; - foreach (var sf in callStack) { + foreach (var sf in callStack) + { var msg = (first ? " ---> " : " at ") + sf.ToStringWithBestSourceLocation(); OnLog?.Invoke(msg); first = false; From 09c92c74a4d72e3ad7d5da97cd1c1e4069ee3d5e Mon Sep 17 00:00:00 2001 From: Dmitry Vasilevsky Date: Thu, 28 Jan 2021 18:00:16 -0800 Subject: [PATCH 3/3] Added comment --- src/Simulation/Common/SimulatorBase.cs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Simulation/Common/SimulatorBase.cs b/src/Simulation/Common/SimulatorBase.cs index 21b4097e5c5..f91ef88d78a 100644 --- a/src/Simulation/Common/SimulatorBase.cs +++ b/src/Simulation/Common/SimulatorBase.cs @@ -43,6 +43,11 @@ public abstract class SimulatorBase : AbstractFactory, IOperat public abstract string Name { get; } + /// + /// When exception printing is enabled, the value of this property controls stack trace printout. + /// When set to true (default), the stack trace is printed in addition to the exception message." + /// When set to false, the exception message is printed without the stack trace." + /// public bool EnableStackTracePrinting { get; set; } = true; ///