Skip to main content

rialo_s_program_runtime/
stable_log.rs

1// Copyright (c) Subzero Labs, Inc.
2// SPDX-License-Identifier: Apache-2.0
3// This file is either (a) original to Subzero Labs, Inc. or (b) derived from the Anza codebase and modified by Subzero Labs, Inc.
4
5//! Stable program log messages
6//!
7//! The format of these log messages should not be modified to avoid breaking downstream consumers
8//! of program logging
9use std::{cell::RefCell, rc::Rc};
10
11use base64::{prelude::BASE64_STANDARD, Engine};
12use itertools::Itertools;
13use rialo_s_log_collector::{ic_logger_msg, LogCollector};
14use rialo_s_pubkey::Pubkey;
15
16/// Log a program invoke.
17///
18/// The general form is:
19///
20/// ```notrust
21/// "Program <address> invoke [<depth>]"
22/// ```
23pub fn program_invoke(
24    log_collector: &Option<Rc<RefCell<LogCollector>>>,
25    program_id: &Pubkey,
26    invoke_depth: usize,
27) {
28    ic_logger_msg!(
29        log_collector,
30        "Program {} invoke [{}]",
31        program_id,
32        invoke_depth
33    );
34}
35
36/// Log a message from the program itself.
37///
38/// The general form is:
39///
40/// ```notrust
41/// "Program log: <program-generated output>"
42/// ```
43///
44/// That is, any program-generated output is guaranteed to be prefixed by "Program log: "
45pub fn program_log(log_collector: &Option<Rc<RefCell<LogCollector>>>, message: &str) {
46    ic_logger_msg!(log_collector, "Program log: {}", message);
47}
48
49/// Emit a program data.
50///
51/// The general form is:
52///
53/// ```notrust
54/// "Program data: <binary-data-in-base64>*"
55/// ```
56///
57/// That is, any program-generated output is guaranteed to be prefixed by "Program data: "
58#[allow(clippy::disallowed_methods)]
59pub fn program_data(log_collector: &Option<Rc<RefCell<LogCollector>>>, data: &[&[u8]]) {
60    ic_logger_msg!(
61        log_collector,
62        "Program data: {}",
63        data.iter().map(|v| BASE64_STANDARD.encode(v)).join(" ")
64    );
65}
66
67/// Log return data as from the program itself. This line will not be present if no return
68/// data was set, or if the return data was set to zero length.
69///
70/// The general form is:
71///
72/// ```notrust
73/// "Program return: <program-id> <program-generated-data-in-base64>"
74/// ```
75///
76/// That is, any program-generated output is guaranteed to be prefixed by "Program return: "
77#[allow(clippy::disallowed_methods)]
78pub fn program_return(
79    log_collector: &Option<Rc<RefCell<LogCollector>>>,
80    program_id: &Pubkey,
81    data: &[u8],
82) {
83    ic_logger_msg!(
84        log_collector,
85        "Program return: {} {}",
86        program_id,
87        BASE64_STANDARD.encode(data)
88    );
89}
90
91/// Log successful program execution.
92///
93/// The general form is:
94///
95/// ```notrust
96/// "Program <address> success"
97/// ```
98pub fn program_success(log_collector: &Option<Rc<RefCell<LogCollector>>>, program_id: &Pubkey) {
99    ic_logger_msg!(log_collector, "Program {} success", program_id);
100}
101
102/// Log program execution failure
103///
104/// The general form is:
105///
106/// ```notrust
107/// "Program <address> failed: <program error details>"
108/// ```
109pub fn program_failure<E: std::fmt::Display>(
110    log_collector: &Option<Rc<RefCell<LogCollector>>>,
111    program_id: &Pubkey,
112    err: &E,
113) {
114    ic_logger_msg!(log_collector, "Program {} failed: {}", program_id, err);
115}