-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Store log entries on the heap and expose via query call
- Loading branch information
Showing
11 changed files
with
68 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,26 @@ | ||
use crate::lifecycle::READER_WRITER_BUFFER_SIZE; | ||
use crate::logs::log; | ||
use crate::memory::get_upgrades_memory; | ||
use crate::state::State; | ||
use crate::InitOrUpgradeArgs; | ||
use ic_cdk::post_upgrade; | ||
use ic_stable_structures::reader::{BufferedReader, Reader}; | ||
use serde::Deserialize; | ||
|
||
type Serialized = (State, Vec<String>); | ||
|
||
#[post_upgrade] | ||
fn post_upgrade(args: InitOrUpgradeArgs) { | ||
let _args = args.into_upgrade_args(); | ||
let memory = get_upgrades_memory(); | ||
let reader = BufferedReader::new(READER_WRITER_BUFFER_SIZE, Reader::new(&memory, 0)); | ||
let mut deserializer = rmp_serde::Deserializer::new(reader); | ||
|
||
let state = State::deserialize(&mut deserializer).unwrap(); | ||
let (state, logs) = Serialized::deserialize(&mut deserializer).unwrap(); | ||
|
||
crate::jobs::start_jobs(&state); | ||
crate::state::init(state); | ||
crate::logs::init(logs); | ||
|
||
ic_cdk::println!("Canister upgrade complete"); | ||
log("Canister upgrade complete"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
use std::cell::RefCell; | ||
use std::collections::VecDeque; | ||
|
||
thread_local! { | ||
static LOGS: RefCell<VecDeque<String>> = RefCell::default(); | ||
} | ||
|
||
pub fn init(logs: Vec<String>) { | ||
LOGS.set(VecDeque::from(logs)); | ||
} | ||
|
||
pub fn log<S: AsRef<str>>(s: S) { | ||
let message = s.as_ref(); | ||
|
||
ic_cdk::println!("{message}"); | ||
|
||
LOGS.with_borrow_mut(|logs| { | ||
let now = ic_cdk::api::time() / 1_000_000; | ||
logs.push_back(format!("{now}: {message}")); | ||
|
||
while logs.len() > 1000 { | ||
logs.pop_front(); | ||
} | ||
}) | ||
} | ||
|
||
pub fn logs() -> Vec<String> { | ||
LOGS.with_borrow(|logs| logs.iter().cloned().collect()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
use ic_cdk::query; | ||
|
||
#[query] | ||
fn logs() -> Vec<String> { | ||
crate::logs::logs() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
mod list_neuron_pairs; | ||
mod logs; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters