Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
google
GitHub Repository: google/crosvm
Path: blob/main/base/src/sys/linux/android/syslog.rs
5394 views
1
// Copyright 2020 The ChromiumOS Authors
2
// Use of this source code is governed by a BSD-style license that can be
3
// found in the LICENSE file.
4
5
//! Implementation of the Syslog trait as a wrapper around Android's logging library, liblog.
6
7
use std::ffi::CString;
8
use std::ffi::NulError;
9
use std::mem::size_of;
10
11
use android_log_sys::__android_log_is_loggable;
12
use android_log_sys::__android_log_message;
13
use android_log_sys::__android_log_write_log_message;
14
use android_log_sys::log_id_t;
15
use android_log_sys::LogPriority;
16
17
use crate::syslog::Error;
18
use crate::syslog::Facility;
19
use crate::syslog::Level;
20
use crate::syslog::Log;
21
use crate::syslog::Syslog;
22
use crate::RawDescriptor;
23
24
pub struct PlatformSyslog {
25
proc_name: CString,
26
}
27
28
impl Syslog for PlatformSyslog {
29
fn new(
30
proc_name: String,
31
_facility: Facility,
32
) -> Result<(Option<Box<dyn Log + Send>>, Option<RawDescriptor>), &'static Error> {
33
Ok((
34
Some(Box::new(Self {
35
proc_name: CString::new(proc_name).unwrap(),
36
})),
37
None,
38
))
39
}
40
}
41
42
impl Log for PlatformSyslog {
43
fn log(&self, record: &log::Record) {
44
let priority = match record.level() {
45
Level::Error => LogPriority::ERROR,
46
Level::Warn => LogPriority::WARN,
47
Level::Info => LogPriority::INFO,
48
Level::Debug => LogPriority::DEBUG,
49
Level::Trace => LogPriority::VERBOSE,
50
};
51
let message = std::fmt::format(*record.args());
52
let _ = android_log(
53
log_id_t::SYSTEM,
54
priority,
55
&self.proc_name,
56
record.file(),
57
record.line(),
58
&message,
59
);
60
}
61
62
fn enabled(&self, _metadata: &log::Metadata) -> bool {
63
true
64
}
65
66
fn flush(&self) {}
67
}
68
69
/// Send a log message to the Android logger (logd, by default) if it is currently configured to be
70
/// loggable based on the priority and tag.
71
///
72
/// # Arguments
73
/// * `priority` - The Android log priority. Used to determine whether the message is loggable.
74
/// * `tag` - A tag to indicate where the log comes from.
75
/// * `file` - The name of the file from where the message is being logged, if available.
76
/// * `line` - The line number from where the message is being logged, if available.
77
/// * `message` - The message to log.
78
fn android_log(
79
buffer_id: log_id_t,
80
priority: LogPriority,
81
tag: &CString,
82
file: Option<&str>,
83
line: Option<u32>,
84
message: &str,
85
) -> Result<(), NulError> {
86
let default_pri = LogPriority::VERBOSE;
87
// SAFETY: `tag` is guaranteed to be valid for duration of the call
88
if unsafe { __android_log_is_loggable(priority as i32, tag.as_ptr(), default_pri as i32) } != 0
89
{
90
let file = file.map(CString::new).transpose()?;
91
let line = line.unwrap_or(0);
92
let message = CString::new(message)?;
93
let mut log_message = __android_log_message {
94
struct_size: size_of::<__android_log_message>(),
95
buffer_id: buffer_id as i32,
96
priority: priority as i32,
97
tag: tag.as_ptr(),
98
file: file.map_or(std::ptr::null(), |v| v.as_ptr()),
99
line,
100
message: message.as_ptr(),
101
};
102
// SAFETY: `log_message` is guaranteed to be valid for duration of the call
103
unsafe { __android_log_write_log_message(&mut log_message) };
104
}
105
Ok(())
106
}
107
108