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

Commit c85b19b

Browse files
author
Jérémie Bertrand
committed
Clean tracing & Logger
1 parent be16c34 commit c85b19b

25 files changed

Lines changed: 159 additions & 155 deletions

src/Pretzel.Logic/Commands/CommandParameters.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public void DetectFromDirectory(IDictionary<string, ISiteEngine> engines, SiteCo
105105
foreach (var engine in engines)
106106
{
107107
if (!engine.Value.CanProcess(context)) continue;
108-
Tracing.InfoFormat("Recommended engine for directory: '{0}'", engine.Key);
108+
Tracing.Info("Recommended engine for directory: '{0}'", engine.Key);
109109
Template = engine.Key;
110110
return;
111111
}

src/Pretzel.Logic/Extensions/Logger.cs

Lines changed: 0 additions & 47 deletions
This file was deleted.

src/Pretzel.Logic/Extensions/Tracing.cs

Lines changed: 55 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,51 +2,85 @@
22

33
namespace Pretzel.Logic.Extensions
44
{
5+
/// <summary>
6+
/// Trace for Pretzel
7+
/// </summary>
58
public static class Tracing
69
{
7-
[Obsolete("Use Tracing directly instead")]
8-
public enum Category {
9-
Debug,
10-
Info,
11-
Error
12-
}
10+
private static Action<string, TraceLevel> _trace;
11+
private static TraceLevel _minLevel;
1312

1413
static Tracing()
1514
{
16-
Logger = new Logger();
15+
// Do nothing by default
16+
_trace = (message, TraceLevel) => { };
17+
_minLevel = TraceLevel.Info;
1718
}
1819

19-
[Obsolete("Use Tracing directly instead")]
20-
public static Logger Logger { get; set; }
20+
internal static void SetTrace(Action<string, TraceLevel> trace)
21+
{
22+
_trace = trace;
23+
}
2124

22-
public static void Debug(string message)
25+
internal static void SetMinimalLevel(TraceLevel level)
2326
{
24-
Logger.Write(message, Category.Debug);
27+
_minLevel = level;
2528
}
2629

27-
public static void Info(string message)
30+
/// <summary>
31+
/// Trace a debug message
32+
/// </summary>
33+
/// <param name="message">Message to trace</param>
34+
/// <param name="messageParameters">Format parameters for the message</param>
35+
public static void Debug(string message, params object[] messageParameters)
2836
{
29-
Logger.Write(message, Category.Info);
37+
TraceMessage(message, messageParameters, TraceLevel.Debug);
3038
}
3139

32-
public static void Error(string message)
40+
/// <summary>
41+
/// Trace an info message
42+
/// </summary>
43+
/// <param name="message">Message to trace</param>
44+
/// <param name="messageParameters">Format parameters for the message</param>
45+
public static void Info(string message, params object[] messageParameters)
3346
{
34-
Logger.Write(message, Category.Error);
47+
TraceMessage(message, messageParameters, TraceLevel.Info);
3548
}
3649

37-
public static void DebugFormat(string message, params object[] messageParameters)
50+
/// <summary>
51+
/// Trace a warning message
52+
/// </summary>
53+
/// <param name="message">Message to trace</param>
54+
/// <param name="messageParameters">Format parameters for the message</param>
55+
public static void Warning(string message, params object[] messageParameters)
3856
{
39-
Logger.WriteFormat(message, Category.Debug, messageParameters);
57+
TraceMessage(message, messageParameters, TraceLevel.Warning);
4058
}
4159

42-
public static void InfoFormat(string message, params object[] messageParameters)
60+
/// <summary>
61+
/// Trace an error message
62+
/// </summary>
63+
/// <param name="message">Message to trace</param>
64+
/// <param name="messageParameters">Format parameters for the message</param>
65+
public static void Error(string message, params object[] messageParameters)
4366
{
44-
Logger.WriteFormat(message, Category.Info, messageParameters);
67+
TraceMessage(message, messageParameters, TraceLevel.Error);
4568
}
4669

47-
public static void ErrorFormat(string message, params object[] messageParameters)
70+
private static void TraceMessage(string message, object[] messageParameters, TraceLevel messageLevel)
4871
{
49-
Logger.WriteFormat(message, Category.Error, messageParameters);
72+
if(messageLevel >= _minLevel)
73+
{
74+
_trace(string.Format(message, messageParameters), messageLevel);
75+
}
5076
}
5177
}
78+
79+
internal enum TraceLevel
80+
{
81+
Debug,
82+
Info,
83+
Warning,
84+
Error
85+
}
5286
}

src/Pretzel.Logic/Import/BloggerImport.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ private void ImportPost(BloggerPost post)
106106
}
107107
catch (Exception e)
108108
{
109-
Tracing.InfoFormat("Failed to write out {0}", fileName);
109+
Tracing.Info("Failed to write out {0}", fileName);
110110
Tracing.Debug(e.Message);
111111
}
112112
}

