Skip to content

Commit

Permalink
Use TextWriter.Null for SystemConsole by default, #936
Browse files Browse the repository at this point in the history
  • Loading branch information
paulirwin committed Oct 15, 2024
1 parent 7f3692b commit 9b1a2a5
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 16 deletions.
8 changes: 4 additions & 4 deletions src/Lucene.Net.QueryParser/Classic/QueryParserTokenManager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Lucene.Net.Support.IO;
using Lucene.Net.Support.IO;
using System;
using System.Diagnostics.CodeAnalysis;
using System.IO;
Expand Down Expand Up @@ -31,7 +31,7 @@ public class QueryParserTokenManager //: QueryParserConstants
{
/// <summary>Debug output. </summary>
#pragma warning disable IDE0052 // Remove unread private members
private TextWriter debugStream; // LUCENENET specific - made private, since we already have a setter
private TextWriter debugStream = Console.Out; // LUCENENET specific - made private, since we already have a setter
#pragma warning restore IDE0052 // Remove unread private members
/// <summary>Set debug output. </summary>
public virtual void SetDebugStream(TextWriter ds)
Expand Down Expand Up @@ -1162,7 +1162,7 @@ public QueryParserTokenManager(ICharStream stream)
m_input_stream = stream;
}

/// <summary>Constructor.
/// <summary>Constructor.
/// <para>Note that this constructor calls a virtual method <see cref="SwitchTo(int)" />. If you
/// are subclassing this class, use <see cref="QueryParserTokenManager(ICharStream)" /> constructor and
/// call SwitchTo if needed.</para>
Expand Down Expand Up @@ -1358,4 +1358,4 @@ private void JjCheckNAddStates(int start, int end)
while (start++ != end);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Lucene.Net.Support.IO;
using Lucene.Net.Support.IO;
using System.Diagnostics.CodeAnalysis;
using System;
using System.IO;
Expand Down Expand Up @@ -32,7 +32,7 @@ public class StandardSyntaxParserTokenManager /*: StandardSyntaxParserConstants*
{
/// <summary>Debug output.</summary>
#pragma warning disable IDE0052 // Remove unread private members
private TextWriter debugStream; // LUCENENET specific - made private, since we already have a setter
private TextWriter debugStream = Console.Out; // LUCENENET specific - made private, since we already have a setter
#pragma warning restore IDE0052 // Remove unread private members
/// <summary>Set debug output.</summary>
public void SetDebugStream(TextWriter ds) { debugStream = new SafeTextWriterWrapper(ds); }
Expand Down Expand Up @@ -869,7 +869,7 @@ public virtual Token GetNextToken()
Token matchedToken;
int curPos = 0;


for (;;)
{
try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,15 @@ namespace Lucene.Net.Util
/// <b>Example:</b>
/// <code>
/// using RandomizedTesting.Generators;
///
///
/// public class Startup : LuceneTestFrameworkInitializer
/// {
/// // Run first
/// protected override void Initialize()
/// {
/// // Inject a custom configuration factory
/// ConfigurationFactory = new MyConfigurationFactory();
///
///
/// // Do any additional dependency injection setup here
/// }
///
Expand Down Expand Up @@ -301,7 +301,7 @@ protected virtual void Initialize()
/// <para/>
/// Repeatable random setup can be done by calling <see cref="Random"/> or by using methods of <see cref="LuceneTestCase"/>.
/// <para/>
/// It is not possible to set <see cref="CodecFactory"/>, <see cref="PostingsFormatFactory"/>,
/// It is not possible to set <see cref="CodecFactory"/>, <see cref="PostingsFormatFactory"/>,
/// <see cref="DocValuesFormatFactory"/> and <see cref="ConfigurationFactory"/>
/// from this method. Those must be called in <see cref="Initialize()"/>.
/// </summary>
Expand Down Expand Up @@ -334,6 +334,10 @@ internal class DefaultLuceneTestFrameworkInitializer : LuceneTestFrameworkInitia
/// </summary>
internal void InitializeStaticState()
{
// Enable console output
SystemConsole.Out = Console.Out;
SystemConsole.Error = Console.Error;

// Setup the factories
ConfigurationSettings.SetConfigurationFactory(ConfigurationFactory);
Codec.SetCodecFactory(CodecFactory);
Expand Down
9 changes: 4 additions & 5 deletions src/Lucene.Net/Support/Util/SystemConsole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,14 @@ namespace Lucene.Net.Util

/// <summary>
/// Mimics <see cref="System.Console"/>, but allows for swapping
/// the <see cref="TextWriter"/> of
/// <see cref="Out"/> and <see cref="Error"/>, or the <see cref="TextReader"/> of <see cref="In"/>
/// the <see cref="TextWriter"/> of
/// <see cref="Out"/> and <see cref="Error"/>
/// with user-defined implementations.
/// </summary>
public static class SystemConsole
{
public static TextWriter Out { get; set; } = Console.Out;
public static TextWriter Error { get; set; } = Console.Error;
public static TextReader In { get; set; } = Console.In;
public static TextWriter Out { get; set; } = TextWriter.Null;
public static TextWriter Error { get; set; } = TextWriter.Null;

[MethodImpl(MethodImplOptions.NoInlining)]
#if FEATURE_CODE_ACCESS_SECURITY
Expand Down
7 changes: 6 additions & 1 deletion src/dotnet/tools/lucene-cli/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Lucene.Net.Configuration;
using Lucene.Net.Util;
using Microsoft.Extensions.Configuration;
using System;

Expand All @@ -24,8 +25,12 @@ public static class Program
{
public static int Main(string[] args)
{
// Enable console output
SystemConsole.Out = Console.Out;
SystemConsole.Error = Console.Error;

var configuration = new ConfigurationBuilder()
.AddEnvironmentVariables(prefix: "lucene:") // Use a custom prefix to only load Lucene.NET settings
.AddEnvironmentVariables(prefix: "lucene:") // Use a custom prefix to only load Lucene.NET settings
.AddJsonFile("appsettings.json", optional: true)
.Build();
ConfigurationSettings.SetConfigurationFactory(new ConfigurationFactory(configuration));
Expand Down

0 comments on commit 9b1a2a5

Please sign in to comment.