Skip to content

Commit 5374caa

Browse files
authored
SWEEP: PrintStackTrace cleanup, #932 (#1101)
* SWEEP: Exception.StackTrace cleanup, #932 * Add unit tests for StackTraceHelper, #932 * Better assert for PrintCurrentStackTrace that caller is the top line * PR feedback * Add string.Contains extension method for .NET Framework and Standard 2.0 * Use MemoryExtensions.Contains * Revert test code to use extension method * Use is null
1 parent dcfa0e2 commit 5374caa

File tree

70 files changed

+645
-352
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+645
-352
lines changed

Directory.Build.targets

+8-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@
8080
<DebugType>portable</DebugType>
8181
</PropertyGroup>
8282

83-
<!-- Features in .NET Standard 2.1, .NET 5.x, .NET 6.x, .NET 7.x, .NET 8.x, and .NET 9.x only -->
83+
<!-- Features in .NET Standard 2.1, .NET Core 3.x, .NET 5.x, .NET 6.x, .NET 7.x, .NET 8.x, and .NET 9.x only -->
8484
<PropertyGroup Condition=" '$(TargetFramework)' == 'netstandard2.1' Or $(TargetFramework.StartsWith('netcoreapp3.')) Or $(TargetFramework.StartsWith('net5.')) Or $(TargetFramework.StartsWith('net6.')) Or $(TargetFramework.StartsWith('net7.')) Or $(TargetFramework.StartsWith('net8.')) Or $(TargetFramework.StartsWith('net9.')) ">
8585

8686
<DefineConstants>$(DefineConstants);FEATURE_ARRAY_FILL</DefineConstants>
@@ -93,6 +93,13 @@
9393

9494
</PropertyGroup>
9595

96+
<!-- Features in .NET Standard 2.1, .NET Core 2.1-2.2, .NET Core 3.x, .NET 5.x, .NET 6.x, .NET 7.x, .NET 8.x, and .NET 9.x only -->
97+
<PropertyGroup Condition=" '$(TargetFramework)' == 'netstandard2.1' Or '$(TargetFramework)' == 'netcoreapp2.1' Or '$(TargetFramework)' == 'netcoreapp2.2' Or $(TargetFramework.StartsWith('netcoreapp3.')) Or $(TargetFramework.StartsWith('net5.')) Or $(TargetFramework.StartsWith('net6.')) Or $(TargetFramework.StartsWith('net7.')) Or $(TargetFramework.StartsWith('net8.')) Or $(TargetFramework.StartsWith('net9.')) ">
98+
99+
<DefineConstants>$(DefineConstants);FEATURE_STRING_CONTAINS_STRINGCOMPARISON</DefineConstants>
100+
101+
</PropertyGroup>
102+
96103
<!-- Features in .NET Standard 2.x, .NET Core 2.x, .NET Core 3.x, .NET 5.x, .NET 6.x, .NET 7.x, .NET 8.x, and .NET 9.x -->
97104
<PropertyGroup Condition=" $(TargetFramework.StartsWith('netstandard2.')) Or $(TargetFramework.StartsWith('netcoreapp2.')) Or $(TargetFramework.StartsWith('netcoreapp3.')) Or $(TargetFramework.StartsWith('net5.')) Or $(TargetFramework.StartsWith('net6.')) Or $(TargetFramework.StartsWith('net7.')) Or $(TargetFramework.StartsWith('net8.')) Or $(TargetFramework.StartsWith('net9.')) ">
98105

src/Lucene.Net.Benchmark/ByTask/Benchmark.cs

+3-4
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,7 @@ public Benchmark(TextReader algReader)
5858
}
5959
catch (Exception e) when (e.IsException())
6060
{
61-
//e.printStackTrace();
62-
Console.Error.WriteLine(e.ToString());
61+
e.PrintStackTrace();
6362
throw new Exception("Error: cannot init PerfRunData!", e);
6463
}
6564

@@ -145,7 +144,7 @@ public static void Exec(string[] args)
145144
}
146145
catch (Exception e) when (e.IsException())
147146
{
148-
Console.Error.WriteLine(e.ToString());
147+
e.PrintStackTrace();
149148
Environment.Exit(1);
150149
}
151150

@@ -160,7 +159,7 @@ public static void Exec(string[] args)
160159
catch (Exception e) when (e.IsException())
161160
{
162161
Console.Error.WriteLine("Error: cannot execute the algorithm! " + e.Message);
163-
Console.Error.WriteLine(e.ToString());
162+
e.PrintStackTrace();
164163
}
165164

166165
Console.WriteLine("####################");

src/Lucene.Net.Benchmark/ByTask/Feeds/EnwikiQueryMaker.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ private static Query[] CreateQueries(IList<object> qs, Analyzer a)
123123
}
124124
catch (Exception e) when (e.IsException())
125125
{
126-
Console.Error.WriteLine(e.ToString());
126+
e.PrintStackTrace();
127127
}
128128
}
129129

