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

Refactor logging #260

Merged
merged 1 commit into from
Aug 4, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions src/Pretzel.Logic/Extensions/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,36 @@ namespace Pretzel.Logic.Extensions
{
public class Logger
{
private readonly List<string> categories;
private readonly List<Tracing.Category> categories;
private TextWriter writer;

public Logger()
{
categories = new List<string>();
categories = new List<Tracing.Category>();
}

public void SetWriter(TextWriter textWriter)
{
writer = textWriter;
}

public void AddCategory(string category)
public void AddCategory(Tracing.Category category)
{
categories.Add(category);
}

public void Write(string message, string category)
public void Write(string message, Tracing.Category category)
{
if (writer == null) return;

if (categories.Contains(category))
{
writer.WriteLine(message);
if (category == Tracing.Category.Error)
{
writer.Flush();
}
}
}
}
}
12 changes: 9 additions & 3 deletions src/Pretzel.Logic/Extensions/Tracing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ namespace Pretzel.Logic.Extensions
{
public static class Tracing
{
public enum Category {
Debug,
Info,
Error
}

static Tracing()
{
Logger = new Logger();
Expand All @@ -13,17 +19,17 @@ static Tracing()

public static void Debug(string message)
{
Logger.Write(message, "debug");
Logger.Write(message, Category.Debug);
}

public static void Info(string message)
{
Logger.Write(message, "info");
Logger.Write(message, Category.Info);
}

public static void Error(string message)
{
Logger.Write(message, "error");
Logger.Write(message, Category.Error);
}
}
}
8 changes: 5 additions & 3 deletions src/Pretzel.Logic/Templating/Razor/RazorSiteEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.ComponentModel.Composition;
using System.IO;
using System.Text.RegularExpressions;
using Pretzel.Logic.Extensions;

namespace Pretzel.Logic.Templating.Razor
{
Expand Down Expand Up @@ -48,10 +49,11 @@ protected override string RenderTemplate(string content, PageContext pageData)
{
return Engine.Razor.RunCompile(content, pageData.Page.File, typeof(PageContext), pageData);
}
catch (Exception ex)
catch (Exception e)
{
Tracing.Debug(ex.Message + Environment.NewLine + ex.StackTrace);
Console.WriteLine(@"Failed to render template, falling back to direct content");
Tracing.Error(@"Failed to render template, falling back to direct content");
Tracing.Debug(e.Message);
Tracing.Debug(e.StackTrace);
return content;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Pretzel.Tests/Import/BloggerImportTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ public void Error_on_write_is_traced()
StringBuilder sb = new StringBuilder();
TextWriter writer = new StringWriter(sb);
Tracing.Logger.SetWriter(writer);
Tracing.Logger.AddCategory("info");
Tracing.Logger.AddCategory("debug");
Tracing.Logger.AddCategory(Tracing.Category.Info);
Tracing.Logger.AddCategory(Tracing.Category.Debug);

var fileSubstitute = Substitute.For<FileBase>();
fileSubstitute.ReadAllText(ImportFile).Returns(ImportContent);
Expand Down
2 changes: 1 addition & 1 deletion src/Pretzel.Tests/Import/HtmlToMarkdownConverterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public void Unknown_node_is_traced_and_child_processed()
StringBuilder sb = new StringBuilder();
TextWriter writer = new StringWriter(sb);
Tracing.Logger.SetWriter(writer);
Tracing.Logger.AddCategory("info");
Tracing.Logger.AddCategory(Tracing.Category.Info);

// act
string markdown = converter.Convert("<body><b>hello</b><br/><em>world</em></body>");
Expand Down
4 changes: 2 additions & 2 deletions src/Pretzel.Tests/Recipe/RecipeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ public RecipeTests()
{
writer = new StringWriter(sb);
Tracing.Logger.SetWriter(writer);
Tracing.Logger.AddCategory("info");
Tracing.Logger.AddCategory("error");
Tracing.Logger.AddCategory(Tracing.Category.Info);
Tracing.Logger.AddCategory(Tracing.Category.Error);
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -893,8 +893,8 @@ public void page_with_false_date_is_not_processed()
StringBuilder sb = new StringBuilder();
TextWriter writer = new StringWriter(sb);
Tracing.Logger.SetWriter(writer);
Tracing.Logger.AddCategory("info");
Tracing.Logger.AddCategory("debug");
Tracing.Logger.AddCategory(Tracing.Category.Info);
Tracing.Logger.AddCategory(Tracing.Category.Error);

// act
var siteContext = generator.BuildContext(@"C:\TestSite", @"C:\TestSite\_site", false);
Expand Down Expand Up @@ -930,8 +930,8 @@ public void render_with_ContentTransformer_exception_should_trace_the_error()
StringBuilder sb = new StringBuilder();
TextWriter writer = new StringWriter(sb);
Tracing.Logger.SetWriter(writer);
Tracing.Logger.AddCategory("info");
Tracing.Logger.AddCategory("debug");
Tracing.Logger.AddCategory(Tracing.Category.Info);
Tracing.Logger.AddCategory(Tracing.Category.Error);

var contentTransformer = Substitute.For<IContentTransform>();
contentTransformer.Transform(Arg.Any<string>()).Returns(s => { throw new Exception("foo bar"); });
Expand Down Expand Up @@ -1027,8 +1027,8 @@ public void file_with_2_ioexception_on_ReadAllText_is_not_processed_and_exceptio
StringBuilder sb = new StringBuilder();
TextWriter writer = new StringWriter(sb);
Tracing.Logger.SetWriter(writer);
Tracing.Logger.AddCategory("info");
Tracing.Logger.AddCategory("debug");
Tracing.Logger.AddCategory(Tracing.Category.Info);
Tracing.Logger.AddCategory(Tracing.Category.Error);

var generator = new SiteContextGenerator(fileSystemSubstitute, Enumerable.Empty<IContentTransform>(), new LinkHelper());

Expand Down
6 changes: 3 additions & 3 deletions src/Pretzel/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ internal class Program
private static void Main(string[] args)
{
Tracing.Logger.SetWriter(Console.Out);
Tracing.Logger.AddCategory("info");
Tracing.Logger.AddCategory("error");
Tracing.Logger.AddCategory(Tracing.Category.Info);
Tracing.Logger.AddCategory(Tracing.Category.Error);

var parameters = BaseParameters.Parse(args, new FileSystem());

if (parameters.Debug)
{
Tracing.Logger.AddCategory("debug");
Tracing.Logger.AddCategory(Tracing.Category.Debug);
}

var program = new Program();
Expand Down