3d1ba6f0创建于 2024年9月20日历史提交
/*
 * The MIT License (MIT)
 * Copyright (C) 2024 Huawei Device Co., Ltd.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 */

#[macro_export]
macro_rules! hilog {
    (@call $log_label:ident, $level:expr, $fmt:literal, $(,)? $($processed_args:expr),* ) => (
        let mut buf = [0u8; 31]; // All tags ending in `\0` must not exceed 31 bytes in length.
        let tag = $log_label.tag.as_bytes();
        let min_len = std::cmp::min(tag.len(), 30); // 30 is the max length of tag.
        buf[0..min_len].copy_from_slice(&tag[0..min_len]);

        let mut log_str = format!($fmt, $($processed_args),*);
        log_str.push('\0');
        let res = unsafe {
            $crate::OH_LOG_Print($log_label.log_type, $level, $log_label.domain,
                buf.as_ptr() as *const c_char,
                log_str.as_ptr() as *const c_char)
        };
        res
    );

    (@rec $priv_flag:ident; $log_label:ident; $level:expr; $fmt:literal; ($arg:expr); $(,)? $($processed_args:expr),*) => {
        if ($priv_flag) {
            hilog!(@call $log_label, $level, $fmt, $($processed_args),*, "<private>");
        } else {
            hilog!(@call $log_label, $level, $fmt, $($processed_args),*, $arg);
        }
    };

    (@rec $priv_flag:ident; $log_label:ident; $level:expr; $fmt:literal; ($arg:expr, $($unprocessed_args:tt)*); $($processed_args:tt)*) => {
        if ($priv_flag) {
            hilog!(@rec $priv_flag; $log_label; $level; $fmt; ($($unprocessed_args)*); $($processed_args)*, "<private>");
        } else {
            hilog!(@rec $priv_flag; $log_label; $level; $fmt; ($($unprocessed_args)*); $($processed_args)*, $arg);
        }
    };

    // Public API
    ($log_label:ident, $level:expr, $fmt:literal, $($unprocessed_args:tt)*) => {
        let priv_flag = $log_label.priv_flag;
        hilog!(@rec priv_flag; $log_label; $level; $fmt; ($($unprocessed_args)*););
    };

    ($log_label:ident, $level:expr, $fmt:literal) => {
        hilog!(@call $log_label, $level, $fmt,);
    };
}

#[macro_export]
macro_rules! hilog_debug{
    ($log_label:ident, $($arg:tt)*) => (
        hilog!($log_label, $crate::LogLevel::Debug, $($arg)*)
    );
}

#[macro_export]
macro_rules! hilog_info{
    ($log_label:ident, $($arg:tt)*) => (
        hilog!($log_label, $crate::LogLevel::Info, $($arg)*)
    );
}

#[macro_export]
macro_rules! hilog_warn{
    ($log_label:ident, $($arg:tt)*) => (
        hilog!($log_label, $crate::LogLevel::Warn, $($arg)*)
    );
}

#[macro_export]
macro_rules! hilog_error{
    ($log_label:ident, $($arg:tt)*) => (
        hilog!($log_label, $crate::LogLevel::Error, $($arg)*)
    );
}

#[macro_export]
macro_rules! hilog_fatal{
    ($log_label:ident, $($arg:tt)*) => (
        hilog!($log_label, $crate::LogLevel::Fatal, $($arg)*)
    );
}