src/Lucene.Net.Benchmark/ByTask/Feeds/ReutersQueryMaker.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ private static Query[] CreateQueries(IList<object> qs, Analyzer a)
103103
}
104104
catch (Exception e) when (e.IsException())
105105
{
106-
Console.Error.WriteLine(e.ToString());
106+
e.PrintStackTrace();
107107
}
108108
}
109109

src/Lucene.Net.Benchmark/ByTask/Feeds/TrecContentSource.cs

+7-7
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ namespace Lucene.Net.Benchmarks.ByTask.Feeds
3838
/// <list type="bullet">
3939
/// <item><term>work.dir</term><description>specifies the working directory. Required if "docs.dir"
4040
/// denotes a relative path (<b>default=work</b>).</description></item>
41-
/// <item><term>docs.dir</term><description>specifies the directory where the TREC files reside.
41+
/// <item><term>docs.dir</term><description>specifies the directory where the TREC files reside.
4242
/// Can be set to a relative path if "work.dir" is also specified
4343
/// (<b>default=trec</b>).
4444
/// </description></item>
@@ -161,7 +161,7 @@ private void Read(StringBuilder buf, string lineStart,
161161
internal virtual void OpenNextFile()
162162
{
163163
DoClose();
164-
//currPathType = null;
164+
//currPathType = null;
165165
while (true)
166166
{
167167
if (nextFile >= inputFiles.Count)
@@ -181,7 +181,7 @@ internal virtual void OpenNextFile()
181181
}
182182
try
183183
{
184-
Stream inputStream = StreamUtils.GetInputStream(f); // support either gzip, bzip2, or regular text file, by extension
184+
Stream inputStream = StreamUtils.GetInputStream(f); // support either gzip, bzip2, or regular text file, by extension
185185
reader = new StreamReader(inputStream, m_encoding);
186186
currPathType = TrecDocParser.PathType(f);
187187
return;
@@ -234,7 +234,7 @@ private void DoClose() // LUCENENET specific - separate disposing from closing s
234234
if (m_verbose)
235235
{
236236
Console.WriteLine("failed to dispose reader !");
237-
Console.WriteLine(e.ToString());
237+
e.PrintStackTrace(Console.Out);
238238
}
239239
}
240240
reader = null;
@@ -275,7 +275,7 @@ public override DocData GetNextDocData(DocData docData)
275275
docBuf.Length = 0;
276276
Read(docBuf, DOC, false, false);
277277

278-
// save parsedFile for passing trecDataParser after the sync block, in
278+
// save parsedFile for passing trecDataParser after the sync block, in
279279
// case another thread will open another file in between.
280280
parsedPathType = currPathType;
281281

@@ -302,7 +302,7 @@ public override DocData GetNextDocData(DocData docData)
302302
// count char length of text to be parsed (may be larger than the resulted plain doc body text).
303303
AddBytes(docBuf.Length);
304304

305-
// This code segment relies on HtmlParser being thread safe. When we get
305+
// This code segment relies on HtmlParser being thread safe. When we get
306306
// here, everything else is already private to that thread, so we're safe.
307307
docData = trecDocParser.Parse(docData, name, this, docBuf, parsedPathType);
308308
AddItem();
@@ -367,7 +367,7 @@ public override void SetConfig(Config config)
367367
{
368368
m_encoding = Encoding.GetEncoding("iso-8859-1"); //StandardCharsets.ISO_8859_1.name();
369369
}
370-
// iteration exclusion in doc name
370+
// iteration exclusion in doc name
371371
excludeDocnameIteration = config.Get("content.source.excludeIteration", false);
372372
}
373373
}

src/Lucene.Net.Misc/Index/CompoundFileExtractor.cs

+2-4
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,7 @@ public static void Main(string[] args)
144144
}
145145
catch (Exception ioe) when (ioe.IsIOException())
146146
{
147-
Console.Error.WriteLine(ioe.ToString());
148-
//Console.Write(ioe.StackTrace);
147+
ioe.PrintStackTrace();
149148
}
150149
finally
151150
{
@@ -162,8 +161,7 @@ public static void Main(string[] args)
162161
}
163162
catch (Exception ioe) when (ioe.IsIOException())
164163
{
165-
Console.Error.WriteLine(ioe.ToString());
166-
//Console.Write(ioe.StackTrace);
164+
ioe.PrintStackTrace();
167165
}
168166
}
169167
}

