Skip to content

Commit af70c48

Browse files
committed
Introduce Boolean properties to know if a logger level is enabled
It is inspired from Common.Logging ILog.IsDebugEnabled See https://github.com/net-commons/common-logging/blob/master/src/Common.Logging.Core/Logging/ILog.cs#L637 If avoid doing an unnecessary String.Format()
1 parent 628f127 commit af70c48

File tree

7 files changed

+58
-22
lines changed

7 files changed

+58
-22
lines changed

src/GitVersionCore.Tests/ModuleInitializer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public static class ModuleInitializer
1111
/// </summary>
1212
public static void Initialize()
1313
{
14-
Logger.SetLoggers(
14+
Logger.SetLoggers(VerbosityLevel.Debug,
1515
s => Console.WriteLine(s),
1616
s => Console.WriteLine(s),
1717
s => Console.WriteLine(s),

src/GitVersionCore/Logger.cs

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,19 @@ static Logger()
1414
Reset();
1515
}
1616

17+
public static bool IsDebugEnabled;
18+
public static bool IsInfoEnabled;
19+
public static bool IsWarningEnabled;
20+
public static bool IsErrorEnabled;
21+
1722
public static Action<string> WriteDebug { get; private set; }
1823
public static Action<string> WriteInfo { get; private set; }
1924
public static Action<string> WriteWarning { get; private set; }
2025
public static Action<string> WriteError { get; private set; }
2126

2227
public static IDisposable IndentLog(string operationDescription)
2328
{
29+
if (!IsInfoEnabled) return new ActionDisposable(() => { });
2430
var start = DateTime.Now;
2531
WriteInfo("Begin: " + operationDescription);
2632
indent = indent + " ";
@@ -41,17 +47,47 @@ static Action<string> ObscurePassword(Action<string> info)
4147
return logAction;
4248
}
4349

44-
public static void SetLoggers(Action<string> debug, Action<string> info, Action<string> warn, Action<string> error)
50+
public static void SetLoggers(VerbosityLevel verbosity, Action<string> debug, Action<string> info, Action<string> warn, Action<string> error)
4551
{
46-
if (debug == null) throw new ArgumentNullException("debug");
47-
if (info == null) throw new ArgumentNullException("info");
48-
if (warn == null) throw new ArgumentNullException("warn");
49-
if (error == null) throw new ArgumentNullException("error");
50-
51-
WriteDebug = LogMessage(ObscurePassword(debug), "DEBUG");
52-
WriteInfo = LogMessage(ObscurePassword(info), "INFO");
53-
WriteWarning = LogMessage(ObscurePassword(warn), "WARN");
54-
WriteError = LogMessage(ObscurePassword(error), "ERROR");
52+
if (verbosity >= VerbosityLevel.Debug)
53+
{
54+
if (debug == null) throw new ArgumentNullException("debug");
55+
IsDebugEnabled = true;
56+
WriteDebug = LogMessage(ObscurePassword(debug), "DEBUG");
57+
}
58+
else {
59+
WriteDebug = (m) => { };
60+
}
61+
if (verbosity >= VerbosityLevel.Info)
62+
{
63+
if (info == null) throw new ArgumentNullException("Info");
64+
IsInfoEnabled = true;
65+
WriteInfo = LogMessage(ObscurePassword(info), "INFO");
66+
}
67+
else
68+
{
69+
WriteInfo = (m) => { };
70+
}
71+
if (verbosity >= VerbosityLevel.Warn)
72+
{
73+
if (warn == null) throw new ArgumentNullException("Warn");
74+
IsWarningEnabled = true;
75+
WriteWarning = LogMessage(ObscurePassword(warn), "WARN");
76+
}
77+
else
78+
{
79+
WriteWarning = (m) => { };
80+
}
81+
if (verbosity >= VerbosityLevel.Error)
82+
{
83+
if (error == null) throw new ArgumentNullException("Error");
84+
IsErrorEnabled = true;
85+
WriteError = LogMessage(ObscurePassword(error), "ERROR");
86+
}
87+
else
88+
{
89+
WriteError = (m) => { };
90+
}
5591
}
5692

5793
public static IDisposable AddLoggersTemporarily(Action<string> debug, Action<string> info, Action<string> warn, Action<string> error)
@@ -60,7 +96,7 @@ public static IDisposable AddLoggersTemporarily(Action<string> debug, Action<str
6096
var currentInfo = WriteInfo;
6197
var currentWarn = WriteWarning;
6298
var currentError = WriteError;
63-
SetLoggers(s =>
99+
SetLoggers(VerbosityLevel.Debug, s =>
64100
{
65101
debug(s);
66102
currentDebug(s);

src/GitVersionExe/Program.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ static int VerifyArgumentsAndRun()
8181
}
8282
else
8383
{
84-
Logger.WriteInfo("Working directory: " + arguments.TargetPath);
84+
if (Logger.IsInfoEnabled) Logger.WriteInfo("Working directory: " + arguments.TargetPath);
8585
}
8686
VerifyConfiguration(arguments, fileSystem);
8787

@@ -179,11 +179,11 @@ static void ConfigureLogging(Arguments arguments)
179179
}
180180
}
181181

182-
Logger.SetLoggers(
183-
s => writeActions.ForEach(a => { if (arguments.Verbosity >= VerbosityLevel.Debug) a(s); }),
184-
s => writeActions.ForEach(a => { if (arguments.Verbosity >= VerbosityLevel.Info) a(s); }),
185-
s => writeActions.ForEach(a => { if (arguments.Verbosity >= VerbosityLevel.Warn) a(s); }),
186-
s => writeActions.ForEach(a => { if (arguments.Verbosity >= VerbosityLevel.Error) a(s); }));
182+
Logger.SetLoggers(arguments.Verbosity,
183+
s => writeActions.ForEach(a => { a(s); }),
184+
s => writeActions.ForEach(a => { a(s); }),
185+
s => writeActions.ForEach(a => { a(s); }),
186+
s => writeActions.ForEach(a => { a(s); }));
187187

188188
if (exception != null)
189189
Logger.WriteError(string.Format("Failed to configure logging for '{0}': {1}", arguments.LogFilePath, exception.Message));

src/GitVersionTask.Tests/ModuleInitializer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public static class ModuleInitializer
1010
/// </summary>
1111
public static void Initialize()
1212
{
13-
Logger.SetLoggers(
13+
Logger.SetLoggers(VerbosityLevel.Debug,
1414
s => System.Console.WriteLine(s),
1515
s => System.Console.WriteLine(s),
1616
s => System.Console.WriteLine(s),

src/GitVersionTask/AssemblyInfoBuilder/UpdateAssemblyInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public UpdateAssemblyInfo()
2020
{
2121
};
2222
logger = new TaskLogger(this);
23-
Logger.SetLoggers(this.LogDebug, this.LogInfo, this.LogWarning, s => this.LogError(s));
23+
Logger.SetLoggers(VerbosityLevel.Debug, this.LogDebug, this.LogInfo, this.LogWarning, s => this.LogError(s));
2424
}
2525

2626
[Required]

src/GitVersionTask/GetVersion.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class GetVersion : GitVersionTaskBase
1313
public GetVersion()
1414
{
1515
logger = new TaskLogger(this);
16-
Logger.SetLoggers(this.LogDebug, this.LogInfo, this.LogWarning, s => this.LogError(s));
16+
Logger.SetLoggers(VerbosityLevel.Debug, this.LogDebug, this.LogInfo, this.LogWarning, s => this.LogError(s));
1717
}
1818

1919
[Required]

src/GitVersionTask/WriteVersionInfoToBuildLog.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class WriteVersionInfoToBuildLog : GitVersionTaskBase
1515
public WriteVersionInfoToBuildLog()
1616
{
1717
logger = new TaskLogger(this);
18-
Logger.SetLoggers(this.LogDebug, this.LogInfo, this.LogWarning, s => this.LogError(s));
18+
Logger.SetLoggers(VerbosityLevel.Debug, this.LogDebug, this.LogInfo, this.LogWarning, s => this.LogError(s));
1919
}
2020

2121
[Required]

0 commit comments

Comments
 (0)