Skip to content

Commit

Permalink
Support colored logs
Browse files Browse the repository at this point in the history
Add a default logger implementation for log crate.
Add a new feature "log".
  • Loading branch information
XuJiandong committed Jun 13, 2024
1 parent da5a078 commit 4619af6
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 5 deletions.
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
* `log` module: colored log
### 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");
log::info!("atomic info");
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::log::init());
log::trace!("this is trace");
log::debug!("this is debug");
log::info!("this is info");
log::warn!("this is warn");
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(())
}
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,5 @@ pub mod dynamic_loading_c_impl;
pub use buddy_alloc;
#[cfg(feature = "dummy-atomic")]
pub mod dummy_atomic;
#[cfg(feature = "log")]
pub mod log;
66 changes: 66 additions & 0 deletions src/log.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))
}

0 comments on commit 4619af6

Please sign in to comment.