Skip to content

Commit

Permalink
Add a warning for file logging being disabled
Browse files Browse the repository at this point in the history
Closes #1119
  • Loading branch information
borrrden committed Jan 24, 2019
1 parent c3a4e97 commit bbb6450
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 3 deletions.
8 changes: 8 additions & 0 deletions src/Couchbase.Lite.Shared/API/Database/Database.cs
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,17 @@ public Database([NotNull]string name, [CanBeNull]DatabaseConfiguration configura
{
Name = CBDebug.MustNotBeNull(WriteLog.To.Database, Tag, nameof(name), name);
Config = configuration?.Freeze() ?? new DatabaseConfiguration(true);
RunOnce.GetInstance(nameof(Database)).Run(CheckFileLogger);
Open();
}

private void CheckFileLogger()
{
if (Log.File.Config == null) {
WriteLog.To.Database.W("Logging", "Database.Log.File.Config is null, meaning file logging is disabled. Log files required for product support are not being generated.");
}
}

internal Database([NotNull]Database other)
: this(other.Name, other.Config)
{
Expand Down
4 changes: 4 additions & 0 deletions src/Couchbase.Lite.Shared/API/Log/FileLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,10 @@ public LogFileConfiguration Config
{
get => _config;
set {
if (value == null) {
WriteLog.To.Database.W("Logging", "Database.Log.File.Config is now null, meaning file logging is disabled. Log files required for product support are not being generated.");
}

_config = value?.Freeze();
UpdateConfig();
}
Expand Down
1 change: 1 addition & 0 deletions src/Couchbase.Lite.Shared/Couchbase.Lite.Shared.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -175,5 +175,6 @@
<Compile Include="$(MSBuildThisFileDirectory)Util\NonNullDictionary.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Util\OptionsDictionary.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Util\RefCountedDisposable.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Util\RunOnce.cs" />
</ItemGroup>
</Project>
66 changes: 66 additions & 0 deletions src/Couchbase.Lite.Shared/Util/RunOnce.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using System;
using System.Collections.Concurrent;

using Couchbase.Lite.Internal.Logging;

using JetBrains.Annotations;

namespace Couchbase.Lite.Util
{
internal sealed class RunOnce
{
#region Constants

private const string Tag = nameof(RunOnce);

[NotNull] private static readonly ConcurrentDictionary<string, RunOnce> Instances =
new ConcurrentDictionary<string, RunOnce>();

#endregion

#region Variables

[NotNull]
private readonly string _id;

[NotNull]
private readonly ConcurrentDictionary<Action, bool> _seen = new ConcurrentDictionary<Action, bool>();

#endregion

#region Constructors

private RunOnce([NotNull]string identifier)
{
_id = identifier;
}

#endregion

#region Public Methods

public static RunOnce GetInstance([NotNull]string identifier)
{
return Instances.GetOrAdd(identifier, k => new RunOnce(k));
}

public void Run([NotNull]Action a)
{
if (_seen.TryAdd(a, false)) {
WriteLog.To.Database.V(Tag, "Executing logic for {0}", _id);
a();
}
}

#endregion

#region Overrides

public override string ToString()
{
return $"RunOnce => [{_id}]";
}

#endregion
}
}
41 changes: 41 additions & 0 deletions src/Couchbase.Lite.Tests.Shared/LogTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
using FluentAssertions;

using JetBrains.Annotations;

using Test.Util;
#if !WINDOWS_UWP
using Xunit;
using Xunit.Abstractions;
Expand All @@ -51,6 +53,25 @@ static LogTest()
}
#endif

#if !WINDOWS_UWP
public LogTest(ITestOutputHelper output)
{
Database.Log.Custom = new XunitLogger(output) { Level = LogLevel.Info };
}
#endif

#if WINDOWS_UWP
private TestContext _testContext;
public TestContext TestContext
{
get => _testContext;
set {
_testContext = value;
Database.Log.Custom = new MSTestLogger(_testContext) { Level = LogLevel.Info };
}
}
#endif

[Fact]
public void TestDefaultLogFormat()
{
Expand Down Expand Up @@ -295,6 +316,26 @@ public void TestConsoleLoggingDomains()
Database.Log.Console.Domains = LogDomain.All;
Database.Log.Console.Level = LogLevel.Warning;
}

[Fact]
public void TestFileLogDisabledWarning()
{
Database.Log.File.Config.Should().BeNull();
var sw = new StringWriter();
Console.SetOut(sw);
using (var db = new Database("tmp")) {
sw.ToString().Contains("file logging is disabled").Should().BeTrue();
db.Delete();
}

sw.Dispose();
sw = new StringWriter();
Console.SetOut(sw);
Database.Log.File.Config = new LogFileConfiguration("foo");
Database.Log.File.Config = null;
sw.ToString().Contains("file logging is disabled").Should().BeTrue();
Console.SetOut(new StreamWriter(Console.OpenStandardOutput()));
}
#endif

[Fact]
Expand Down
4 changes: 2 additions & 2 deletions src/Couchbase.Lite.Tests.Shared/PerfTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public virtual TestContext TestContext
get => _output;
set {
_output = value;
Database.Log.Custom = new MSTestLogger(value);
Database.Log.Custom = new MSTestLogger(value) { Level = LogLevel.Info };
}
}
#endif
Expand All @@ -71,7 +71,7 @@ static PerfTest()
protected PerfTest(ITestOutputHelper output)
{
_output = output;
Database.Log.Custom = new XunitLogger(output);
Database.Log.Custom = new XunitLogger(output) { Level = LogLevel.Info };
}
#endif

Expand Down
2 changes: 1 addition & 1 deletion src/Couchbase.Lite.Tests.Shared/TestCase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public TestContext TestContext
get => _testContext;
set {
_testContext = value;
Database.Log.Custom = new MSTestLogger(_testContext);
Database.Log.Custom = new MSTestLogger(_testContext) { Level = LogLevel.Info };
}
}
#endif
Expand Down

0 comments on commit bbb6450

Please sign in to comment.