From 33b9630d43871bea43f39d3bbc455db1a5bdab7a Mon Sep 17 00:00:00 2001 From: Martin Pitt Date: Sat, 25 Sep 2021 12:42:02 +0200 Subject: [PATCH] serde: Use log framework Run with `RUST_LOG=debug` to see the serialized message. --- serde/Cargo.toml | 2 ++ serde/src/main.rs | 6 ++++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/serde/Cargo.toml b/serde/Cargo.toml index 64d55f5..b5dbc41 100644 --- a/serde/Cargo.toml +++ b/serde/Cargo.toml @@ -8,3 +8,5 @@ edition = "2018" [dependencies] serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" +log = "0.4" +env_logger = "0.9" diff --git a/serde/src/main.rs b/serde/src/main.rs index 8579522..fc88d23 100644 --- a/serde/src/main.rs +++ b/serde/src/main.rs @@ -1,5 +1,6 @@ use std::fs; use std::error::Error; + use serde::{Serialize, Deserialize}; const DB_PATH: &str = "/tmp/contacts.json"; @@ -31,7 +32,7 @@ fn build_contacts() -> Contacts { fn create_contacts() -> Result<(), Box> { let contacts = build_contacts(); let serialized = serde_json::to_string(&contacts)?; - println!("serialized: {}", serialized); + log::debug!("serialized: {}", &serialized); let mut f = fs::File::create(DB_PATH)?; serde_json::to_writer_pretty(&mut f, &contacts)?; Ok(()) @@ -43,8 +44,9 @@ fn load_contacts() -> Result> { } fn main() -> Result<(), Box> { + env_logger::init(); create_contacts()?; let contacts = load_contacts()?; - println!("deserialized: {:?}", contacts); + println!("deserialized: {:?}", &contacts); Ok(()) } -- 2.39.5