Skip to content

Commit 0977c5e

Browse files
author
Edward Thomson
committed
Introduce tracing
1 parent 0e5bfbc commit 0977c5e

16 files changed

Lines changed: 232 additions & 2 deletions
0 Bytes
Binary file not shown.
-24 KB
Binary file not shown.
0 Bytes
Binary file not shown.
-24 KB
Binary file not shown.

LibGit2Sharp.Tests/LibGit2Sharp.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@
124124
<Compile Include="TestHelpers\SelfCleaningDirectory.cs" />
125125
<Compile Include="TestHelpers\SignatureExtensions.cs" />
126126
<Compile Include="TestHelpers\SkippableFactAttribute.cs" />
127+
<Compile Include="LogFixture.cs" />
127128
<Compile Include="TreeDefinitionFixture.cs" />
128129
<Compile Include="TreeFixture.cs" />
129130
<Compile Include="UnstageFixture.cs" />

LibGit2Sharp.Tests/LogFixture.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System;
2+
using LibGit2Sharp;
3+
using LibGit2Sharp.Core;
4+
using Xunit;
5+
6+
namespace LibGit2Sharp.Tests
7+
{
8+
public class LogFixture
9+
{
10+
[Fact]
11+
public void CanEnableAndDisableLogging()
12+
{
13+
// Setting logging produces a log message at level Info,
14+
// ensure that we catch it.
15+
LogLevel level = LogLevel.None;
16+
string message = null;
17+
18+
GlobalSettings.LogConfiguration = new LogConfiguration(LogLevel.Trace, (l, m) => { level = l; message = m; });
19+
20+
Assert.Equal(LogLevel.Info, level);
21+
Assert.Equal("Logging enabled at level Trace", message);
22+
23+
// Configuring at Warning and higher means that the
24+
// message at level Info should not be produced.
25+
level = LogLevel.None;
26+
message = null;
27+
28+
GlobalSettings.LogConfiguration = new LogConfiguration(LogLevel.Warning, (l, m) => { level = l; message = m; });
29+
30+
Assert.Equal(LogLevel.None, level);
31+
Assert.Equal(null, message);
32+
33+
// Similarly, turning logging off should produce no messages.
34+
GlobalSettings.LogConfiguration = LogConfiguration.None;
35+
36+
Assert.Equal(LogLevel.None, level);
37+
Assert.Equal(null, message);
38+
}
39+
}
40+
}

LibGit2Sharp/Core/NativeMethods.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1492,6 +1492,11 @@ internal static extern int git_tag_delete(
14921492
[DllImport(libgit2)]
14931493
internal static extern void git_threads_shutdown();
14941494

1495+
internal delegate void git_trace_cb(LogLevel level, IntPtr message);
1496+
1497+
[DllImport(libgit2)]
1498+
internal static extern int git_trace_set(LogLevel level, git_trace_cb trace_cb);
1499+
14951500
internal delegate int git_transfer_progress_callback(ref GitTransferProgress stats, IntPtr payload);
14961501

14971502
internal delegate int git_transport_cb(out IntPtr transport, IntPtr remote, IntPtr payload);

LibGit2Sharp/Core/Proxy.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2943,6 +2943,19 @@ public static GitObjectType git_tag_target_type(GitObjectSafeHandle tag)
29432943

29442944
#endregion
29452945

2946+
#region git_trace_
2947+
2948+
public static void git_trace_set(LogLevel level, NativeMethods.git_trace_cb callback)
2949+
{
2950+
using (ThreadAffinity())
2951+
{
2952+
int res = NativeMethods.git_trace_set(level, callback);
2953+
Ensure.ZeroResult(res);
2954+
}
2955+
}
2956+
2957+
#endregion
2958+
29462959
#region git_transport_
29472960

29482961
public static void git_transport_register(String prefix, IntPtr transport_cb, IntPtr param)

LibGit2Sharp/GlobalSettings.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using LibGit2Sharp.Core;
3+
using LibGit2Sharp.Handlers;
34

45
namespace LibGit2Sharp
56
{
@@ -10,6 +11,8 @@ public static class GlobalSettings
1011
{
1112
private static readonly Lazy<Version> version = new Lazy<Version>(Version.Build);
1213

14+
private static LogConfiguration logConfiguration = LogConfiguration.None;
15+
1316
/// <summary>
1417
/// Returns all the optional features that were compiled into
1518
/// libgit2.
@@ -84,5 +87,38 @@ public static void UnregisterSmartSubtransport<T>(SmartSubtransportRegistration<
8487
Proxy.git_transport_unregister(registration.Scheme);
8588
registration.Free();
8689
}
90+
91+
/// <summary>
92+
/// Registers a new <see cref="LogConfiguration"/> to receive
93+
/// information logging information from libgit2 and LibGit2Sharp.
94+
///
95+
/// Note that this configuration is global to an entire process
96+
/// and does not honor application domains.
97+
/// </summary>
98+
public static LogConfiguration LogConfiguration
99+
{
100+
set
101+
{
102+
Ensure.ArgumentNotNull(value, "value");
103+
104+
logConfiguration = value;
105+
106+
if (logConfiguration.Level == LogLevel.None)
107+
{
108+
Proxy.git_trace_set(0, null);
109+
}
110+
else
111+
{
112+
Proxy.git_trace_set(value.Level, value.GitTraceHandler);
113+
114+
Log.Write(LogLevel.Info, "Logging enabled at level {0}", value.Level);
115+
}
116+
}
117+
118+
get
119+
{
120+
return logConfiguration;
121+
}
122+
}
87123
}
88124
}

LibGit2Sharp/Handlers.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,12 @@ public enum PackBuilderStage
111111
/// </summary>
112112
Deltafying
113113
}
114+
115+
/// <summary>
116+
/// Delegate definition for logging. This callback will be used to
117+
/// write logging messages in libgit2 and LibGit2Sharp.
118+
/// </summary>
119+
/// <param name="level">The level of the log message.</param>
120+
/// <param name="message">The log message.</param>
121+
public delegate void LogHandler(LogLevel level, string message);
114122
}

0 commit comments

Comments
 (0)