1 //! Expose some internal methods to set the log level for system syslog.
2 //!
3 //! On systems that use syslog (i.e. `vlog_syslog.cc`), there is support
4 //! to filter out logs before they go to syslog. This module provides Rust apis
5 //! to tune log levels for syslog.
6
7 use num_derive::ToPrimitive;
8 use num_traits::cast::ToPrimitive;
9 use std::ffi::CString;
10 use std::os::raw::c_char;
11
12 #[derive(ToPrimitive)]
13 #[repr(u8)]
14 /// Android framework log priority levels.
15 /// They are defined in system/logging/liblog/include/android/log.h by
16 /// the Android Framework code.
17 pub enum Level {
18 Verbose = 2,
19 Debug = 3,
20 Info = 4,
21 Warn = 5,
22 Error = 6,
23 Fatal = 7,
24 }
25
26 // Defined in syslog linkage. See |vlog_syslog.cc|.
27 extern "C" {
SetLogLevelForTag(tag: *const c_char, level: u8)28 fn SetLogLevelForTag(tag: *const c_char, level: u8);
SetDefaultLogLevel(level: u8)29 fn SetDefaultLogLevel(level: u8);
30 }
31
32 /// Set a default level value for failed |level.to_u8()| of |Level::Info|.
33 const DEFAULT_LEVEL: u8 = 4;
34
35 /// Set the level of logs which will get printed for the given tag.
36 ///
37 /// Args:
38 /// tag - LOG_TAG for the system module that's logging.
39 /// level - Minimum log level that will be sent to syslog.
set_log_level_for_tag(tag: &str, level: Level)40 pub fn set_log_level_for_tag(tag: &str, level: Level) {
41 let cstr: CString = CString::new(tag).expect("CString::new failed on log tag");
42
43 unsafe {
44 SetLogLevelForTag(cstr.as_ptr(), level.to_u8().unwrap_or(DEFAULT_LEVEL));
45 }
46 }
47
48 /// Set the default log level for log tags. Will be overridden by any tag specific levels.
49 ///
50 /// Args:
51 /// level - Minimum log level that will be sent to syslog.
set_default_log_level(level: Level)52 pub fn set_default_log_level(level: Level) {
53 unsafe {
54 SetDefaultLogLevel(level.to_u8().unwrap_or(DEFAULT_LEVEL));
55 }
56 }
57