File tree 10 files changed +231
-0
lines changed 10 files changed +231
-0
lines changed Original file line number Diff line number Diff line change 124
124
<Compile Include =" TestHelpers\SelfCleaningDirectory.cs" />
125
125
<Compile Include =" TestHelpers\SignatureExtensions.cs" />
126
126
<Compile Include =" TestHelpers\SkippableFactAttribute.cs" />
127
+ <Compile Include =" LogFixture.cs" />
127
128
<Compile Include =" TreeDefinitionFixture.cs" />
128
129
<Compile Include =" TreeFixture.cs" />
129
130
<Compile Include =" UnstageFixture.cs" />
Original file line number Diff line number Diff line change
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.
628C
34
+ GlobalSettings . LogConfiguration = LogConfiguration . None ;
35
+
36
+ Assert . Equal ( LogLevel . None , level ) ;
37
+ Assert . Equal ( null , message ) ;
38
+ }
39
+ }
40
+ }
Original file line number Diff line number Diff line change @@ -1496,6 +1496,11 @@ internal static extern int git_tag_delete(
1496
1496
[ DllImport ( libgit2 ) ]
1497
1497
internal static extern void git_threads_shutdown ( ) ;
1498
1498
1499
+ internal delegate void git_trace_cb ( LogLevel level , IntPtr message ) ;
1500
+
1501
+ [ DllImport ( libgit2 ) ]
1502
+ internal static extern int git_trace_set ( LogLevel level , git_trace_cb trace_cb ) ;
1503
+
1499
1504
internal delegate int git_transfer_progress_callback ( ref GitTransferProgress stats , IntPtr payload ) ;
1500
1505
1501
1506
internal delegate int git_transport_cb ( out IntPtr transport , IntPtr remote , IntPtr payload ) ;
Original file line number Diff line number Diff line change @@ -2951,6 +2951,19 @@ public static GitObjectType git_tag_target_type(GitObjectSafeHandle tag)
2951
2951
2952
2952
#endregion
2953
2953
2954
+ #region git_trace_
2955
+
2956
+ public static void git_trace_set ( LogLevel level , NativeMethods . git_trace_cb callback )
2957
+ {
2958
+ using ( ThreadAffinity ( ) )
2959
+ {
2960
+ int res = NativeMethods . git_trace_set ( level , callback ) ;
2961
+ Ensure . ZeroResult ( res ) ;
2962
+ }
2963
+ }
2964
+
2965
+ #endregion
2966
+
2954
2967
#region git_transport_
2955
2968
2956
2969
public static void git_transport_register ( String prefix , IntPtr transport_cb , IntPtr param )
Original file line number Diff line number Diff line change 1
1
using System ;
2
2
using LibGit2Sharp . Core ;
3
+ using LibGit2Sharp . Handlers ;
3
4
4
5
namespace LibGit2Sharp
5
6
{
@@ -10,6 +11,8 @@ public static class GlobalSettings
10
11
{
11
12
private static readonly Lazy < Version > version = new Lazy < Version > ( Version . Build ) ;
12
13
14
+ private static LogConfiguration logConfiguration = LogConfiguration . None ;
15
+
13
16
/// <summary>
14
17
/// Returns all the optional features that were compiled into
15
18
/// libgit2.
@@ -84,5 +87,38 @@ public static void UnregisterSmartSubtransport<T>(SmartSubtransportRegistration<
84
87
Proxy . git_transport_unregister ( registration . Scheme ) ;
85
88
registration . Free ( ) ;
86
89
}
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
+ }
87
123
}
88
124
}
Original file line number Diff line number Diff line change @@ -111,4 +111,12 @@ public enum PackBuilderStage
111
111
/// </summary>
112
112
Deltafying
113
113
}
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 ) ;
114
122
}
Original file line number Diff line number Diff line change 139
139
<Compile Include =" StageOptions.cs" />
140
140
<Compile Include =" StatusOptions.cs" />
141
141
<Compile Include =" SimilarityOptions.cs" />
142
+ <Compile Include =" Log.cs" />
143
+ <Compile Include =" LogConfiguration.cs" />
144
+ <Compile Include =" LogLevel.cs" />
142
145
<Compile Include =" UnbornBranchException.cs" />
143
146
<Compile Include =" LockedFileException.cs" />
144
147
<Compile Include =" Core\GitRepositoryInitOptions.cs" />
Original file line number Diff line number Diff line change
1
+ using System . Runtime . CompilerServices ;
2
+ using LibGit2Sharp . Core ;
3
+
4
+ namespace LibGit2Sharp
5
+ {
6
+ internal class Log
7
+ {
8
+ private static bool IsEnabled ( LogConfiguration configuration , LogLevel level )
9
+ {
10
+ return ( configuration . Level != LogLevel . None && configuration . Level >= level ) ;
11
+ }
12
+
13
+ internal static bool IsEnabled ( LogLevel level )
14
+ {
15
+ return IsEnabled ( GlobalSettings . LogConfiguration , level ) ;
16
+ }
17
+
18
+ internal static void Write ( LogLevel level , string message , params object [ ] args )
19
+ {
20
+ LogConfiguration configuration = GlobalSettings . LogConfiguration ;
21
+
22
+ if ( ! IsEnabled ( configuration , level ) )
23
+ {
24
+ return ;
25
+ }
26
+
27
+ configuration . Handler ( level , string . Format ( message , args ) ) ;
28
+ }
29
+ }
30
+ }
Original file line number Diff line number Diff line change
1
+ using System ;
2
+
F438
using LibGit2Sharp . Core ;
3
+ using LibGit2Sharp . Handlers ;
4
+
5
+ namespace LibGit2Sharp
6
+ {
7
+ /// <summary>
8
+ /// Logging and tracing configuration for libgit2 and LibGit2Sharp.
9
+ /// </summary>
10
+ public sealed class LogConfiguration
11
+ {
12
+ /// <summary>
13
+ /// The default logging configuration, which performs no logging at all.
14
+ /// </summary>
15
+ public static readonly LogConfiguration None = new LogConfiguration { Level = LogLevel . None } ;
16
+
17
+ /// <summary>
18
+ /// Creates a new logging configuration to call the given
19
+ /// delegate when logging occurs at the given level.
20
+ /// </summary>
21
+ /// <param name="level">Level to log at</param>
22
+ /// <param name="handler">Handler to call when logging occurs</param>
23
+ public LogConfiguration ( LogLevel level , LogHandler handler )
24
+ {
25
+ Ensure . ArgumentConformsTo < LogLevel > ( level , ( t ) => { return ( level != LogLevel . None ) ; } , "level" ) ;
26
+ Ensure . ArgumentNotNull ( handler , "handler" ) ;
27
+
28
+ Level = level ;
29
+ Handler = handler ;
30
+ }
31
+
32
+ private LogConfiguration ( )
33
+ {
34
+ }
35
+
36
+ internal LogLevel Level { get ; private set ; }
37
+ internal LogHandler Handler { get ; private set ; }
38
+
39
+ internal void GitTraceHandler ( LogLevel level , IntPtr msg )
40
+ {
41
+ string message = LaxUtf8Marshaler . FromNative ( msg ) ;
42
+ Handler ( level , message ) ;
43
+ }
44
+ }
45
+ }
Original file line number Diff line number Diff line change
1
+ using System ;
2
+ using System . Collections . Generic ;
3
+ using System . Linq ;
4
+ using System . Text ;
5
+
6
+ namespace LibGit2Sharp
7
+ {
8
+ /// <summary>
9
+ /// Available logging levels. When logging is enabled at a particular
10
+ /// level, callers will be provided logging at the given level and all
11
+ /// lower levels.
12
+ /// </summary>
13
+ public enum LogLevel
14
+ {
15
+ /// <summary>
16
+ /// No logging will be provided.
17
+ /// </summary>
18
+ None = 0 ,
19
+
20
+ /// <summary>
21
+ /// Severe errors that may impact the program's execution.
22
+ /// </summary>
23
+ Fatal = 1 ,
24
+
25
+ /// <summary>
26
+ /// Errors that do not impact the program's execution.
27
+ /// </summary>
28
+ Error = 2 ,
29
+
30
+ /// <summary>
31
+ /// Warnings that suggest abnormal data.
32
+ /// </summary>
33
+ Warning = 3 ,
34
+
35
+ /// <summary>
36
+ /// Informational messages about program execution.
37
+ /// </summary>
38
+ Info = 4 ,
39
+
40
+ /// <summary>
41
+ /// Detailed data that allows for debugging.
42
+ /// </summary>
43
+ Debug = 5 ,
44
+
45
+ /// <summary>
46
+ /// Tracing is exceptionally detailed debugging data.
47
+ /// </summary>
48
+ Trace = 6 ,
49
+ }
50
+ }
You can’t perform that action at this time.
0 commit comments