Skip to content

Commit a88e863

Browse files
authored
Merge pull request #1107 from JakeGinnivan/log-verbosity
Log verbosity
2 parents 691b295 + 783b634 commit a88e863

20 files changed

+169
-55
lines changed

src/GitVersionCore.Tests/ConfigProviderTests.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ public void WarnOnExistingGitVersionConfigYamlFile(string path)
239239

240240
var logOutput = string.Empty;
241241
Action<string> action = info => { logOutput = info; };
242-
using (Logger.AddLoggersTemporarily(action, action, action))
242+
using (Logger.AddLoggersTemporarily(action, action, action, action))
243243
{
244244
ConfigurationProvider.Verify(workingPath, repoPath, fileSystem);
245245
}
@@ -256,9 +256,10 @@ public void WarnOnAmbiguousConfigFilesAtTheSameProjectRootDirectory(string path)
256256

257257
var logOutput = string.Empty;
258258
Action<string> action = info => { logOutput = info; };
259-
Logger.SetLoggers(action, action, action);
260-
261-
ConfigurationProvider.Verify(workingPath, repoPath, fileSystem);
259+
using (Logger.AddLoggersTemporarily(action, action, action, action))
260+
{
261+
ConfigurationProvider.Verify(workingPath, repoPath, fileSystem);
262+
}
262263

263264
var configFileDeprecatedWarning = string.Format("Ambiguous config files at '{0}'", path);
264265
logOutput.Contains(configFileDeprecatedWarning).ShouldBe(true);
@@ -286,7 +287,7 @@ public void NoWarnOnGitVersionYmlFile()
286287

287288
var s = string.Empty;
288289
Action<string> action = info => { s = info; };
289-
using (Logger.AddLoggersTemporarily(action, action, action))
290+
using (Logger.AddLoggersTemporarily(action, action, action, action))
290291
{
291292
ConfigurationProvider.Provide(repoPath, fileSystem);
292293
}

src/GitVersionCore.Tests/ExecuteCoreTests.cs

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,14 @@ public void CacheFileExistsOnDisk()
7171

7272
var versionAndBranchFinder = new ExecuteCore(fileSystem);
7373

74-
var info = RepositoryScope(versionAndBranchFinder, (fixture, vv) =>
74+
var logs = RepositoryScope(versionAndBranchFinder, (fixture, vv) =>
7575
{
7676
fileSystem.WriteAllText(vv.FileName, versionCacheFileContent);
7777
vv = versionAndBranchFinder.ExecuteGitVersion(null, null, null, null, false, fixture.RepositoryPath, null);
7878
vv.AssemblySemVer.ShouldBe("4.10.3.0");
7979
});
8080

81-
info.ShouldContain("Deserializing version variables from cache file", () => info);
81+
logs.Info.ShouldContain("Deserializing version variables from cache file", () => logs.Info);
8282
}
8383

8484

@@ -139,8 +139,8 @@ public void CacheFileExistsOnDiskWhenOverrideConfigIsSpecifiedVersionShouldBeDyn
139139
[Test]
140140
public void CacheFileIsMissing()
141141
{
142-
var info = RepositoryScope();
143-
info.ShouldContain("yml not found", () => info);
142+
var logsMessages = RepositoryScope();
143+
logsMessages.Info.ShouldContain("yml not found", () => logsMessages.Info);
144144
}
145145

146146

@@ -260,19 +260,34 @@ public void DynamicRepositoriesShouldNotErrorWithFailedToFindGitDirectory()
260260
});
261261
}
262262

