Why run `dfx deploy backend` in #[pre_upgrade] function don`t show println!() info in command line?

Here is part of my backend code of actor.rs
Is it because the pre_upgrade() didnt even auto called when executing dfx deploy backend ?:nerd_face:

//...
/**
 * 1. each time upgrade(cmd : dfx deploy ),
 * will *erase* all ic-DB (canister stable memory)
 * so we can:
 *      1.manully erase all,
 *      2.or , restore from a in memory data.(such as a hashmap)
 *
 *  
 * 2. transational upgrade:
 * if pre_upgrade, upgrade ,post_upgrade
 * any step go wrong.
 * will revert to last version.
 *
 *
 */
#[pre_upgrade]
fn pre_upgrade() {
    let canister_id = id();
    print(format!("starting pre_upgrade {:?}", canister_id));

    CONTEXT.with(|c| {
        let context = c.borrow();
        let id = context.id;
        // get users list from vilotile storage (computer memory)
        let users = Vec::from_iter(context.user_service.users.values().cloned());
        let db: CanisterDB = CanisterDB { id, users };
        // println!("{:#?}", db);
        // IMPORTANT save all userdata into IC-DB
        storage::stable_save((db,)).expect("failed to save state data");

        // IMPORTANT erase db in running canister.(ic or local)
        // in bash, maybe this need to do : 
        // dfx deploy backend --network ic  -m reinstall 
        // let _empty_db = CanisterDB::default();
        // storage::stable_save((_empty_db,)).expect("failed to save state data");
        print(format!("started pre_upgrade {:?}", canister_id));
    });
}
//...

Use ic_cdk::println! macro instead: println in ic_cdk - Rust

1 Like

I tried this :

#[query]
fn test_print() -> u32{
    use ic_cdk::println;
    ic_cdk::println!("test_print");
    return 0;
}
btwl@btwl-virtual-machine ~/D/i/tax_lint (master) [255]> 
  dfx canister call backend test_print

Please enter the passphrase for your identity: [hidden]
Decryption complete.
(0 : nat32)

It didnt print the test_print to terminal why ?

The message should be printed on the terminal where the replica dfx start is running.