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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
extern crate android_log_sys as log_ffi;
#[macro_use] extern crate log;
use log_ffi::LogPriority;
use log::{Log,LogLevel,LogMetadata,LogRecord};
use std::ffi::CStr;
use std::mem;
use std::fmt;
fn android_log(prio: log_ffi::LogPriority, tag: &CStr, msg: &CStr) {
unsafe { log_ffi::__android_log_write(prio as log_ffi::c_int, tag.as_ptr(), msg.as_ptr()) };
}
struct PlatformLogger;
const LOGGING_TAG_MAX_LEN: usize = 23;
const LOGGING_MSG_MAX_LEN: usize = 4000;
impl Log for PlatformLogger {
fn enabled(&self, _: &LogMetadata) -> bool {
true
}
fn log(&self, record: &LogRecord) {
let mut tag_bytes: [u8; LOGGING_TAG_MAX_LEN + 1] = unsafe { mem::uninitialized() };
self.fill_tag_bytes(&mut tag_bytes, record);
let tag: &CStr = unsafe { CStr::from_ptr(mem::transmute(tag_bytes.as_ptr())) };
let mut writer = PlatformLogWriter::new(
record.level(),
tag
);
let _ = fmt::write(&mut writer, *record.args());
writer.flush();
}
}
impl PlatformLogger {
fn fill_tag_bytes(&self, array: &mut [u8], record: &LogRecord) {
let tag_bytes_iter = record.location().module_path().bytes();
if tag_bytes_iter.len() > LOGGING_TAG_MAX_LEN {
for (input, output) in tag_bytes_iter
.take(LOGGING_TAG_MAX_LEN - 2)
.chain(b"..\0".iter().cloned())
.zip(array.iter_mut())
{
*output = input;
}
} else {
for (input, output) in tag_bytes_iter
.chain(b"\0".iter().cloned())
.zip(array.iter_mut())
{
*output = input;
}
}
}
}
struct PlatformLogWriter<'a> {
priority: LogPriority,
len: usize,
last_newline_index: usize,
tag: &'a CStr,
buffer: [u8; LOGGING_MSG_MAX_LEN + 1],
}
impl<'a> PlatformLogWriter<'a> {
pub fn new<'r>(level: LogLevel, tag: &'r CStr) -> PlatformLogWriter<'r> {
PlatformLogWriter {
priority: match level {
LogLevel::Warn => LogPriority::WARN,
LogLevel::Info => LogPriority::INFO,
LogLevel::Debug => LogPriority::DEBUG,
LogLevel::Error => LogPriority::ERROR,
LogLevel::Trace => LogPriority::VERBOSE,
},
len: 0,
last_newline_index: 0,
tag: tag,
buffer: unsafe { mem::uninitialized() },
}
}
fn temporal_flush(&mut self) {
let total_len = self.len;
if total_len == 0 {
return;
}
if self.last_newline_index > 0 {
let copy_from_index = self.last_newline_index;
let remaining_chunk_len = total_len - copy_from_index;
self.output_specified_len(copy_from_index);
self.copy_bytes_to_start(copy_from_index, remaining_chunk_len);
self.len = remaining_chunk_len;
} else {
self.output_specified_len(total_len);
self.len = 0;
}
self.last_newline_index = 0;
}
fn flush(&mut self) {
let total_len = self.len;
if total_len == 0 {
return;
}
self.output_specified_len(total_len);
self.len = 0;
self.last_newline_index = 0;
}
fn output_specified_len(&mut self, len: usize) {
let last_byte: u8 = self.buffer[len];
self.buffer[len] = b'\0';
let msg: &CStr = unsafe { CStr::from_ptr(mem::transmute(self.buffer.as_ptr())) };
android_log(self.priority, self.tag, msg);
self.buffer[len] = last_byte;
}
fn copy_bytes_to_start(&mut self, index: usize, len: usize) {
for i in 0..len {
*unsafe { self.buffer.get_unchecked_mut(i) } =
*unsafe { self.buffer.get_unchecked_mut(i + index) };
}
}
}
impl<'a> fmt::Write for PlatformLogWriter<'a> {
fn write_str(&mut self, s: &str) -> fmt::Result {
let mut incomming_bytes = s.as_bytes();
while incomming_bytes.len() > 0 {
let len = self.len;
let new_len = len + incomming_bytes.len();
let last_newline = self.buffer[len..LOGGING_MSG_MAX_LEN].iter_mut()
.zip(incomming_bytes)
.enumerate()
.fold(None, |acc, (i, (output, input))| {
*output = *input;
if *input == b'\n' { Some(i) } else { acc }
});
if let Some(newline) = last_newline {
self.last_newline_index = len + newline;
}
let written_len = if new_len <= LOGGING_MSG_MAX_LEN {
self.len = new_len;
new_len - len
} else {
self.len = LOGGING_MSG_MAX_LEN;
self.temporal_flush();
LOGGING_MSG_MAX_LEN - len
};
incomming_bytes = &incomming_bytes[written_len..];
}
Ok(())
}
}
pub fn init_once(log_level: LogLevel) {
match log::set_logger(|max_log_level| {
max_log_level.set(log_level.to_log_level_filter());
return Box::new(PlatformLogger);
}) {
Err(e) => debug!("{}", e),
_ => (),
}
}