src/Lucene.Net.Replicator/ReplicationClient.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,8 @@ private void WriteToInfoStream(string message)
291291
/// </remarks>
292292
protected virtual void HandleUpdateException(Exception exception)
293293
{
294-
WriteToInfoStream(string.Format("an error occurred during revision update: {0}", exception));
294+
// LUCENENET specific - use utility method, and print full exception to be equivalent to printStackTrace in Java
295+
WriteToInfoStream($"an error occurred during revision update: {exception}");
295296
}
296297

297298
/// <summary>

src/Lucene.Net.TestFramework/Analysis/BaseTokenStreamTestCase.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using Lucene.Net.Documents;
66
using Lucene.Net.Index;
77
using Lucene.Net.Support;
8-
using Lucene.Net.Support.Threading;
98
using Lucene.Net.Util;
109
using RandomizedTesting.Generators;
1110
using System;
@@ -545,7 +544,7 @@ internal static void CheckResetException(Analyzer a, string input)
545544
}
546545
catch (Exception unexpected) when (unexpected.IsException())
547546
{
548-
unexpected.printStackTrace(Console.Error);
547+
unexpected.PrintStackTrace(Console.Error);
549548
Assert.Fail("Got wrong exception when Reset() not called: " + unexpected);
550549
}
551550
finally

src/Lucene.Net.TestFramework/Codecs/Lucene3x/PreFlexRWPostingsFormat.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Lucene.Net.Index;
2+
using Lucene.Net.Support;
23
using Lucene.Net.Util;
34
using Console = Lucene.Net.Util.SystemConsole;
45

@@ -81,4 +82,4 @@ protected override bool SortTermsByUnicode
8182
}
8283
}
8384
#pragma warning restore 612, 618
84-
}
85+
}

src/Lucene.Net.TestFramework/Codecs/Lucene3x/PreFlexRWTermVectorsFormat.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Lucene.Net.Index;
22
using Lucene.Net.Store;
3+
using Lucene.Net.Support;
34
using Lucene.Net.Util;
45
using System;
56

@@ -65,4 +66,4 @@ protected internal override bool SortTermsByUnicode()
6566
}
6667
}
6768
#pragma warning restore 612, 618
68-
}
69+
}

src/Lucene.Net.TestFramework/Index/ThreadedIndexingAndSearchingTestCase.cs

+11-12
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,10 @@ protected virtual void DoAfterIndexingThreadDone()
121121
{
122122
}
123123

124-
private ThreadJob[] LaunchIndexingThreads(LineFileDocs docs,
125-
int numThreads,
126-
long stopTime,
127-
ISet<string> delIDs,
124+
private ThreadJob[] LaunchIndexingThreads(LineFileDocs docs,
125+
int numThreads,
126+
long stopTime,
127+
ISet<string> delIDs,
128128
ISet<string> delPackIDs,
129129
ConcurrentQueue<SubDocs> allSubDocs)
130130
{
@@ -264,7 +264,7 @@ public override void Run()
264264
outerInstance.m_delCount.AddAndGet(delSubDocs.SubIDs.Count);
265265
if (Verbose)
266266
{
267-
Console.WriteLine(Thread.CurrentThread.Name + ": update pack packID=" + delSubDocs.PackID +
267+
Console.WriteLine(Thread.CurrentThread.Name + ": update pack packID=" + delSubDocs.PackID +
268268
" count=" + docsList.Count + " docs=" + string.Format(J2N.Text.StringFormatter.InvariantCulture, "{0}", docIDs));
269269
}
270270
outerInstance.UpdateDocuments(packIDTerm, docsList);
@@ -273,7 +273,7 @@ public override void Run()
273273
{
274274
if (Verbose)
275275
{
276-
Console.WriteLine(Thread.CurrentThread.Name + ": add pack packID=" + packID +
276+
Console.WriteLine(Thread.CurrentThread.Name + ": add pack packID=" + packID +
277277
" count=" + docsList.Count + " docs=" + string.Format(J2N.Text.StringFormatter.InvariantCulture, "{0}", docIDs));
278278
}
279279
outerInstance.AddDocuments(packIDTerm, docsList);
@@ -378,9 +378,8 @@ public override void Run()
378378
catch (Exception t) when (t.IsThrowable())
379379
{
380380
Console.WriteLine(Thread.CurrentThread.Name + ": hit exc");
381-
Console.WriteLine(t.ToString());
382-
Console.Write(t.StackTrace);
383-
outerInstance.m_failed.Value = (true);
381+
outerInstance.m_failed.Value = true;
382+
t.PrintStackTrace();
384383
throw RuntimeException.Create(t);
385384
}
386385
}
@@ -536,8 +535,8 @@ public override void Run()
536535
catch (Exception t) when (t.IsThrowable())
537536
{
538537
Console.WriteLine(Thread.CurrentThread.Name + ": hit exc");
539-
outerInstance.m_failed.Value = (true);
540-
Console.WriteLine(t.ToString());
538+
outerInstance.m_failed.Value = true;
539+
t.PrintStackTrace(Console.Out);
541540
throw RuntimeException.Create(t);
542541
}
543542
}
@@ -929,4 +928,4 @@ protected virtual void SmokeTestSearcher(IndexSearcher s)
929928
RunQuery(s, pq);
930929
}
931930
}
932-
}
931+
}

