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

Support colored logs #93

Merged
merged 2 commits into from
Jul 22, 2024
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
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ libc = []
ckb2023 = []
# work with `target-feature=-a` Cargo flag
dummy-atomic = []
log = ["dep:log", "dummy-atomic"]

[build-dependencies]
cc = "1.0"
Expand All @@ -35,6 +36,7 @@ ckb-types = { package = "ckb-gen-types", version = "0.116", default-features = f
buddy-alloc = { version = "0.5.0", optional = true }
ckb-x64-simulator = { version = "0.8", optional = true }
gcd = "2.3.0"
log = { version = "0.4.21", optional = true, default-features = false }

[workspace]
exclude = ["test"]
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ This library contains several modules that help you write CKB contract with Rust
* `entry!` macro: defines contract entry point
* `default_alloc!` macro: defines global allocator for no-std rust
* `dummy_atomic` module: dummy atomic operations
* `logger` module: colored logger implementation
### Memory allocator

Default allocator uses a mixed allocation strategy:
Expand Down
1 change: 1 addition & 0 deletions contracts/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion contracts/ckb-std-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
ckb-std = { path = "../../", features = [ "dlopen-c", "dummy-atomic" ] }
ckb-std = { path = "../../", features = [ "dlopen-c", "dummy-atomic", "log"] }
blake2b-ref = { version = "0.3", default-features = false }
bytes = { version = "1.6.0", default-features = false }
log = { version = "0.4.17", default-features = false }
17 changes: 13 additions & 4 deletions contracts/ckb-std-tests/src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ use ckb_std::{dynamic_loading, dynamic_loading_c_impl};
use core::ffi::c_void;
#[cfg(target_arch = "riscv64")]
use core::mem::size_of_val;
#[cfg(target_arch = "riscv64")]
use log::{info, warn};

fn new_blake2b() -> Blake2b {
const CKB_HASH_PERSONALIZATION: &[u8] = b"ckb-default-hash";
Expand Down Expand Up @@ -423,8 +421,8 @@ fn test_atomic() {
assert_eq!(v.len(), 4);

// The log crate uses atomic operations.
info!("atomic info");
warn!("atomic warn");
ckb_std::log::info!("atomic info");
ckb_std::log::warn!("atomic warn");
}

#[cfg(target_arch = "riscv64")]
Expand Down Expand Up @@ -786,6 +784,16 @@ fn test_atomic2() {
assert_eq!(data, 0xFFFFFFFFFFFFFFFE);
}

#[cfg(target_arch = "riscv64")]
fn test_log() {
drop(ckb_std::logger::init());
ckb_std::log::trace!("this is trace");
ckb_std::log::debug!("this is debug");
ckb_std::log::info!("this is info");
ckb_std::log::warn!("this is warn");
ckb_std::log::error!("this is error");
}

pub fn main() -> Result<(), Error> {
test_basic();
test_load_data();
Expand All @@ -812,6 +820,7 @@ pub fn main() -> Result<(), Error> {
{
test_atomic();
test_atomic2();
test_log();
}
Ok(())
}
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,7 @@ pub mod dynamic_loading_c_impl;
pub use buddy_alloc;
#[cfg(feature = "dummy-atomic")]
pub mod dummy_atomic;
#[cfg(feature = "log")]
pub mod logger;
#[cfg(feature = "log")]
pub use log;
66 changes: 66 additions & 0 deletions src/logger.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
extern crate alloc;

use crate::syscalls;
use alloc::format;
use log::{Level, Metadata, Record};
use log::{LevelFilter, SetLoggerError};

struct SimpleLogger;

impl log::Log for SimpleLogger {
fn enabled(&self, _metadata: &Metadata) -> bool {
true
}

fn log(&self, record: &Record) {
let metadata = record.metadata();
if self.enabled(metadata) {
let level = metadata.level();
match level {
Level::Error => syscalls::debug(format!(
"[\x1b[31m{}\x1b[0m {}:{}] {}",
record.level(),
record.file().unwrap_or("???"),
record.line().unwrap_or(0),
record.args()
)),
Level::Warn => syscalls::debug(format!(
"[\x1b[33m{}\x1b[0m {}:{}] {}",
record.level(),
record.file().unwrap_or("???"),
record.line().unwrap_or(0),
record.args()
)),
Level::Info => syscalls::debug(format!(
"[\x1b[32m{}\x1b[0m {}:{}] {}",
record.level(),
record.file().unwrap_or("???"),
record.line().unwrap_or(0),
record.args()
)),
Level::Debug => syscalls::debug(format!(
"[\x1b[34m{}\x1b[0m {}:{}] {}",
record.level(),
record.file().unwrap_or("???"),
record.line().unwrap_or(0),
record.args()
)),
Level::Trace => syscalls::debug(format!(
"[\x1b[36m{}\x1b[0m {}:{}] {}",
record.level(),
record.file().unwrap_or("???"),
record.line().unwrap_or(0),
record.args()
)),
}
}
}

fn flush(&self) {}
}

static LOGGER: SimpleLogger = SimpleLogger;

pub fn init() -> Result<(), SetLoggerError> {
log::set_logger(&LOGGER).map(|()| log::set_max_level(LevelFilter::Trace))
}
Loading