Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 97eb6264 authored by Yun-hao Chung's avatar Yun-hao Chung Committed by Gerrit Code Review
Browse files

Merge "Floss: Retry and avoid crashing when failing to connect to syslog" into main

parents 5fe550e4 3d42d402
Loading
Loading
Loading
Loading
+11 −0
Original line number Diff line number Diff line
@@ -59,6 +59,8 @@ const VERBOSE_ONLY_LOG_TAGS: &[&str] = &[
    "uipc",      // Userspace IPC implementation
];

const INIT_LOGGING_MAX_RETRY: u8 = 3;

/// Runs the Bluetooth daemon serving D-Bus IPC.
fn main() -> Result<(), Box<dyn Error>> {
    let matches = App::new("Bluetooth Adapter Daemon")
@@ -125,7 +127,16 @@ fn main() -> Result<(), Box<dyn Error>> {

    // Forward --hci to Fluoride.
    init_flags.push(format!("--hci={}", hci_index));

    let logging = Arc::new(Mutex::new(Box::new(BluetoothLogging::new(is_debug, log_output))));
    // TODO(b/307171804): Investigate why connecting to unix syslog might fail.
    // Retry it a few times. Ignore the failure if fails too many times.
    for _ in 0..INIT_LOGGING_MAX_RETRY {
        match logging.lock().unwrap().initialize() {
            Ok(_) => break,
            Err(_) => continue,
        }
    }

    // Always treat discovery as classic only
    init_flags.push(String::from("INIT_classic_discovery_only=true"));
+18 −7
Original line number Diff line number Diff line
use bt_common::init_flags;
use log::LevelFilter;
use syslog::{BasicLogger, Facility, Formatter3164};
use syslog::{BasicLogger, Error, Facility, Formatter3164};

use log_panics;

@@ -16,13 +16,20 @@ pub trait IBluetoothLogging {
/// Logging related implementation.
pub struct BluetoothLogging {
    is_debug: bool,
    is_stderr: bool,
    is_initialized: bool,
}

impl BluetoothLogging {
    pub fn new(is_debug: bool, log_output: &str) -> Self {
        let level = if is_debug { LevelFilter::Debug } else { LevelFilter::Info };
        let is_stderr = log_output == "stderr";
        Self { is_debug, is_stderr, is_initialized: false }
    }

    pub fn initialize(&mut self) -> Result<(), syslog::Error> {
        let level = if self.is_debug { LevelFilter::Debug } else { LevelFilter::Info };

        if log_output == "stderr" {
        if self.is_stderr {
            env_logger::Builder::new().filter(None, level).init();
        } else {
            let formatter = Formatter3164 {
@@ -32,22 +39,26 @@ impl BluetoothLogging {
                pid: 0,
            };

            let logger = syslog::unix(formatter).expect("could not connect to syslog");
            let logger = syslog::unix(formatter)?;
            let _ = log::set_boxed_logger(Box::new(BasicLogger::new(logger)))
                .map(|()| log::set_max_level(level));
            log_panics::init();
        }

        Self { is_debug }
        self.is_initialized = true;
        Ok(())
    }
}

impl IBluetoothLogging for BluetoothLogging {
    fn is_debug_enabled(&self) -> bool {
        self.is_debug
        self.is_initialized && self.is_debug
    }

    fn set_debug_logging(&mut self, enabled: bool) {
        if !self.is_initialized {
            return;
        }

        self.is_debug = enabled;

        // Update log level in Linux stack.