src/Lucene.Net.TestFramework/Search/ShardSearchingTestBase.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -736,7 +736,7 @@ public override void Run()
736736
catch (Exception t) when (t.IsThrowable())
737737
{
738738
Console.WriteLine("FAILED:");
739-
Console.Out.WriteLine(t.StackTrace);
739+
t.PrintStackTrace(Console.Out);
740740
throw RuntimeException.Create(t);
741741
}
742742
}

src/Lucene.Net.TestFramework/Store/MockDirectoryWrapper.cs

+5-3
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,7 @@ internal virtual void MaybeThrowIOException(string message)
494494
if (LuceneTestCase.Verbose)
495495
{
496496
Console.WriteLine(Thread.CurrentThread.Name + ": MockDirectoryWrapper: now throw random exception" + (message is null ? "" : " (" + message + ")"));
497+
StackTraceHelper.PrintCurrentStackTrace(Console.Out);
497498
}
498499
throw new IOException("a random IOException" + (message is null ? "" : " (" + message + ")"));
499500
}
@@ -505,15 +506,16 @@ internal virtual void MaybeThrowIOExceptionOnOpen(string name)
505506
{
506507
if (LuceneTestCase.Verbose)
507508
{
508-
Console.WriteLine(Thread.CurrentThread.Name + ": MockDirectoryWrapper: now throw random exception during open file=" + name);
509+
Console.WriteLine(Thread.CurrentThread.Name + ": MockDirectoryWrapper: now throw random exception during open file=" + name);
510+
StackTraceHelper.PrintCurrentStackTrace(Console.Out);
509511
}
510512
if (allowRandomFileNotFoundException == false || randomState.NextBoolean())
511513
{
512514
throw new IOException("a random IOException (" + name + ")");
513515
}
514516
else
515517
{
516-
throw randomState.NextBoolean() ? (IOException)new FileNotFoundException("a random IOException (" + name + ")") : new DirectoryNotFoundException("a random IOException (" + name + ")");
518+
throw randomState.NextBoolean() ? new FileNotFoundException("a random IOException (" + name + ")") : new DirectoryNotFoundException("a random IOException (" + name + ")");
517519
}
518520
}
519521
}
@@ -1038,7 +1040,7 @@ protected override void Dispose(bool disposing)
10381040
catch (Exception t) when (t.IsThrowable())
10391041
{
10401042
Console.Error.WriteLine("ERROR processing leftover segments file " + file + ":");
1041-
Console.WriteLine(t.ToString());
1043+
t.PrintStackTrace();
10421044
}
10431045
}
10441046
}

src/Lucene.Net.TestFramework/Store/MockIndexOutputWrapper.cs

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Lucene.Net.Util;
1+
using Lucene.Net.Support;
2+
using Lucene.Net.Util;
23
using System;
34
using System.IO;
45
using System.Threading;
@@ -97,7 +98,7 @@ private void CheckDiskFull(byte[] b, int offset, DataInput @in, long len)
9798
if (LuceneTestCase.Verbose)
9899
{
99100
Console.WriteLine(Thread.CurrentThread.Name + ": MDW: now throw fake disk full");
100-
Console.WriteLine(Environment.StackTrace);
101+
StackTraceHelper.PrintCurrentStackTrace(Console.Out);
101102
}
102103
throw new IOException(message);
103104
}
@@ -199,4 +200,4 @@ public override string ToString()
199200
return "MockIndexOutputWrapper(" + @delegate + ")";
200201
}
201202
}
202-
}
203+
}

src/Lucene.Net.TestFramework/Support/JavaCompatibility/SystemTypesHelpers.cs

-11
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
using System.Linq;
99
using System.Text;
1010
using System.Text.RegularExpressions;
11-
using Console = Lucene.Net.Util.SystemConsole;
1211
using JCG = J2N.Collections.Generic;
1312

1413
namespace Lucene.Net
@@ -290,16 +289,6 @@ public static void retainAll<T>(this ISet<T> s, ISet<T> other)
290289
}
291290
}
292291

293-
public static void printStackTrace(this Exception e)
294-
{
295-
Console.Error.WriteLine(e.StackTrace);
296-
}
297-
298-
public static void printStackTrace(this Exception e, TextWriter destination)
299-
{
300-
destination.WriteLine(e.StackTrace);
301-
}
302-
303292
/// <summary>
304293
/// Locates resources in the same directory as this type
305294
/// </summary>

0 commit comments

Comments
 (0)