Skip to content

Commit

Permalink
Further improve logging color output
Browse files Browse the repository at this point in the history
  • Loading branch information
Gwindalmir committed Sep 4, 2021
1 parent 32545d1 commit c390dda
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 13 deletions.
27 changes: 24 additions & 3 deletions WorkshopToolCommon/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;
using VRage;
using VRage.GameServices;
Expand Down Expand Up @@ -81,9 +82,12 @@ public static class LoggingHelper
/// <param name="customMessage">Message text to log, exception message will be appended.</param>
public static void Log(this Exception ex, string customMessage = "ERROR: An exception occurred: ")
{
MySandboxGame.Log.WriteLineAndConsole(customMessage + ex.Message);
MySandboxGame.Log.WriteLineToConsole("Check the log file for details.");
MySandboxGame.Log.WriteLine(ex.StackTrace);
ProgramBase.ConsoleWriteColored(ConsoleColor.Red, () =>
{
MySandboxGame.Log.WriteLineAndConsole(customMessage + ex.Message);
MySandboxGame.Log.WriteLineToConsole("Check the log file for details.");
MySandboxGame.Log.WriteLine(ex.StackTrace);
});
}

/// <summary>
Expand Down Expand Up @@ -121,4 +125,21 @@ public static string AsString(this ulong id)
}
#endif
}

public static class ConsoleHelper
{
public static bool IsInteractive(this TextWriter stream)
{
if (!Environment.UserInteractive)
return false;

if (Console.Out == stream)
return !Console.IsOutputRedirected;

if (Console.Error == stream)
return !Console.IsErrorRedirected;

return false;
}
}
}
16 changes: 10 additions & 6 deletions WorkshopToolCommon/GameBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,8 @@ public virtual int InitGame(string[] args)
{
if (!options.Clear && !options.ListCloud && !options.ListDLCs)
{
Console.WriteLine(CommandLine.Text.HelpText.AutoBuild(result, null, null).ToString());
ProgramBase.ConsoleWriteColored(ConsoleColor.Yellow, () =>
Console.WriteLine(HelpText.AutoBuild(result, null, null).ToString()));
return Cleanup(1);
}
}
Expand Down Expand Up @@ -201,9 +202,12 @@ public virtual int InitGame(string[] args)

if (!SteamAPI.IsSteamRunning())
{
MySandboxGame.Log.WriteLineAndConsole("ERROR: * Steam not detected. Is Steam running and not as Admin? *");
MySandboxGame.Log.WriteLineAndConsole("* Only compile testing is available. *");
MySandboxGame.Log.WriteLineAndConsole("");
ProgramBase.ConsoleWriteColored(ConsoleColor.Yellow, () =>
{
MySandboxGame.Log.WriteLineAndConsole("ERROR: * Steam not detected. Is Steam running and not as Admin? *");
MySandboxGame.Log.WriteLineAndConsole("* Only compile testing is available. *");
MySandboxGame.Log.WriteLineAndConsole("");
});

if (options.Download)
return Cleanup(3);
Expand All @@ -217,7 +221,7 @@ public virtual int InitGame(string[] args)
ProgramBase.CheckForUpdate(MySandboxGame.Log.WriteLineAndConsole);

MySandboxGame.Log.WriteLineToConsole(string.Empty);
ProgramBase.ConsoleWriteColored(ConsoleColor.Gray, () =>
ProgramBase.ConsoleWriteColored(ConsoleColor.White, () =>
MySandboxGame.Log.WriteLineAndConsole($"Log file: {MySandboxGame.Log.GetFilePath()}"));
MySandboxGame.Log.WriteLineToConsole(string.Empty);

Expand Down Expand Up @@ -729,7 +733,7 @@ static bool ProcessItemsDownload(WorkshopType type, IEnumerable<string> paths, P
if (paths == null || paths?.Count() == 0 )
return true;

var width = Console.IsOutputRedirected ? 256 : Console.WindowWidth;
var width = Console.Out.IsInteractive() ? Console.WindowWidth : 256;

var items = new List<MyWorkshopItem>();
var modids = paths.Select(ulong.Parse);
Expand Down
6 changes: 3 additions & 3 deletions WorkshopToolCommon/ProgramBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ internal static void CheckForUpdate(Action<string> logMethod = null, Action<stri
catch (Exception ex)
{
// Don't cause problems if update checker failed. Just report it.
ConsoleWriteColored(ConsoleColor.Green, () =>
ConsoleWriteColored(ConsoleColor.Red, () =>
errorMethod($"Error checking for update: {ex.Message}"));
}
}
Expand All @@ -58,7 +58,7 @@ public static void ConsoleWriteColored(ConsoleColor color, Action<string> output
public static void ConsoleWriteColored(ConsoleColor color, Action outputMethod)
{
// Don't colorize output if the terminal is non-interactive, or any output streams are redirected.
if(Environment.UserInteractive && !Console.IsOutputRedirected && !Console.IsErrorRedirected)
if(Console.Out.IsInteractive() && Console.Error.IsInteractive())
Console.ForegroundColor = color;

try
Expand All @@ -67,7 +67,7 @@ public static void ConsoleWriteColored(ConsoleColor color, Action outputMethod)
}
finally
{
if (Environment.UserInteractive && !Console.IsOutputRedirected && !Console.IsErrorRedirected)
if (Console.Out.IsInteractive() && Console.Error.IsInteractive())
Console.ResetColor();
}
}
Expand Down
2 changes: 1 addition & 1 deletion WorkshopToolCommon/Uploader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -902,7 +902,7 @@ void PrintItemDetails()
if (m_deps?.Length > 0)
{
var depItems = new List<MyWorkshopItem>();
var width = Console.IsOutputRedirected ? 256 : Console.WindowWidth;
var width = Console.Out.IsInteractive() ? Console.WindowWidth : 256;
#if SE
var depIds = new List<WorkshopId>();
foreach (var item in m_deps)
Expand Down

0 comments on commit c390dda

Please sign in to comment.