Skip to content

Commit

Permalink
Closes #3075
Browse files Browse the repository at this point in the history
  • Loading branch information
JustArchi committed Nov 19, 2023
1 parent fe8e125 commit 2e9791f
Showing 1 changed file with 25 additions and 6 deletions.
31 changes: 25 additions & 6 deletions ArchiSteamFarm/NLog/Logging.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ internal static class Logging {
private const byte ConsoleResponsivenessDelay = 250; // In milliseconds
private const string GeneralLayout = $@"${{date:format=yyyy-MM-dd HH\:mm\:ss}}|${{processname}}-${{processid}}|${{level:uppercase=true}}|{LayoutMessage}";
private const string LayoutMessage = @"${logger}|${message}${onexception:inner= ${exception:format=toString,Data}}";
private const byte MaxReadFailures = 100; // How many unknown characters we allow before closing STDIN in order to avoid infinite loop

internal static bool LogFileExists => File.Exists(SharedInfo.LogFile);

Expand Down Expand Up @@ -308,12 +309,19 @@ private static string ConsoleReadLineMasked(char mask = '*') {

StringBuilder result = new();

byte readFailures = 0;

while (true) {
ConsoleKeyInfo keyInfo = Console.ReadKey(true);

switch (keyInfo.Key) {
case 0 when ++readFailures < MaxReadFailures:
// Likely linux terminal closing STDIN, but we'll check for more
continue;
case 0:
// Linux terminal closing STDIN, we're done here
ASF.ArchiLogger.LogGenericWarning(string.Format(CultureInfo.CurrentCulture, Strings.WarningFailedWithError, nameof(readFailures)));

return result.ToString();
case ConsoleKey.Enter:
// User finishing input, as expected
Expand All @@ -322,6 +330,8 @@ private static string ConsoleReadLineMasked(char mask = '*') {
return result.ToString();
}

readFailures = 0;

Check warning on line 333 in ArchiSteamFarm/NLog/Logging.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Code is unreachable

Code is unreachable

// User continues input
if (!char.IsControl(keyInfo.KeyChar)) {
result.Append(keyInfo.KeyChar);
Expand All @@ -344,8 +354,10 @@ private static string ConsoleReadLineMasked(char mask = '*') {
}

private static async Task HandleConsoleInteractively() {
while (!Program.ShutdownSequenceInitialized) {
try {
try {
byte readFailures = 0;

while (!Program.ShutdownSequenceInitialized) {
if (IsWaitingForUserInput || !Console.KeyAvailable) {
await Task.Delay(ConsoleResponsivenessDelay).ConfigureAwait(false);

Expand All @@ -358,17 +370,26 @@ private static async Task HandleConsoleInteractively() {
ConsoleKeyInfo keyInfo = Console.ReadKey(true);

switch (keyInfo.Key) {
case 0 when ++readFailures < MaxReadFailures:
// Likely linux terminal closing STDIN, but we'll check for more
continue;
case 0:
// Linux terminal closing STDIN, we're done here
ASF.ArchiLogger.LogGenericWarning(string.Format(CultureInfo.CurrentCulture, Strings.WarningFailedWithError, nameof(readFailures)));

return;
case ConsoleKey.C:
// User hitting 'c', as expected
break;
default:

Check warning on line 384 in ArchiSteamFarm/NLog/Logging.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Code is unreachable

Code is unreachable
// Any other input, ignored
readFailures = 0;

continue;
}

readFailures = 0;

OnUserInputStart();

try {
Expand Down Expand Up @@ -416,11 +437,9 @@ private static async Task HandleConsoleInteractively() {
} finally {
ConsoleSemaphore.Release();
}
} catch (Exception e) {
ASF.ArchiLogger.LogGenericException(e);

return;
}
} catch (Exception e) {
ASF.ArchiLogger.LogGenericException(e);
}
}

Expand Down

0 comments on commit 2e9791f

Please sign in to comment.