1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
use crate::ArwenApiImpl;
use elrond_wasm::api::LogApi;

extern "C" {
	fn writeLog(pointer: *const u8, length: i32, topicPtr: *const u8, numTopics: i32);
}

const TOPIC_LENGTH: usize = 32;

/// Interface to only be used by code generated by the macros.
/// The smart contract code doesn't have access to these methods directly.
impl LogApi for ArwenApiImpl {
	fn write_log(&self, topics: &[[u8; 32]], data: &[u8]) {
		let mut topics_raw = [0u8; TOPIC_LENGTH * 10]; // hopefully we never have more than 10 topics
		for i in 0..topics.len() {
			topics_raw[TOPIC_LENGTH * i..TOPIC_LENGTH * (i + 1)].copy_from_slice(&topics[i]);
		}
		unsafe {
			writeLog(
				data.as_ptr(),
				data.len() as i32,
				topics_raw.as_ptr(),
				topics.len() as i32,
			);
		}
	}
}