1 // Licensed to the Apache Software Foundation (ASF) under one
2 // or more contributor license agreements.  See the NOTICE file
3 // distributed with this work for additional information
4 // regarding copyright ownership.  The ASF licenses this file
5 // to you under the Apache License, Version 2.0 (the
6 // "License"); you may not use this file except in compliance
7 // with the License.  You may obtain a copy of the License at
8 //
9 //   http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing,
12 // software distributed under the License is distributed on an
13 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, either express or implied.  See the License for the
15 // specific language governing permissions and limitations
16 // under the License.
17 
18 use libc;
19 use optee_utee_sys as raw;
20 use std::fmt;
21 use std::io;
22 use std::io::Write;
23 
24 pub struct Trace;
25 
26 impl Trace {
new() -> Self27     fn new() -> Self {
28         Trace {}
29     }
30 
_print(fmt: fmt::Arguments)31     pub fn _print(fmt: fmt::Arguments) {
32         let mut writer = Trace::new();
33         let result = writer.write_fmt(fmt);
34 
35         if let Err(e) = result {
36             panic!("failed printing to trace: {}", e);
37         }
38     }
39 
set_level(level: i32)40     pub fn set_level(level: i32) {
41         unsafe {
42             raw::trace_set_level(level);
43         }
44     }
45 
get_level() -> i3246     pub fn get_level() -> i32 {
47         unsafe { raw::trace_get_level() }
48     }
49 }
50 
51 impl io::Write for Trace {
write(&mut self, buf: &[u8]) -> io::Result<usize>52     fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
53         unsafe {
54             raw::_utee_log(buf.as_ptr() as *const libc::c_void, buf.len());
55         }
56         Ok(buf.len())
57     }
58 
flush(&mut self) -> io::Result<()>59     fn flush(&mut self) -> io::Result<()> {
60         Ok(())
61     }
62 }
63