263-
string RepositoryScope(ExecuteCore executeCore = null, Action<EmptyRepositoryFixture, VersionVariables> fixtureAction = null)
263+
LogMessages RepositoryScope(ExecuteCore executeCore = null, Action<EmptyRepositoryFixture, VersionVariables> fixtureAction = null)
264264
{
265265
// Make sure GitVersion doesn't trigger build server mode when we are running the tests
266266
Environment.SetEnvironmentVariable(AppVeyor.EnvironmentVariableName, null);
267267
Environment.SetEnvironmentVariable(TravisCI.EnvironmentVariableName, null);
268+
var debugBuilder = new StringBuilder();
269+
Action<string> debugLogger = s =>
270+
{
271+
debugBuilder.AppendLine(s);
272+
};
268273
var infoBuilder = new StringBuilder();
269274
Action<string> infoLogger = s =>
270275
{
271276
infoBuilder.AppendLine(s);
272277
};
278+
var warnBuilder = new StringBuilder();
279+
Action<string> warnLogger = s =>
280+
{
281+
warnBuilder.AppendLine(s);
282+
};
283+
var errorBuilder = new StringBuilder();
284+
Action<string> errorLogger = s =>
285+
{
286+
errorBuilder.AppendLine(s);
287+
};
273288
executeCore = executeCore ?? new ExecuteCore(fileSystem);
274289

275-
using (Logger.AddLoggersTemporarily(infoLogger, s => {}, s => { }))
290+
using (Logger.AddLoggersTemporarily(debugLogger, infoLogger, warnLogger, errorLogger))
276291
using (var fixture = new EmptyRepositoryFixture())
277292
{
278293
fixture.Repository.MakeACommit();
@@ -287,6 +302,12 @@ string RepositoryScope(ExecuteCore executeCore = null, Action<EmptyRepositoryFix
287302
}
288303
}
289304

290-
return infoBuilder.ToString();
305+
return new LogMessages
306+
{
307+
Debug = debugBuilder.ToString(),
308+
Info = infoBuilder.ToString(),
309+
Warn = warnBuilder.ToString(),
310+
Error = errorBuilder.ToString()
311+
};
291312
}
292313
}

src/GitVersionCore.Tests/GitVersionCore.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@
126126
<Compile Include="GitVersionContextTests.cs" />
127127
<Compile Include="Helpers\DirectoryHelper.cs" />
128128
<Compile Include="IntegrationTests\MainlineDevelopmentMode.cs" />
129+
<Compile Include="LogMessages.cs" />
129130
<Compile Include="Mocks\MockThreadSleep.cs" />
130131
<Compile Include="OperationWithExponentialBackoffTests.cs" />
131132
<Compile Include="Init\InitScenarios.cs" />
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
internal class LogMessages
2+
{
3+
public string Debug { get; internal set; }
4+
public object Error { get; internal set; }
5+
public string Info { get; internal set; }
6+
public string Warn { get; internal set; }
7+
}

src/GitVersionCore.Tests/LoggerTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public void LoggerObscuresPassword(string protocol)
1717
const string password = "password";
1818
var s = string.Empty;
1919
Action<string> action = info => { s = info; };
20-
using (Logger.AddLoggersTemporarily(action, action, action))
20+
using (Logger.AddLoggersTemporarily(action, action, action, action))
2121
Logger.WriteInfo(string.Format("{0}://{1}:{2}@workspace.visualstudio.com/DefaultCollection/_git/CAS",protocol,username,password));
2222

2323
s.Contains(password).ShouldBe(false);
@@ -29,7 +29,7 @@ public void UsernameWithoutPassword()
2929
var s = string.Empty;
3030
Action<string> action = info => { s = info; };
3131
const string repoUrl = "http://[email protected]/DefaultCollection/_git/CAS";
32-
using (Logger.AddLoggersTemporarily(action, action, action))
32+
using (Logger.AddLoggersTemporarily(action, action, action, action))
3333
Logger.WriteInfo(repoUrl);
3434

3535
s.Contains(repoUrl).ShouldBe(true);

