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

base / logging / rust_logger.rs [blame]

// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

chromium::import! {
    "//base:logging_log_severity_bindgen" as log_severity;
    "//base:logging_rust_log_integration_bindgen" as rust_log_integration;
}

use log::Level::{Debug, Error, Info, Trace, Warn};
use log::{LevelFilter, Metadata, Record};
use log_severity::logging::{LOGGING_ERROR, LOGGING_INFO, LOGGING_WARNING};
use rust_log_integration::logging::internal::print_rust_log;
use std::ffi::CString;

struct RustLogger;

impl log::Log for RustLogger {
    fn enabled(&self, _metadata: &Metadata) -> bool {
        // Always enabled, as it's controlled by command line flags managed by the C++
        // implementation.
        true
    }

    fn log(&self, record: &Record) {
        // TODO(thiruak1024@gmail.com): Rather than using heap allocation to pass |msg|
        // and |file|, we should return a pointer and size object to leverage the
        // string_view object in C++. https://crbug.com/371112531
        let msg = match record.args().as_str() {
            Some(s) => CString::new(s),
            None => CString::new(&*record.args().to_string()),
        }
        .expect("CString::new failed to create the log message!");
        let file = CString::new(record.file().unwrap())
            .expect("CString::new failed to create the log file name!");
        unsafe {
            print_rust_log(
                msg.as_ptr(),
                file.as_ptr(),
                record.line().unwrap() as i32,
                match record.metadata().level() {
                    Error => LOGGING_ERROR,
                    Warn => LOGGING_WARNING,
                    Info => LOGGING_INFO,
                    // Note that Debug and Trace level logs are dropped at
                    // compile time at the macro call-site when DCHECK_IS_ON()
                    // is false. This is done through a Cargo feature.
                    Debug | Trace => LOGGING_INFO,
                },
                record.metadata().level() == Trace,
            )
        }
    }
    fn flush(&self) {}
}

static RUST_LOGGER: RustLogger = RustLogger;

#[cxx::bridge(namespace = "logging::internal")]
mod ffi {
    extern "Rust" {
        fn init_rust_log_crate();
    }
}

pub fn init_rust_log_crate() {
    // An error may occur if set_logger has already been called, which can happen
    // during unit tests. In that case, return from the method without executing the
    // subsequent code.
    if let Err(_) = log::set_logger(&RUST_LOGGER) {
        return;
    };
    log::set_max_level(LevelFilter::Trace);
}