junobuild_satellite/logs/
loggers.rs

1use crate::logs::types::logs::{Log, LogLevel};
2use crate::random::runtime::random;
3use crate::{set_doc_store, SetDoc};
4use ic_cdk::api::time;
5use junobuild_collections::constants::db::COLLECTION_LOG_KEY;
6use junobuild_shared::ic::api::id;
7use junobuild_shared::types::core::Key;
8use junobuild_utils::encode_doc_data;
9use serde::Serialize;
10
11/// Logs a message at the `Info` level.
12///
13/// # Arguments
14///
15/// * `message` - A string slice that holds the message to be logged.
16///
17/// # Returns
18///
19/// A result indicating success (`Ok(())`) or containing an error message (`Err(String)`).
20pub fn log(message: String) -> Result<(), String> {
21    set_log::<()>(LogLevel::Info, message, None)
22}
23
24/// Logs a message at the `Info` level with additional serialized data.
25///
26/// # Arguments
27///
28/// * `message` - The message to be logged.
29/// * `data` - A reference to the data to be logged. The data must implement the `Serialize` trait.
30///
31/// # Returns
32///
33/// A result indicating success or containing an error message.
34pub fn log_with_data<T: Serialize>(message: String, data: &T) -> Result<(), String> {
35    set_log::<T>(LogLevel::Info, message, Some(data))
36}
37
38/// Logs an informational message.
39///
40/// This function is a convenience wrapper for `log`, setting the log level to `Info`.
41pub fn info(message: String) -> Result<(), String> {
42    set_log::<()>(LogLevel::Info, message, None)
43}
44
45/// Logs an informational message with additional serialized data.
46pub fn info_with_data<T: Serialize>(message: String, data: &T) -> Result<(), String> {
47    set_log::<T>(LogLevel::Info, message, Some(data))
48}
49
50/// Logs a debug-level message.
51pub fn debug(message: String) -> Result<(), String> {
52    set_log::<()>(LogLevel::Debug, message, None)
53}
54
55/// Logs a debug-level message with additional serialized data.
56pub fn debug_with_data<T: Serialize>(message: String, data: &T) -> Result<(), String> {
57    set_log::<T>(LogLevel::Debug, message, Some(data))
58}
59
60/// Logs a warning message.
61pub fn warn(message: String) -> Result<(), String> {
62    set_log::<()>(LogLevel::Warning, message, None)
63}
64
65/// Logs a warning message with additional serialized data.
66pub fn warn_with_data<T: Serialize>(message: String, data: &T) -> Result<(), String> {
67    set_log::<T>(LogLevel::Warning, message, Some(data))
68}
69
70/// Logs an error message.
71pub fn error(message: String) -> Result<(), String> {
72    set_log::<()>(LogLevel::Error, message, None)
73}
74
75/// Logs an error message with additional serialized data.
76pub fn error_with_data<T: Serialize>(message: String, data: &T) -> Result<(), String> {
77    set_log::<T>(LogLevel::Error, message, Some(data))
78}
79
80fn set_log<T: Serialize>(level: LogLevel, message: String, data: Option<&T>) -> Result<(), String> {
81    let nonce = random()?;
82
83    let key: Key = format!("{}-{}", time(), nonce);
84
85    let log_data = data.map(encode_doc_data).transpose()?;
86
87    let log: Log = Log {
88        level,
89        message,
90        data: log_data,
91    };
92
93    let doc: SetDoc = SetDoc {
94        description: None,
95        data: encode_doc_data(&log)?,
96        version: None,
97    };
98
99    set_doc_store(id(), COLLECTION_LOG_KEY.to_string(), key, doc)?;
100
101    Ok(())
102}