Skip to content
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

Rename logger to logs #1005

Merged
merged 3 commits into from
Jun 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions soroban-sdk/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ use crate::auth::InvokerContractAuthEntry;
use crate::unwrap::UnwrapInfallible;
use crate::unwrap::UnwrapOptimized;
use crate::{
crypto::Crypto, deploy::Deployer, events::Events, ledger::Ledger, logging::Logger,
storage::Storage, Address, Vec,
crypto::Crypto, deploy::Deployer, events::Events, ledger::Ledger, logs::Logs, storage::Storage,
Address, Vec,
};
use internal::{
AddressObject, Bool, BytesObject, DurationObject, I128Object, I256Object, I64Object,
Expand Down Expand Up @@ -442,10 +442,18 @@ impl Env {
.unwrap_infallible();
}

/// Get the [Logger] for logging debug events.
/// Get the [Logs] for logging debug events.
#[inline(always)]
pub fn logger(&self) -> Logger {
Logger::new(self)
#[deprecated(note = "use [Env::logs]")]
#[doc(hidden)]
pub fn logger(&self) -> Logs {
self.logs()
}

/// Get the [Logs] for logging debug events.
#[inline(always)]
pub fn logs(&self) -> Logs {
Logs::new(self)
}
}

Expand Down
2 changes: 1 addition & 1 deletion soroban-sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,7 @@ pub mod deploy;
pub mod events;
pub mod iter;
pub mod ledger;
pub mod logging;
pub mod logs;
mod map;
pub mod storage;
pub mod token;
Expand Down
32 changes: 19 additions & 13 deletions soroban-sdk/src/logging.rs → soroban-sdk/src/logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,21 +62,21 @@ use crate::{env::internal::EnvBase, Env, Val};
/// let value = 5;
/// log!(&env, "a log entry", value, Symbol::short("another"));
///
/// use soroban_sdk::testutils::Logger;
/// let logentry = env.logger().all().last().unwrap().clone();
/// use soroban_sdk::testutils::Logs;
/// let logentry = env.logs().all().last().unwrap().clone();
/// assert!(logentry.contains("[\"a log entry\", 5, another]"));
/// # }
/// ```
#[macro_export]
macro_rules! log {
($env:expr, $fmt:literal $(,)?) => {
if cfg!(debug_assertions) {
$env.logger().log($fmt, &[]);
$env.logs().add($fmt, &[]);
}
};
($env:expr, $fmt:literal, $($args:expr),* $(,)?) => {
if cfg!(debug_assertions) {
$env.logger().log($fmt, &[
$env.logs().add($fmt, &[
$(
<_ as $crate::IntoVal<Env, $crate::Val>>::into_val(&$args, $env)
),*
Expand All @@ -85,27 +85,33 @@ macro_rules! log {
};
}

/// Logger logs debug events.
/// Logs logs debug events.
///
/// See [`log`][crate::log] for how to conveniently log debug events.
#[derive(Clone)]
pub struct Logger(Env);
pub struct Logs(Env);

impl Debug for Logger {
impl Debug for Logs {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "Logger")
write!(f, "Logs")
}
}

impl Logger {
impl Logs {
#[inline(always)]
pub(crate) fn env(&self) -> &Env {
&self.0
}

#[inline(always)]
pub(crate) fn new(env: &Env) -> Logger {
Logger(env.clone())
pub(crate) fn new(env: &Env) -> Logs {
Logs(env.clone())
}

#[deprecated(note = "use [Logs::add]")]
#[inline(always)]
pub fn log(&self, msg: &'static str, args: &[Val]) {
self.add(msg, args);
}

/// Log a debug event.
Expand All @@ -115,7 +121,7 @@ impl Logger {
///
/// See [`log`][crate::log] for how to conveniently log debug events.
#[inline(always)]
pub fn log(&self, msg: &'static str, args: &[Val]) {
pub fn add(&self, msg: &'static str, args: &[Val]) {
if cfg!(debug_assertions) {
let env = self.env();
env.log_from_slice(msg, args).unwrap();
Expand All @@ -128,7 +134,7 @@ use crate::testutils;

#[cfg(any(test, feature = "testutils"))]
#[cfg_attr(feature = "docs", doc(cfg(feature = "testutils")))]
impl testutils::Logger for Logger {
impl testutils::Logs for Logs {
fn all(&self) -> std::vec::Vec<String> {
use crate::xdr::{
ContractEventBody, ContractEventType, ScSymbol, ScVal, ScVec, StringM, VecM,
Expand Down
4 changes: 2 additions & 2 deletions soroban-sdk/src/testutils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ pub trait Events {
fn all(&self) -> Vec<(crate::Address, Vec<Val>, Val)>;
}

/// Test utilities for [`Logger`][crate::logging::Logger].
pub trait Logger {
/// Test utilities for [`Logs`][crate::logs::Logs].
pub trait Logs {
/// Returns all diagnostic events that have been logged.
fn all(&self) -> std::vec::Vec<String>;
/// Prints all diagnostic events to stdout.
Expand Down
8 changes: 4 additions & 4 deletions tests/logging/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl Contract {
mod test {
extern crate std;

use soroban_sdk::{testutils::Logger, Env};
use soroban_sdk::{testutils::Logs, Env};

use crate::{Contract, ContractClient};

Expand All @@ -41,7 +41,7 @@ mod test {

client.hello();

env.logger().print();
env.logs().print();

if cfg!(debug_assertions) {
let pats = std::vec![
Expand All @@ -52,11 +52,11 @@ mod test {
"[\"one and two:\", one, two]",
"[\"one and two:\", one, two]"
];
for (msg, pat) in env.logger().all().iter().zip(pats.iter()) {
for (msg, pat) in env.logs().all().iter().zip(pats.iter()) {
assert!(msg.contains(pat));
}
} else {
assert_eq!(env.logger().all(), std::vec![""; 0]);
assert_eq!(env.logs().all(), std::vec![""; 0]);
}
}
}