android_logger_lite/
lib.rs

1use std::ffi::CStr;
2use std::os::raw;
3
4#[allow(non_camel_case_types)]
5pub type c_va_list = raw::c_void;
6#[allow(non_camel_case_types)]
7pub type c_int = raw::c_int;
8#[allow(non_camel_case_types)]
9pub type c_char = raw::c_char;
10
11///LogPriority
12#[derive(Clone, Copy)]
13#[repr(isize)]
14pub enum LogPriority {
15    UNKNOWN = 0,
16    DEFAULT = 1,
17    VERBOSE = 2,
18    DEBUG = 3,
19    INFO = 4,
20    WARN = 5,
21    ERROR = 6,
22    FATAL = 7,
23    SILENT = 8,
24}
25
26#[allow(non_camel_case_types)]
27#[repr(C)]
28#[derive(Debug, Copy, Clone)]
29pub struct __android_log_message {
30    pub struct_size: usize,
31    pub buffer_id: i32,
32    pub priority: i32,
33    pub tag: *const c_char,
34    pub file: *const c_char,
35    pub line: u32,
36    pub message: *const c_char,
37}
38
39///Log.v
40pub fn v(tag: String, msg: String) {
41    rust_android_log(LogPriority::VERBOSE, tag, msg);
42}
43
44///Log.d
45pub fn d(tag: String, msg: String) {
46    rust_android_log(LogPriority::DEBUG, tag, msg);
47}
48
49///Log.i
50pub fn i(tag: String, msg: String) {
51    rust_android_log(LogPriority::INFO, tag, msg);
52}
53
54///Log.w
55pub fn w(tag: String, msg: String) {
56    rust_android_log(LogPriority::WARN, tag, msg);
57}
58
59///Log.e
60pub fn e(tag: String, msg: String) {
61    rust_android_log(LogPriority::ERROR, tag, msg);
62}
63
64pub fn rust_android_log(prio: LogPriority, tag: String, msg: String) {
65    let android_tag = tag + "\0";
66    let android_msg = msg + "\0";
67    let tag: &CStr = unsafe { CStr::from_ptr(android_tag.as_ptr().cast()) };
68    let msg: &CStr = unsafe { CStr::from_ptr(android_msg.as_ptr().cast()) };
69    android_log(prio, tag, msg);
70}
71
72fn android_log(prio: LogPriority, tag: &CStr, msg: &CStr) {
73    unsafe {
74        __android_log_write(
75            prio as c_int,
76            tag.as_ptr() as *const c_char,
77            msg.as_ptr() as *const c_char,
78        )
79    };
80}
81
82
83#[link(name = "log")]
84extern "C" {
85    pub fn __android_log_write(prio: c_int,
86                               tag: *const c_char,
87                               text: *const c_char)
88                               -> c_int;
89    pub fn __android_log_buf_write(buf_id: c_int,
90                                   prio: c_int,
91                                   tag: *const c_char,
92                                   text: *const c_char)
93                                   -> c_int;
94    pub fn __android_log_print(prio: c_int,
95                               tag: *const c_char,
96                               fmt: *const c_char,
97                               ...)
98                               -> c_int;
99    pub fn __android_log_vprint(prio: c_int,
100                                tag: *const c_char,
101                                fmt: *const c_char,
102                                ap: *mut c_va_list)
103                                -> c_int;
104    pub fn __android_log_assert(cond: *const c_char,
105                                tag: *const c_char,
106                                fmt: *const c_char,
107                                ...);
108    pub fn __android_log_is_loggable(prio: c_int,
109                                     tag: *const c_char,
110                                     default_prio: c_int)
111                                     -> c_int;
112    pub fn __android_log_write_log_message(log_message: *mut __android_log_message);
113}
114