src/GitVersionCore.Tests/ModuleInitializer.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public static class ModuleInitializer
1212
public static void Initialize()
1313
{
1414
Logger.SetLoggers(
15+
s => Console.WriteLine(s),
1516
s => Console.WriteLine(s),
1617
s => Console.WriteLine(s),
1718
s => Console.WriteLine(s));

src/GitVersionCore/GitVersionCore.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@
135135
<Compile Include="OutputVariables\VersionVariables.cs" />
136136
<Compile Include="SemanticVersionExtensions.cs" />
137137
<Compile Include="SemanticVersionFormatValues.cs" />
138+
<Compile Include="VerbosityLevel.cs" />
138139
<Compile Include="VersionFilters\MinDateVersionFilter.cs" />
139140
<Compile Include="VersionFilters\IVersionFilter.cs" />
140141
<Compile Include="VersionFilters\ShaVersionFilter.cs" />

src/GitVersionCore/Logger.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ public static class Logger
99
static readonly Regex ObscurePasswordRegex = new Regex("(https?://)(.+)(:.+@)", RegexOptions.Compiled);
1010
static string indent = string.Empty;
1111

12-
1312
static Logger()
1413
{
1514
Reset();
1615
}
1716

17+
public static Action<string> WriteDebug { get; private set; }
1818
public static Action<string> WriteInfo { get; private set; }
1919
public static Action<string> WriteWarning { get; private set; }
2020
public static Action<string> WriteError { get; private set; }
@@ -41,23 +41,31 @@ static Action<string> ObscurePassword(Action<string> info)
4141
return logAction;
4242
}
4343

44-
public static void SetLoggers(Action<string> info, Action<string> warn, Action<string> error)
44+
public static void SetLoggers(Action<string> debug, Action<string> info, Action<string> warn, Action<string> error)
4545
{
46+
if (debug == null) throw new ArgumentNullException("debug");
4647
if (info == null) throw new ArgumentNullException("info");
4748
if (warn == null) throw new ArgumentNullException("warn");
4849
if (error == null) throw new ArgumentNullException("error");
4950

51+
WriteDebug = LogMessage(ObscurePassword(debug), "DEBUG");
5052
WriteInfo = LogMessage(ObscurePassword(info), "INFO");
5153
WriteWarning = LogMessage(ObscurePassword(warn), "WARN");
5254
WriteError = LogMessage(ObscurePassword(error), "ERROR");
5355
}
5456

55-
public static IDisposable AddLoggersTemporarily(Action<string> info, Action<string> warn, Action<string> error)
57+
public static IDisposable AddLoggersTemporarily(Action<string> debug, Action<string> info, Action<string> warn, Action<string> error)
5658
{
59+
var currentDebug = WriteDebug;
5760
var currentInfo = WriteInfo;
5861
var currentWarn = WriteWarning;
5962
var currentError = WriteError;
60-
SetLoggers(s => {
63+
SetLoggers(s =>
64+
{
65+
debug(s);
66+
currentDebug(s);
67+
}, s =>
68+
{
6169
info(s);
6270
currentInfo(s);
6371
}, s =>
@@ -72,6 +80,7 @@ public static IDisposable AddLoggersTemporarily(Action<string> info, Action<stri
7280

7381
return new ActionDisposable(() =>
7482
{
83+
WriteDebug = currentDebug;
7584
WriteInfo = currentInfo;
7685
WriteWarning = currentWarn;
7786
WriteError = currentError;
@@ -85,6 +94,7 @@ static Action<string> LogMessage(Action<string> logAction, string level)
8594

8695
public static void Reset()
8796
{
97+
WriteDebug = s => { throw new Exception("Debug logger not defined. Attempted to log: " + s); };
8898
WriteInfo = s => { throw new Exception("Info logger not defined. Attempted to log: " + s); };
8999
WriteWarning = s => { throw new Exception("Warning logger not defined. Attempted to log: " + s); };
90100
WriteError = s => { throw new Exception("Error logger not defined. Attempted to log: " + s); };

src/GitVersionCore/VerbosityLevel.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace GitVersion
2+
{
3+
public enum VerbosityLevel
4+
{
5+
None,
6+
Error,
7+
Warn,
8+
Info,
9+
Debug
10+
}
11+
}

0 commit comments

Comments
 (0)