Skip to content

Commit

Permalink
logging parity
Browse files Browse the repository at this point in the history
  • Loading branch information
rxyyy committed Nov 6, 2023
1 parent f864eb5 commit 40cc7bf
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 23 deletions.
14 changes: 7 additions & 7 deletions src/FileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static FileInfo CopyFile(FileInfo info, string dstPath)
}
catch (Exception e)
{
Logging.Message($"An exception occurred while attempting to copy file {info.Name}.");
Logging.Fatal($"An exception occurred while attempting to copy file {info.Name}.");
Logging.Exception(e);
}

Expand All @@ -34,7 +34,7 @@ public static bool SetCurrentDirectory(string path)
}
catch (Exception e)
{
Logging.Message($"An exception occurred while attempting to change the current directory to {path}.");
Logging.Fatal($"An exception occurred while attempting to change the current directory to {path}.");
Logging.Exception(e);
}

Expand All @@ -49,7 +49,7 @@ public static DirectoryInfo CreateDirectory(string path)
}
catch (Exception e)
{
Logging.Message($"An exception occurred while attempting to create directory {path}.");
Logging.Fatal($"An exception occurred while attempting to create directory {path}.");
Logging.Exception(e);
}

Expand All @@ -68,13 +68,13 @@ public static bool DeleteDirectory(string path, bool isRecursive)
}
catch (Exception e)
{
Logging.Message($"An exception occurred while attempting to delete directory {path}.");
Logging.Fatal($"An exception occurred while attempting to delete directory {path}.");
Logging.Exception(e);
}
}
else
{
Logging.Message($"Directory {path} does not exist.");
Logging.Warning($"Directory {path} does not exist.");

return true;
}
Expand Down Expand Up @@ -118,12 +118,12 @@ public static bool CopyDirectory(string srcPath, string dstPath, bool copySubDir
}
else
{
Logging.Message($"Directory {srcPath} does not exist.");
Logging.Warning($"Directory {srcPath} does not exist.");
}
}
catch (Exception e)
{
Logging.Message($"An exception occurred while attempting to copy directory {srcPath} to directory {dstPath}.");
Logging.Fatal($"An exception occurred while attempting to copy directory {srcPath} to directory {dstPath}.");
Logging.Exception(e);
}

Expand Down
33 changes: 23 additions & 10 deletions src/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ private static bool Build(string rootDirectory, string dstDirectory)
}
else
{
Logging.Message("Skipping directory clean-up, directory Packed does not exist.");
Logging.Info("Skipping directory clean-up, directory Packed does not exist.");
}

return false;
Expand All @@ -94,7 +94,7 @@ private static bool BuildScriptsLua(string rootDirectory, string scriptsDirector

if (!Process.Create(compilerPath, $"-s -o {dstFile} {script}"))
{
Logging.Message($"Script {script} could not compile.");
Logging.Fatal($"Script {script} could not compile.");

return false;
}
Expand All @@ -104,12 +104,12 @@ private static bool BuildScriptsLua(string rootDirectory, string scriptsDirector
}
else
{
Logging.Message($"File {compilerName} does not exist.");
Logging.Warning($"File {compilerName} does not exist.");
}
}
else
{
Logging.Message($"Directory {scriptsDirectory} does not exist.");
Logging.Warning($"Directory {scriptsDirectory} does not exist.");
}

return false;
Expand All @@ -132,7 +132,7 @@ private static bool BuildScriptsNFSMS(string scriptsDirectory)
}
else
{
Logging.Message($"Directory {scriptsDirectory} does not exist.");
Logging.Warning($"Directory {scriptsDirectory} does not exist.");
}

return Process.Create(ms_AttribulatorExecutableName, $"apply-script -i Unpacked -o Packed -p CARBON -s {directories}");
Expand Down Expand Up @@ -166,29 +166,42 @@ private static bool Build(string[] args)
}
else
{
Logging.Message($"File {ms_AttribulatorExecutableName} does not exist.");
Logging.Fatal($"File {ms_AttribulatorExecutableName} does not exist.");
}
}
else
{
Logging.Message($"Directory {ms_VanillaUnpackedDirectoryName} does not exist.");
Logging.Fatal($"Directory {ms_VanillaUnpackedDirectoryName} does not exist.");
}
}
else
{
Logging.Message($"Directory {attribulatorDirectory} does not exist.");
Logging.Fatal($"Directory {attribulatorDirectory} does not exist.");
}
}
else
{
var currentProcess = System.Diagnostics.Process.GetCurrentProcess();

Logging.Message($"Usage: {currentProcess.ProcessName} path/to/attribulator path/to/repository [path/to/copy/post/build].");
Logging.Info($"Usage: {currentProcess.ProcessName} path/to/attribulator path/to/repository [path/to/copy/post/build].");
}

return false;
}

public static void Main(string[] args) => Logging.Message($"Build {(Build(args) ? "successful" : "failed")}.");
public static void Main(string[] args)
{
var result = Build(args);
var @string = "successful";
var channelIndex = 3;

if (!result)
{
@string = "failed";
++channelIndex;
}

Logging.Message(channelIndex, $"Build {@string}.");
}
}
}
10 changes: 7 additions & 3 deletions src/Process.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,18 @@ public static bool Create(string path, string arguments)
process.WaitForExit();

var exitCode = process.ExitCode;
var result = exitCode == 0;

Logging.Message($"{path} exited with code {exitCode}.");
if (!result)
{
Logging.Fatal($"Process {path} exited with code {exitCode}.");
}

return exitCode == 0;
return result;
}
catch (Exception e)
{
Logging.Message($"An exception occurred while attempting to create process {path}.");
Logging.Fatal($"An exception occurred while attempting to create process {path}.");
Logging.Exception(e);
}

Expand Down
39 changes: 36 additions & 3 deletions src/Utils/Logging.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,51 @@ namespace Attribulatorulator.Utils
{
public static class Logging
{
public static void Message(string message) => Console.WriteLine(message);
private struct Channel
{
public ConsoleColor Color;
public string Name;
}

private static readonly Channel[] _channels = new[]
{
new Channel { Color = ConsoleColor.Gray, Name = " TRACE" },
new Channel { Color = ConsoleColor.Green, Name = " DEBUG" },
new Channel { Color = ConsoleColor.Yellow, Name = " WARNING" },
new Channel { Color = ConsoleColor.Cyan, Name = " INFO" },
new Channel { Color = ConsoleColor.Red, Name = " FATAL" },
};

public static void Exception(Exception e)
{
Message(e.Message);
Fatal(e.Message);

var stackTrace = e.StackTrace;

if (stackTrace is not null)
{
Message(stackTrace);
Fatal(stackTrace);
}
}

public static void Trace(string message) => Message(0, message);
public static void Debug(string message) => Message(1, message);
public static void Warning(string message) => Message(2, message);
public static void Info(string message) => Message(3, message);
public static void Fatal(string message) => Message(4, message);

public static void Message(int channelIndex, string message)
{
Console.Write('[');

var channel = _channels[channelIndex];
var color = channel.Color;

Console.ForegroundColor = color;

Console.Write(channel.Name);
Console.ResetColor();
Console.Write($"] {message}{Environment.NewLine}");
}
}
}

0 comments on commit 40cc7bf

Please sign in to comment.