Skip to content
This repository has been archived by the owner on Dec 12, 2020. It is now read-only.

Commit

Permalink
Merge pull request #75 from LokiMidgard/Logger
Browse files Browse the repository at this point in the history
Logger
  • Loading branch information
AArnott authored Aug 20, 2018
2 parents 227430f + 798be8b commit 50458b5
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 3 deletions.
41 changes: 40 additions & 1 deletion src/CodeGeneration.Roslyn.Tasks/GenerateCodeFromAttributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,51 @@ protected override string GenerateResponseFileCommands()
argBuilder.AppendLine(this.generatedCompileItemsFilePath);

argBuilder.AppendLine("--");
foreach(var item in this.Compile)
foreach (var item in this.Compile)
{
argBuilder.AppendLine(item.ItemSpec);
}

return argBuilder.ToString();
}

protected override void LogEventsFromTextOutput(string singleLine, MessageImportance messageImportance)
{
MessageImportance newImportance;
if (DidExtractPrefix("High"))
{
newImportance = MessageImportance.High;
}
else if (DidExtractPrefix("Normal"))
{
newImportance = MessageImportance.Normal;
}
else if (DidExtractPrefix("Low"))
{
newImportance = MessageImportance.Low;
}
else
{
newImportance = messageImportance;
}

if (newImportance < messageImportance)
{
messageImportance = newImportance; // Lower value => higher importance
}

base.LogEventsFromTextOutput(singleLine, messageImportance);

bool DidExtractPrefix(string importanceString)
{
var prefix = $"::{importanceString}::";
if (singleLine.StartsWith(prefix))
{
singleLine = singleLine.Substring(prefix.Length);
return true;
}
return false;
}
}
}
}
6 changes: 4 additions & 2 deletions src/CodeGeneration.Roslyn.Tool/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,10 @@ static int Main(string[] args)
{
generator.Generate(progress);
}
catch
catch (Exception e)
{
Logger.Log(LogLevel.High, $"{e.GetType().Name}: {e.Message}");
Logger.Log(LogLevel.High, e.ToString());
return 3;
}

Expand All @@ -65,7 +67,7 @@ static int Main(string[] args)

foreach (var file in generator.GeneratedFiles)
{
Console.WriteLine(file);
Logger.Log(LogLevel.Normal, file);
}

return 0;
Expand Down
48 changes: 48 additions & 0 deletions src/CodeGeneration.Roslyn/Logger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace CodeGeneration.Roslyn
{
public enum LogLevel
{
/// <summary>
/// High importance, appears in less verbose logs
/// </summary>
High = 0,

/// <summary>
/// Normal importance
/// </summary>
Normal = 1,

/// <summary>
/// Low importance, appears in more verbose logs
/// </summary>
Low = 2,
}
public static class Logger
{
public static void Log(LogLevel logLevel, string message)
{
// Prefix every Line with loglevel
var begin = 0;
var end = message.IndexOf('\n');
bool foundR = end > 0 && message[end - 1] == '\r';
if(foundR)
end--;
while (end != -1)
{
Print(message.Substring(begin, end - begin));
begin = end + (foundR ? 2 : 1);
end = message.IndexOf('\n', begin);
foundR = end > 0 && message[end - 1] == '\r';
if(foundR)
end--;
}
Print(message.Substring(begin, message.Length - begin));

void Print(string toPrint) => Console.WriteLine($"::{logLevel}::{toPrint}");
}
}
}

0 comments on commit 50458b5

Please sign in to comment.