-
Notifications
You must be signed in to change notification settings - Fork 394
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
Update log
crate and create shim method(s) between git2
's tracing and log
#1066
Closed
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b4daf13
Update log crate and add shim methods between git2's tracing and log
vcfxb d316b0e
Replace tabs with spaces
vcfxb 4db09e4
Remove trailing whitespace
vcfxb 9e7bae0
fix formatting
vcfxb 396c5d2
Remove function converting `TraceLevel` to `LogLevelFilter`.
vcfxb 5521816
Do not use `log` types in public functions.
vcfxb 8d87dd5
cargo fmt
vcfxb eba4cbd
Merge remote-tracking branch 'origin/master'
vcfxb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
use std::sync::atomic::{AtomicUsize, Ordering}; | ||
|
||
use libc::c_char; | ||
use log::RecordBuilder; | ||
|
||
use crate::{panic, raw, util::Binding}; | ||
|
||
|
@@ -57,14 +58,36 @@ impl Binding for TraceLevel { | |
} | ||
} | ||
|
||
impl TraceLevel { | ||
/// Attempt to convert this [TraceLevel] to a [log::LevelFilter]. | ||
/// | ||
/// This function is not public to avoid having a public dependency on [`log`]. | ||
/// | ||
/// This is done trivially with two exceptions: | ||
/// - [TraceLevel::None] goes to [None] | ||
/// - [TraceLevel::Fatal] goes to [log::Level::Error]. | ||
const fn as_log_level(self) -> Option<log::Level> { | ||
use log::Level; | ||
|
||
match self { | ||
Self::None => None, | ||
Self::Fatal | Self::Error => Some(Level::Error), | ||
Self::Warn => Some(Level::Warn), | ||
Self::Info => Some(Level::Info), | ||
Self::Debug => Some(Level::Debug), | ||
Self::Trace => Some(Level::Trace), | ||
} | ||
} | ||
} | ||
|
||
//TODO: pass raw &[u8] and leave conversion to consumer (breaking API) | ||
/// Callback type used to pass tracing events to the subscriber. | ||
/// see `trace_set` to register a subscriber. | ||
pub type TracingCb = fn(TraceLevel, &str); | ||
|
||
static CALLBACK: AtomicUsize = AtomicUsize::new(0); | ||
|
||
/// | ||
/// Set the tracing callback. | ||
pub fn trace_set(level: TraceLevel, cb: TracingCb) -> bool { | ||
CALLBACK.store(cb as usize, Ordering::SeqCst); | ||
|
||
|
@@ -75,6 +98,27 @@ pub fn trace_set(level: TraceLevel, cb: TracingCb) -> bool { | |
return true; | ||
} | ||
|
||
/// Passes [trace_set] a shim function to pass tracing info to the [log] crate. | ||
pub fn trace_shim_log_crate() { | ||
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. |
||
// Use `trace` to get all tracing events -- let the user configure filtering | ||
// through the `log` crate. | ||
trace_set(TraceLevel::Trace, |level, msg| { | ||
// Convert the trace level to a log level. | ||
let log_level = level | ||
.as_log_level() | ||
.expect("libgit2 should not produce tracing events with level=None"); | ||
|
||
// Build a record to pass to the logger. | ||
let mut record_builder = RecordBuilder::new(); | ||
|
||
// Set the target and level. | ||
record_builder.target("libgit2").level(log_level); | ||
|
||
// Log the trace event to the global logger. | ||
log::logger().log(&record_builder.args(format_args!("{}", msg)).build()); | ||
}); | ||
} | ||
|
||
extern "C" fn tracing_cb_c(level: raw::git_trace_level_t, msg: *const c_char) { | ||
let cb = CALLBACK.load(Ordering::SeqCst); | ||
panic::wrap(|| unsafe { | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This has the same symptom. Different users may have different thoughts on mapping Fatal and Error to different levels. We try not to decide this kind of application logic for users.