-
Notifications
You must be signed in to change notification settings - Fork 920
Introduce tracing #832
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Introduce tracing #832
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| using System; | ||
| using LibGit2Sharp; | ||
| using LibGit2Sharp.Core; | ||
| using Xunit; | ||
|
|
||
| namespace LibGit2Sharp.Tests | ||
| { | ||
| public class LogFixture | ||
| { | ||
| [Fact] | ||
| public void CanEnableAndDisableLogging() | ||
| { | ||
| // Setting logging produces a log message at level Info, | ||
| // ensure that we catch it. | ||
| LogLevel level = LogLevel.None; | ||
| string message = null; | ||
|
|
||
| GlobalSettings.LogConfiguration = new LogConfiguration(LogLevel.Trace, (l, m) => { level = l; message = m; }); | ||
|
|
||
| Assert.Equal(LogLevel.Info, level); | ||
| Assert.Equal("Logging enabled at level Trace", message); | ||
|
|
||
| // Configuring at Warning and higher means that the | ||
| // message at level Info should not be produced. | ||
| level = LogLevel.None; | ||
| message = null; | ||
|
|
||
| GlobalSettings.LogConfiguration = new LogConfiguration(LogLevel.Warning, (l, m) => { level = l; message = m; }); | ||
|
|
||
| Assert.Equal(LogLevel.None, level); | ||
| Assert.Equal(null, message); | ||
|
|
||
| // Similarly, turning logging off should produce no messages. | ||
| GlobalSettings.LogConfiguration = LogConfiguration.None; | ||
|
|
||
| Assert.Equal(LogLevel.None, level); | ||
| Assert.Equal(null, message); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| using System.Runtime.CompilerServices; | ||
| using LibGit2Sharp.Core; | ||
|
|
||
| namespace LibGit2Sharp | ||
| { | ||
| internal class Log | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about internal class Log
{
private static bool IsEnabled(LogConfiguration configuration, LogLevel level)
{
return (configuration.Level != LogLevel.None && configuration.Level >= level);
}
internal static bool IsEnabled(LogLevel level)
{
return IsEnabled(GlobalSettings.LogConfiguration);
}
internal static void Write(LogLevel level, string message, params object[] args)
{
if (!IsEnabled(GlobalSettings.LogConfiguration))
{
return;
}
configuration.Handler(level, string.Format(message, args));
}
}
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure what the proposal is here...doesn't seem like this code would work?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
@dahlbyk You're right. I forgot to declare a local
Note: This comment has been made on a previous version of this The goal was to avoid the duplicated use of
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for clarifying... I thought the current code looked pretty good. 😀 |
||
| { | ||
| private static bool IsEnabled(LogConfiguration configuration, LogLevel level) | ||
| { | ||
| return (configuration.Level != LogLevel.None && configuration.Level >= level); | ||
| } | ||
|
|
||
| internal static bool IsEnabled(LogLevel level) | ||
| { | ||
| return IsEnabled(GlobalSettings.LogConfiguration, level); | ||
| } | ||
|
|
||
| internal static void Write(LogLevel level, string message, params object[] args) | ||
| { | ||
| LogConfiguration configuration = GlobalSettings.LogConfiguration; | ||
|
|
||
| if (!IsEnabled(configuration, level)) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| configuration.Handler(level, string.Format(message, args)); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| using System; | ||
| using LibGit2Sharp.Core; | ||
| using LibGit2Sharp.Handlers; | ||
|
|
||
| namespace LibGit2Sharp | ||
| { | ||
| /// <summary> | ||
| /// Logging and tracing configuration for libgit2 and LibGit2Sharp. | ||
| /// </summary> | ||
| public sealed class LogConfiguration | ||
| { | ||
| /// <summary> | ||
| /// The default logging configuration, which performs no logging at all. | ||
| /// </summary> | ||
| public static readonly LogConfiguration None = new LogConfiguration { Level = LogLevel.None }; | ||
|
|
||
| /// <summary> | ||
| /// Creates a new logging configuration to call the given | ||
| /// delegate when logging occurs at the given level. | ||
| /// </summary> | ||
| /// <param name="level">Level to log at</param> | ||
| /// <param name="handler">Handler to call when logging occurs</param> | ||
| public LogConfiguration(LogLevel level, LogHandler handler) | ||
| { | ||
| Ensure.ArgumentConformsTo<LogLevel>(level, (t) => { return (level != LogLevel.None); }, "level"); | ||
| Ensure.ArgumentNotNull(handler, "handler"); | ||
|
|
||
| Level = level; | ||
| Handler = handler; | ||
| } | ||
|
|
||
| private LogConfiguration() | ||
| { | ||
| } | ||
|
|
||
| internal LogLevel Level { get; private set; } | ||
| internal LogHandler Handler { get; private set; } | ||
|
|
||
| internal void GitTraceHandler(LogLevel level, IntPtr msg) | ||
| { | ||
| string message = LaxUtf8Marshaler.FromNative(msg); | ||
| Handler(level, message); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Text; | ||
|
|
||
| namespace LibGit2Sharp | ||
| { | ||
| /// <summary> | ||
| /// Available logging levels. When logging is enabled at a particular | ||
| /// level, callers will be provided logging at the given level and all | ||
| /// lower levels. | ||
| /// </summary> | ||
| public enum LogLevel | ||
| { | ||
| /// <summary> | ||
| /// No logging will be provided. | ||
| /// </summary> | ||
| None = 0, | ||
|
|
||
| /// <summary> | ||
| /// Severe errors that may impact the program's execution. | ||
| /// </summary> | ||
| Fatal = 1, | ||
|
|
||
| /// <summary> | ||
| /// Errors that do not impact the program's execution. | ||
| /// </summary> | ||
| Error = 2, | ||
|
|
||
| /// <summary> | ||
| /// Warnings that suggest abnormal data. | ||
| /// </summary> | ||
| Warning = 3, | ||
|
|
||
| /// <summary> | ||
| /// Informational messages about program execution. | ||
| /// </summary> | ||
| Info = 4, | ||
|
|
||
| /// <summary> | ||
| /// Detailed data that allows for debugging. | ||
| /// </summary> | ||
| Debug = 5, | ||
|
|
||
| /// <summary> | ||
| /// Tracing is exceptionally detailed debugging data. | ||
| /// </summary> | ||
| Trace = 6, | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe also assert that a message isn't logged if
new LogConfiguration(LogLevel.Warn, ...)is set?