Skip to content

Commit a59d9ce

Browse files
author
Edward Thomson
committed
Introduce tracing
1 parent f3a6b7e commit a59d9ce

18 files changed

Lines changed: 245 additions & 6 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: 2 additions & 1 deletion
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="TraceFixture.cs" />
127128
<Compile Include="TreeDefinitionFixture.cs" />
128129
<Compile Include="TreeFixture.cs" />
129130
<Compile Include="UnstageFixture.cs" />
@@ -150,4 +151,4 @@
150151
<Target Name="AfterBuild">
151152
</Target>
152153
-->
153-
</Project>
154+
</Project>

LibGit2Sharp.Tests/TestHelpers/DirectoryHelper.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public static void DeleteDirectory(string directoryPath)
4747

4848
if (!Directory.Exists(directoryPath))
4949
{
50-
Trace.WriteLine(
50+
System.Diagnostics.Trace.WriteLine(
5151
string.Format("Directory '{0}' is missing and can't be removed.",
5252
directoryPath));
5353

@@ -75,7 +75,7 @@ public static void DeleteDirectory(string directoryPath)
7575
}
7676
catch (IOException)
7777
{
78-
Trace.WriteLine(string.Format("{0}The directory '{1}' could not be deleted!" +
78+
System.Diagnostics.Trace.WriteLine(string.Format("{0}The directory '{1}' could not be deleted!" +
7979
"{0}Most of the time, this is due to an external process accessing the files in the temporary repositories created during the test runs, and keeping a handle on the directory, thus preventing the deletion of those files." +
8080
"{0}Known and common causes include:" +
8181
"{0}- Windows Search Indexer (go to the Indexing Options, in the Windows Control Panel, and exclude the bin folder of LibGit2Sharp.Tests)" +

LibGit2Sharp.Tests/TraceFixture.cs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using System;
2+
using LibGit2Sharp;
3+
using LibGit2Sharp.Core;
4+
using Xunit;
5+
6+
namespace LibGit2Sharp.Tests
7+
{
8+
public class TraceFixture
9+
{
10+
[Fact]
11+
public void CanEnableAndDisableTracing()
12+
{
13+
TraceLevel level = TraceLevel.None;
14+
string message = null;
15+
16+
GlobalSettings.TraceConfiguration = new TraceConfiguration(TraceLevel.Info, (l, m) => { level = l; message = m; });
17+
Trace.Write(TraceLevel.Info, "This is an informative trace.");
18+
19+
Assert.Equal(TraceLevel.Info, level);
20+
Assert.Equal("This is an informative trace.", message);
21+
22+
GlobalSettings.TraceConfiguration = TraceConfiguration.None;
23+
Trace.Write(TraceLevel.Error, "This will not be traced since tracing is disabled.");
24+
25+
Assert.Equal(TraceLevel.Info, level);
26+
Assert.Equal("This is an informative trace.", message);
27+
}
28+
29+
[Fact]
30+
public void CanEasilyDetermineIfTracingIsEnabled()
31+
{
32+
GlobalSettings.TraceConfiguration = new TraceConfiguration(TraceLevel.Info, (l, m) => { });
33+
34+
Assert.Equal(true, Trace.IsEnabled(TraceLevel.Fatal));
35+
Assert.Equal(true, Trace.IsEnabled(TraceLevel.Error));
36+
Assert.Equal(true, Trace.IsEnabled(TraceLevel.Warning));
37+
Assert.Equal(true, Trace.IsEnabled(TraceLevel.Info));
38+
Assert.Equal(false, Trace.IsEnabled(TraceLevel.Debug));
39+
Assert.Equal(false, Trace.IsEnabled(TraceLevel.Trace));
40+
41+
GlobalSettings.TraceConfiguration = TraceConfiguration.None;
42+
43+
Assert.Equal(false, Trace.IsEnabled(TraceLevel.Fatal));
44+
Assert.Equal(false, Trace.IsEnabled(TraceLevel.Error));
45+
Assert.Equal(false, Trace.IsEnabled(TraceLevel.Warning));
46+
Assert.Equal(false, Trace.IsEnabled(TraceLevel.Info));
47+
Assert.Equal(false, Trace.IsEnabled(TraceLevel.Debug));
48+
Assert.Equal(false, Trace.IsEnabled(TraceLevel.Trace));
49+
}
50+
}
51+
}

LibGit2Sharp/Core/Handles/SafeHandleBase.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ protected override void Dispose(bool disposing)
3535
{
3636
if (!disposing && !IsInvalid)
3737
{
38-
Trace.WriteLine(string.Format(CultureInfo.InvariantCulture, "A {0} handle wrapper has not been properly disposed.", GetType().Name));
38+
System.Diagnostics.Trace.WriteLine(string.Format(CultureInfo.InvariantCulture, "A {0} handle wrapper has not been properly disposed.", GetType().Name));
3939
#if LEAKS
4040
Trace.WriteLine(trace);
4141
#endif
42-
Trace.WriteLine("");
42+
System.Diagnostics.Trace.WriteLine("");
4343
}
4444

4545
base.Dispose(disposing);

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(TraceLevel level, IntPtr message);
1496+
1497+
[DllImport(libgit2)]
1498+
internal static extern int git_trace_set(TraceLevel level, IntPtr 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: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2937,6 +2937,21 @@ public static GitObjectType git_tag_target_type(GitObjectSafeHandle tag)
29372937

29382938
#endregion
29392939

2940+
#region git_trace_
2941+
2942+
public static void git_trace_set(TraceLevel level, NativeMethods.git_trace_cb callback)
2943+
{
2944+
using (ThreadAffinity())
2945+
{
2946+
IntPtr callback_ptr = callback != null ? Marshal.GetFunctionPointerForDelegate(callback) : IntPtr.Zero;
2947+
int res = NativeMethods.git_trace_set(level, callback_ptr);
2948+
2949+
Ensure.ZeroResult(res);
2950+
}
2951+
}
2952+
2953+
#endregion
2954+
29402955
#region git_transport_
29412956

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

0 commit comments

Comments
 (0)