src/Pretzel.Logic/Import/HtmlToMarkdownConverter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ private void ProcessNodes(StringBuilder markdown, IEnumerable<HtmlNode> htmlNode
130130
break;
131131
default:
132132
ProcessNodes(markdown, htmlNode.ChildNodes);
133-
Tracing.InfoFormat("{0}", htmlNode.OuterHtml);
133+
Tracing.Info("{0}", htmlNode.OuterHtml);
134134
break;
135135
}
136136
}

src/Pretzel.Logic/Pretzel.Logic.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@
105105
<Compile Include="Extensibility\TagFactoryBase.cs" />
106106
<Compile Include="Extensions\DictionaryExtensions.cs" />
107107
<Compile Include="Extensions\IAssembly.cs" />
108-
<Compile Include="Extensions\Logger.cs" />
109108
<Compile Include="Extensions\Sitemap.cs" />
110109
<Compile Include="Extensions\StringExtensions.cs" />
111110
<Compile Include="Extensions\Tracing.cs" />

src/Pretzel.Logic/Recipe/Ingredient.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,19 @@ public void Create()
3232

3333
if (!fileSystem.Directory.Exists(postPath))
3434
{
35-
Tracing.InfoFormat("{0} folder not found", postPath);
35+
Tracing.Info("{0} folder not found", postPath);
3636
return;
3737
}
3838

3939
if (fileSystem.File.Exists(fileSystem.Path.Combine(postPath, postName)))
4040
{
41-
Tracing.InfoFormat("The \"{0}\" file already exists", postName);
41+
Tracing.Info("The \"{0}\" file already exists", postName);
4242
return;
4343
}
4444

4545
fileSystem.File.WriteAllText(fileSystem.Path.Combine(postPath, postName), pageContents);
4646

47-
Tracing.InfoFormat("Created the \"{0}\" post ({1})", title, postName);
47+
Tracing.Info("Created the \"{0}\" post ({1})", title, postName);
4848
}
4949
}
5050
}

src/Pretzel.Logic/Recipe/Recipe.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public void Create()
106106
}
107107
catch (Exception ex)
108108
{
109-
Tracing.ErrorFormat("Error trying to create template: {0}", ex);
109+
Tracing.Error("Error trying to create template: {0}", ex);
110110
}
111111
}
112112

src/Pretzel.Logic/Templating/Context/SiteContextGenerator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ private Page CreatePage(SiteContext context, IConfiguration config, string file,
285285
}
286286
catch (Exception e)
287287
{
288-
Tracing.InfoFormat("Failed to build post from File: {0}", file);
288+
Tracing.Info("Failed to build post from File: {0}", file);
289289
Tracing.Info(e.Message);
290290
Tracing.Debug(e.ToString());
291291
}
@@ -353,7 +353,7 @@ private string SafeReadLine(string file)
353353
{
354354
if (SanityCheck.IsLockedByAnotherProcess(file))
355355
{
356-
Tracing.InfoFormat("File {0} is locked by another process. Skipping", file);
356+
Tracing.Info("File {0} is locked by another process. Skipping", file);
357357
return string.Empty;
358358
}
359359

src/Pretzel.Logic/Templating/JekyllEngineBase.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public void Process(SiteContext siteContext, bool skipFileOnError = false)
5454
_lightweightMarkupEngine = new CommonMarkEngine();
5555
}
5656

57-
Tracing.DebugFormat("LightweightMarkupEngine: {0}", _lightweightMarkupEngine.GetType().Name);
57+
Tracing.Debug("LightweightMarkupEngine: {0}", _lightweightMarkupEngine.GetType().Name);
5858

5959
Context = siteContext;
6060
PreProcess();
@@ -234,7 +234,7 @@ private string RenderContent(string file, string contents)
234234
}
235235
catch (Exception e)
236236
{
237-
Tracing.InfoFormat("Error ({0}) converting {1}", e.Message, file);
237+
Tracing.Info("Error ({0}) converting {1}", e.Message, file);
238238
Tracing.Debug(e.ToString());
239239
html = String.Format("<p><b>Error converting markdown:</b><br />{0}</p><p>Original content:<br /><pre>{1}</pre></p>", e.Message, contents);
240240
}

0 commit comments

Comments
 (0)