Skip to content

Commit 959453b

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

18 files changed

+246
-6
lines changed
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="TraceFixture.cs" />
127128
<Compile Include="TreeDefinitionFixture.cs" />
128129
<Compile Include="TreeFixture.cs" />
129130
<Compile Include="UnstageFixture.cs" />

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: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
level = TraceLevel.None;
23+
message = null;
24+
25+
GlobalSettings.TraceConfiguration = TraceConfiguration.None;
26+
Trace.Write(TraceLevel.Error, "This will not be traced since tracing is disabled.");
27+
28+
Assert.Equal(TraceLevel.None, level);
29+
Assert.Equal(null, message);
30+
}
31+
32+
[Fact]
33+
public void CanEasilyDetermineIfTracingIsEnabled()
34+
{
35+
GlobalSettings.TraceConfiguration = new TraceConfiguration(TraceLevel.Info, (l, m) => { });
36+
37+
Assert.Equal(true, Trace.IsEnabled(TraceLevel.Fatal));
38+
Assert.Equal(true, Trace.IsEnabled(TraceLevel.Error));
39+
Assert.Equal(true, Trace.IsEnabled(TraceLevel.Warning));
40+
Assert.Equal(true, Trace.IsEnabled(TraceLevel.Info));
41+
Assert.Equal(false, Trace.IsEnabled(TraceLevel.Debug));
42+
Assert.Equal(false, Trace.IsEnabled(TraceLevel.Trace));
43+
44+
GlobalSettings.TraceConfiguration = TraceConfiguration.None;
45+
46+
Assert.Equal(false, Trace.IsEnabled(TraceLevel.Fatal));
47+
Assert.Equal(false, Trace.IsEnabled(TraceLevel.Error));
48+
Assert.Equal(false, Trace.IsEnabled(TraceLevel.Warning));
49+
Assert.Equal(false, Trace.IsEnabled(TraceLevel.Info));
50+
Assert.Equal(false, Trace.IsEnabled(TraceLevel.Debug));
51+
Assert.Equal(false, Trace.IsEnabled(TraceLevel.Trace));
52+
}
53+
}
54+
}

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, 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
@@ -2937,6 +2937,19 @@ 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+
int res = NativeMethods.git_trace_set(level, callback);
2947+
Ensure.ZeroResult(res);
2948+
}
2949+
}
2950+
2951+
#endregion
2952+
29402953
#region git_transport_
29412954

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

0 commit comments

Comments
 (0)