File tree Expand file tree Collapse file tree 10 files changed +231
-0
lines changed
Expand file tree Collapse file tree 10 files changed +231
-0
lines changed Original file line number Diff line number Diff line change 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" />
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.
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(
14961496 [ DllImport ( libgit2 ) ]
14971497 internal static extern void git_threads_shutdown ( ) ;
14981498
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+
14991504 internal delegate int git_transfer_progress_callback ( ref GitTransferProgress stats , IntPtr payload ) ;
15001505
15011506 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)
29512951
29522952 #endregion
29532953
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+
29542967 #region git_transport_
29552968
29562969 public static void git_transport_register ( String prefix , IntPtr transport_cb , IntPtr param )
Original file line number Diff line number Diff line change 11using System ;
22using LibGit2Sharp . Core ;
3+ using LibGit2Sharp . Handlers ;
34
45namespace 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}
Original file line number Diff line number Diff 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}
Original file line number Diff line number Diff line change 139139 <Compile Include =" StageOptions.cs" />
140140 <Compile Include =" StatusOptions.cs" />
141141 <Compile Include =" SimilarityOptions.cs" />
142+ <Compile Include =" Log.cs" />
143+ <Compile Include =" LogConfiguration.cs" />
144+ <Compile Include =" LogLevel.cs" />
142145 <Compile Include =" UnbornBranchException.cs" />
143146 <Compile Include =" LockedFileException.cs" />
144147 <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+ 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