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
28
29
30
31
32
// Copyright 2021 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.

use diagnostics_message::{self as message, MonikerWithUrl};
use std::ffi::CString;
use std::os::raw::c_char;

#[no_mangle]
#[allow(clippy::not_unsafe_ptr_arg_deref)]
pub extern "C" fn fuchsia_decode_log_message_to_json(msg: *const u8, size: usize) -> *mut c_char {
    let managed_ptr;
    unsafe {
        managed_ptr = std::slice::from_raw_parts(msg, size);
    }
    let data = &message::from_structured(
        MonikerWithUrl { moniker: "<test_moniker>".to_string(), url: "".to_string() },
        managed_ptr,
    )
    .unwrap();
    let item = serde_json::to_string(&data).unwrap();
    CString::new(format!("[{}]", item)).unwrap().into_raw()
}

#[no_mangle]
#[allow(clippy::not_unsafe_ptr_arg_deref)]
pub extern "C" fn fuchsia_free_decoded_log_message(msg: *mut c_char) {
    let str_to_free;
    unsafe {
        str_to_free = CString::from_raw(msg);
    }
    let _freer = str_to_free;
}