-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adds logging for simulation and adds JSON string representation of Di…
…agnosticEvents
- Loading branch information
1 parent
2d72179
commit 55e9489
Showing
13 changed files
with
139 additions
and
23 deletions.
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 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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
use std::sync::{Arc, Mutex}; | ||
use tracing::{Event, Subscriber}; | ||
use tracing_subscriber::layer::{Context, SubscriberExt}; | ||
|
||
struct TestSubscriber { | ||
logs: Arc<Mutex<Vec<String>>>, | ||
} | ||
|
||
impl<S: Subscriber> tracing_subscriber::Layer<S> for TestSubscriber { | ||
fn on_event(&self, event: &Event<'_>, _ctx: Context<'_, S>) { | ||
// Capture the event data | ||
let mut logs = self.logs.lock().unwrap(); | ||
logs.push(format!("{:?}", event)); | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_diagnostic_events_logging() { | ||
let logs = Arc::new(Mutex::new(Vec::new())); | ||
let subscriber = TestSubscriber { logs: logs.clone() }; | ||
|
||
tracing::subscriber::with_default(tracing_subscriber::registry().with(subscriber), || { | ||
let events = vec![ | ||
"AAAAAAAAAAAAAAAAAAAAAgAAAAAAAAADAAAADwAAAAdmbl9jYWxsAAAAAA0AAAAgfKvD/pIJPlRnGd3RKaBZSHfoq/nJbJSYxkVTScSbhuYAAAAPAAAABGRlY3IAAAAB".to_string(), | ||
"AAAAAAAAAAAAAAABfKvD/pIJPlRnGd3RKaBZSHfoq/nJbJSYxkVTScSbhuYAAAACAAAAAAAAAAEAAAAPAAAAA2xvZwAAAAAQAAAAAQAAAAIAAAAOAAAACWNvdW50OiB7fQAAAAAAAAMAAAAA".to_string(), | ||
"AAAAAAAAAAAAAAABfKvD/pIJPlRnGd3RKaBZSHfoq/nJbJSYxkVTScSbhuYAAAACAAAAAAAAAAIAAAAPAAAABWVycm9yAAAAAAAAAgAAAAEAAAAGAAAAEAAAAAEAAAACAAAADgAAACdWTSBjYWxsIHRyYXBwZWQ6IFVucmVhY2hhYmxlQ29kZVJlYWNoZWQAAAAADwAAAARkZWNy".to_string(), | ||
]; | ||
soroban_cli::log::sim_diagnostic_events(&events, tracing::Level::ERROR); | ||
}); | ||
|
||
let captured_logs = logs.lock().unwrap(); | ||
assert!(captured_logs.iter().any(|log| log.contains("0: \"AAAAAAAAAAAAAAAAAAAAAgAAAAAAAAADAAAADwAAAAdmbl9jYWxsAAAAAA0AAAAgfKvD/pIJPlRnGd3RKaBZSHfoq/nJbJSYxkVTScSbhuYAAAAPAAAABGRlY3IAAAAB\" {\"in_successful_contract_call\":false,\"event\":{\"ext\":\"v0\",\"contract_id\":null,\"type_\":\"diagnostic\",\"body\":{\"v0\":{\"topics\":[{\"symbol\":\"fn_call\"},{\"bytes\":\"7cabc3fe92093e546719ddd129a0594877e8abf9c96c9498c6455349c49b86e6\"},{\"symbol\":\"decr\"}],\"data\":\"void\"}}}}"))); | ||
assert!(captured_logs | ||
.iter() | ||
.any(|log| log.contains("VM call trapped"))); | ||
} |
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 |
---|---|---|
|
@@ -4,6 +4,7 @@ mod config; | |
mod help; | ||
#[cfg(feature = "it")] | ||
mod integration; | ||
mod log; | ||
mod plugin; | ||
mod util; | ||
mod version; |
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
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
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,11 +1,54 @@ | ||
pub fn diagnostic_events(events: &[impl std::fmt::Debug], level: tracing::Level) { | ||
for (i, event) in events.iter().enumerate() { | ||
if level == tracing::Level::TRACE { | ||
tracing::trace!("{i}: {event:#?}"); | ||
} else if level == tracing::Level::INFO { | ||
tracing::info!("{i}: {event:#?}"); | ||
} else if level == tracing::Level::ERROR { | ||
tracing::error!("{i}: {event:#?}"); | ||
use serde_json; | ||
use soroban_sdk::xdr::WriteXdr; | ||
use stellar_xdr::curr::{DiagnosticEvent, Limits, ReadXdr}; | ||
|
||
pub fn sim_diagnostic_events(events: &[String], level: tracing::Level) { | ||
tracing::debug!("test"); | ||
let diagnostic_events: Vec<Result<DiagnosticEvent, _>> = events | ||
.iter() | ||
.map(|event_xdr| DiagnosticEvent::from_xdr_base64(event_xdr, Limits::none())) | ||
.collect(); | ||
|
||
log_diagnostic_events(&diagnostic_events, events, level); | ||
} | ||
|
||
pub fn diagnostic_events(events: &[DiagnosticEvent], level: tracing::Level) { | ||
let diagnostic_events: Vec<Result<DiagnosticEvent, std::convert::Infallible>> = | ||
events.iter().map(|event| Ok(event.clone())).collect(); | ||
|
||
let event_xdrs: Vec<String> = events | ||
.iter() | ||
.map(|event| event.to_xdr_base64(Limits::none()).unwrap_or_default()) | ||
.collect(); | ||
|
||
log_diagnostic_events(&diagnostic_events, &event_xdrs, level); | ||
} | ||
|
||
fn log_diagnostic_events<E: std::fmt::Debug>( | ||
diagnostic_events: &[Result<DiagnosticEvent, E>], | ||
event_xdrs: &[String], | ||
level: tracing::Level, | ||
) { | ||
for (i, (event_result, event_xdr)) in diagnostic_events.iter().zip(event_xdrs).enumerate() { | ||
let json_result = event_result | ||
.as_ref() | ||
.ok() | ||
.and_then(|event| serde_json::to_string(event).ok()); | ||
|
||
let log_message = match (event_result, json_result) { | ||
(Ok(_), Some(json)) => format!("{i}: {event_xdr:#?} {json}"), | ||
(Err(e), _) => { | ||
format!("{i}: {event_xdr:#?} Failed to decode DiagnosticEvent XDR: {e:#?}") | ||
} | ||
(Ok(_), None) => format!("{i}: {event_xdr:#?} JSON encoding of DiagnosticEvent failed"), | ||
}; | ||
eprintln!("{log_message}"); | ||
|
||
match level { | ||
tracing::Level::TRACE => tracing::trace!("{}", log_message), | ||
tracing::Level::INFO => tracing::info!("{}", log_message), | ||
tracing::Level::ERROR => tracing::error!("{}", log_message), | ||
_ => tracing::debug!("{}", log_message), // Default to debug for other levels | ||
} | ||
} | ||
} |
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