-
Notifications
You must be signed in to change notification settings - Fork 54
feat(swift-sdk): iOS simluator writes logs to disk #3785
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
Open
ZocoLini
wants to merge
1
commit into
v3.1-dev
Choose a base branch
from
feat/swift-sdk-logs
base: v3.1-dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+499
−0
Open
Changes from all commits
Commits
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,170 @@ | ||
| use std::ffi::CStr; | ||
| use std::fs; | ||
| use std::path::{Path, PathBuf}; | ||
| use std::sync::Mutex; | ||
|
|
||
| use tracing_subscriber::layer::SubscriberExt; | ||
| use tracing_subscriber::util::SubscriberInitExt; | ||
| use tracing_subscriber::Layer; | ||
|
|
||
| const VERSION: &str = env!("CARGO_PKG_VERSION"); | ||
| const GIT_COMMIT: &str = env!("PLATFORM_WALLET_GIT_COMMIT"); | ||
| const GIT_DIRTY: &str = env!("PLATFORM_WALLET_GIT_DIRTY"); | ||
|
|
||
| /// Install the global `tracing` subscriber. Returns | ||
| /// `true` only when this call installed the subscriber. | ||
| /// | ||
| /// A `false` return covers both (a) a subscriber was already | ||
| /// installed (first init wins) and (b) path couldn't be opened. | ||
| /// | ||
| /// # Safety | ||
| /// - `path` must be a valid pointer to a null terminated string | ||
| /// or null. | ||
| #[no_mangle] | ||
| pub unsafe extern "C" fn platform_wallet_enable_file_logging( | ||
| level: u8, | ||
| path: *const std::ffi::c_char, | ||
| ) -> bool { | ||
| if path.is_null() { | ||
| return false; | ||
| } | ||
|
|
||
| let Some(path) = CStr::from_ptr(path).to_str().ok().map(PathBuf::from) else { | ||
| return false; | ||
| }; | ||
|
|
||
| enable_file_logging(level_to_directive(level), &path) | ||
| } | ||
|
|
||
| fn enable_file_logging(log_level: &str, path: &Path) -> bool { | ||
| let Some(f_sdk) = open_file(path.join("dash_sdk").join("run.log")) else { | ||
| return false; | ||
| }; | ||
| let Some(f_pw) = open_file(path.join("platform_wallet").join("run.log")) else { | ||
| return false; | ||
| }; | ||
| let Some(f_spv) = open_file(path.join("dash_spv").join("run.log")) else { | ||
| return false; | ||
| }; | ||
| let Some(f_kw) = open_file(path.join("key_wallet").join("run.log")) else { | ||
| return false; | ||
| }; | ||
| let Some(f_grpc) = open_file(path.join("grpc").join("run.log")) else { | ||
| return false; | ||
| }; | ||
|
|
||
| let l_sdk = tracing_subscriber::fmt::layer() | ||
| .with_writer(Mutex::new(f_sdk)) | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| .with_ansi(false) | ||
| .with_filter(tracing_subscriber::EnvFilter::new(format!( | ||
| "dash_sdk={log_level},rs_sdk_ffi={log_level}" | ||
| ))); | ||
|
ZocoLini marked this conversation as resolved.
|
||
|
|
||
| let l_pw = tracing_subscriber::fmt::layer() | ||
| .with_writer(Mutex::new(f_pw)) | ||
| .with_ansi(false) | ||
| .with_filter(tracing_subscriber::EnvFilter::new(format!( | ||
| "platform_wallet={log_level},platform_wallet_ffi={log_level}" | ||
| ))); | ||
|
|
||
| let l_spv = tracing_subscriber::fmt::layer() | ||
| .with_writer(Mutex::new(f_spv)) | ||
| .with_ansi(false) | ||
| .with_filter(tracing_subscriber::EnvFilter::new(format!( | ||
| "dash_spv={log_level}" | ||
| ))); | ||
|
|
||
| let l_kw = tracing_subscriber::fmt::layer() | ||
| .with_writer(Mutex::new(f_kw)) | ||
| .with_ansi(false) | ||
| .with_filter(tracing_subscriber::EnvFilter::new(format!( | ||
| "key_wallet={log_level}" | ||
| ))); | ||
|
|
||
| let l_grpc = tracing_subscriber::fmt::layer() | ||
| .with_writer(Mutex::new(f_grpc)) | ||
| .with_ansi(false) | ||
| .with_filter(tracing_subscriber::EnvFilter::new(format!( | ||
| "dapi_grpc={log_level},tonic={log_level},h2={log_level},\ | ||
| hyper={log_level},tower={log_level}" | ||
| ))); | ||
|
|
||
| if fs::write(path.join("build_info.txt"), build_info_string()).is_err() { | ||
| return false; | ||
| } | ||
|
|
||
| let stdout_layer = tracing_subscriber::fmt::layer().with_filter(broad_env_filter(log_level)); | ||
|
|
||
| if tracing_subscriber::registry() | ||
| .with(stdout_layer) | ||
| .with(l_sdk) | ||
| .with(l_pw) | ||
| .with(l_spv) | ||
| .with(l_kw) | ||
| .with(l_grpc) | ||
| .try_init() | ||
| .is_err() | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| tracing::info!(level = log_level, "file logging enabled"); | ||
| true | ||
| } | ||
|
ZocoLini marked this conversation as resolved.
ZocoLini marked this conversation as resolved.
ZocoLini marked this conversation as resolved.
ZocoLini marked this conversation as resolved.
|
||
|
|
||
| fn open_file(path: PathBuf) -> Option<fs::File> { | ||
| if let Some(parent) = path.parent() { | ||
| fs::create_dir_all(parent).ok()?; | ||
| } | ||
|
|
||
| fs::OpenOptions::new() | ||
| .create(true) | ||
| .append(true) | ||
| .open(&path) | ||
| .ok() | ||
| } | ||
|
|
||
| fn broad_env_filter(log_level: &str) -> tracing_subscriber::EnvFilter { | ||
| let directives = format!( | ||
| "dash_sdk={log_level},rs_sdk={log_level},rs_sdk_ffi={log_level},\ | ||
| platform_wallet={log_level},platform_wallet_ffi={log_level},\ | ||
| dash_spv={log_level},key_wallet={log_level},\ | ||
| dapi_grpc={log_level},h2={log_level},tower={log_level},\ | ||
| hyper={log_level},tonic={log_level}" | ||
| ); | ||
|
|
||
| tracing_subscriber::EnvFilter::try_from_default_env() | ||
| .unwrap_or_else(|_| tracing_subscriber::EnvFilter::new(directives)) | ||
| } | ||
|
|
||
| fn level_to_directive(level: u8) -> &'static str { | ||
| match level { | ||
| 0 => "error", | ||
| 1 => "warn", | ||
| 2 => "info", | ||
| 3 => "debug", | ||
| 4 => "trace", | ||
| _ => "info", | ||
| } | ||
| } | ||
|
|
||
| fn build_info_string() -> String { | ||
| let dirty = match GIT_DIRTY { | ||
| "0" => "no", | ||
| "1" => "yes", | ||
| _ => "unknown", | ||
| }; | ||
| let mut out = format!( | ||
| "platform-wallet-version: {VERSION}\n\ | ||
| git-commit: {GIT_COMMIT}\n\ | ||
| git-dirty: {dirty}\n\ | ||
| # to reproduce: git checkout {GIT_COMMIT}\n" | ||
| ); | ||
| if dirty == "yes" { | ||
| out.push_str( | ||
| "# WARNING: this build had uncommitted changes; the commit hash above does NOT \ | ||
| fully describe the source state.\n", | ||
| ); | ||
| } | ||
| out | ||
| } | ||
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| logs-*/ |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.