From 3ad260b3aeeb248ff0780ba8b7eedce16a05e1ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arkadiusz=20J=C4=99drzejewski?= Date: Thu, 22 Jan 2026 10:14:25 +0000 Subject: [PATCH] log: add Rust log init for C++ - Add lib for log init when Rust lib is used by C++. - Add example. --- score/mw/log/rust/score_log_bridge_init/BUILD | 68 ++++++++++++++++ .../examples/example_lib.hpp | 19 +++++ .../examples/example_lib.rs | 79 +++++++++++++++++++ .../score_log_bridge_init/examples/main.cpp | 28 +++++++ .../log/rust/score_log_bridge_init/src/ffi.rs | 50 ++++++++++++ .../src/score_log_bridge_init.cpp | 67 ++++++++++++++++ .../src/score_log_bridge_init.hpp | 65 +++++++++++++++ 7 files changed, 376 insertions(+) create mode 100644 score/mw/log/rust/score_log_bridge_init/BUILD create mode 100644 score/mw/log/rust/score_log_bridge_init/examples/example_lib.hpp create mode 100644 score/mw/log/rust/score_log_bridge_init/examples/example_lib.rs create mode 100644 score/mw/log/rust/score_log_bridge_init/examples/main.cpp create mode 100644 score/mw/log/rust/score_log_bridge_init/src/ffi.rs create mode 100644 score/mw/log/rust/score_log_bridge_init/src/score_log_bridge_init.cpp create mode 100644 score/mw/log/rust/score_log_bridge_init/src/score_log_bridge_init.hpp diff --git a/score/mw/log/rust/score_log_bridge_init/BUILD b/score/mw/log/rust/score_log_bridge_init/BUILD new file mode 100644 index 0000000..33abafb --- /dev/null +++ b/score/mw/log/rust/score_log_bridge_init/BUILD @@ -0,0 +1,68 @@ +# ******************************************************************************* +# Copyright (c) 2025 Contributors to the Eclipse Foundation +# +# See the NOTICE file(s) distributed with this work for additional +# information regarding copyright ownership. +# +# This program and the accompanying materials are made available under the +# terms of the Apache License Version 2.0 which is available at +# https://www.apache.org/licenses/LICENSE-2.0 +# +# SPDX-License-Identifier: Apache-2.0 +# ******************************************************************************* + +load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library") +load("@rules_rust//rust:defs.bzl", "rust_static_library") + +rust_static_library( + name = "ffi", + srcs = ["src/ffi.rs"], + edition = "2021", + visibility = ["//visibility:private"], + deps = [ + "//score/mw/log/rust/score_log_bridge", + ], +) + +cc_library( + name = "score_log_bridge_init", + srcs = ["src/score_log_bridge_init.cpp"], + hdrs = ["src/score_log_bridge_init.hpp"], + includes = ["src/"], + visibility = ["//visibility:public"], + deps = [ + ":ffi", + # Link dependency required by `:ffi`. + "//score/mw/log/detail/common:recorder_factory", + ], +) + +# Example consists of Rust library, C++ library, C++ application. + +rust_static_library( + name = "example_lib_rs", + srcs = ["examples/example_lib.rs"], + edition = "2021", + visibility = ["//visibility:private"], + deps = [ + "//score/mw/log/rust/score_log_bridge", + "@score_baselibs_rust//src/log/score_log", + ], +) + +cc_library( + name = "example_lib_cpp", + srcs = ["examples/example_lib.hpp"], + visibility = ["//visibility:private"], + deps = [":example_lib_rs"], +) + +cc_binary( + name = "example", + srcs = ["examples/main.cpp"], + visibility = ["//visibility:public"], + deps = [ + ":example_lib_cpp", + ":score_log_bridge_init", + ], +) diff --git a/score/mw/log/rust/score_log_bridge_init/examples/example_lib.hpp b/score/mw/log/rust/score_log_bridge_init/examples/example_lib.hpp new file mode 100644 index 0000000..3a4cd7c --- /dev/null +++ b/score/mw/log/rust/score_log_bridge_init/examples/example_lib.hpp @@ -0,0 +1,19 @@ +/******************************************************************************** + * Copyright (c) 2025 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * This program and the accompanying materials are made available under the + * terms of the Apache License Version 2.0 which is available at + * https://www.apache.org/licenses/LICENSE-2.0 + * + * SPDX-License-Identifier: Apache-2.0 + ********************************************************************************/ + +#pragma once + +extern "C" { +void show_logs_global_logger(); +void show_logs_local_logger(); +} diff --git a/score/mw/log/rust/score_log_bridge_init/examples/example_lib.rs b/score/mw/log/rust/score_log_bridge_init/examples/example_lib.rs new file mode 100644 index 0000000..1969642 --- /dev/null +++ b/score/mw/log/rust/score_log_bridge_init/examples/example_lib.rs @@ -0,0 +1,79 @@ +// +// Copyright (c) 2025 Contributors to the Eclipse Foundation +// +// See the NOTICE file(s) distributed with this work for additional +// information regarding copyright ownership. +// +// This program and the accompanying materials are made available under the +// terms of the Apache License Version 2.0 which is available at +// +// +// SPDX-License-Identifier: Apache-2.0 +// + +//! Module contains functions printing example logs. +//! Based on `//score/mw/log/rust/score_log_bridge:example`. + +use score_log::{debug, error, fatal, info, trace, warn, Log}; +use score_log_bridge::ScoreLoggerBuilder; + +/// Show example logs using global logger. +#[no_mangle] +extern "C" fn show_logs_global_logger() { + // Regular log usage. + trace!("This is a trace log - hidden"); + debug!("This is a debug log - hidden"); + info!("This is an info log"); + warn!("This is a warn log"); + error!("This is an error log"); + fatal!("This is a fatal log"); + + // Log with modified context. + trace!(context: "EX1", "This is a trace log - hidden"); + debug!(context: "EX1", "This is a debug log - hidden"); + info!(context: "EX1", "This is an info log"); + warn!(context: "EX1", "This is a warn log"); + error!(context: "EX1", "This is an error log"); + fatal!(context: "EX1", "This is a fatal log"); + + // Log with numeric values. + let x1 = 123.4; + let x2 = 111; + let x3 = true; + let x4 = -0x3Fi8; + error!( + "This is an error log with numeric values: {} {} {} {:x}", + x1, x2, x3, x4, + ); +} + +/// Show example logs using function-logger logger instance. +#[no_mangle] +extern "C" fn show_logs_local_logger() { + // Use logger instance with modified context. + let logger = ScoreLoggerBuilder::new() + .context("ALFA") + .show_module(false) + .show_file(true) + .show_line(false) + .build(); + + // Log with provided logger. + trace!( + logger: logger, + "This is a trace log - hidden" + ); + debug!(logger: logger, "This is a debug log - hidden"); + info!(logger: logger, "This is an info log"); + warn!(logger: logger, "This is a warn log"); + error!(logger: logger, "This is an error log"); + fatal!(logger: logger, "This is an fatal log"); + + // Log with provided logger and modified context. + trace!(logger: logger, context: "EX2", "This is a trace log - hidden"); + debug!(logger: logger, context: "EX2", "This is a debug log - hidden"); + info!(logger: logger, context: "EX2", "This is an info log"); + warn!(logger: logger, context: "EX2", "This is a warn log"); + error!(logger: logger, context: "EX2", "This is an error log"); + fatal!(logger: logger, context: "EX2", "This is an fatal log"); +} diff --git a/score/mw/log/rust/score_log_bridge_init/examples/main.cpp b/score/mw/log/rust/score_log_bridge_init/examples/main.cpp new file mode 100644 index 0000000..3c97c65 --- /dev/null +++ b/score/mw/log/rust/score_log_bridge_init/examples/main.cpp @@ -0,0 +1,28 @@ +/******************************************************************************** + * Copyright (c) 2025 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * This program and the accompanying materials are made available under the + * terms of the Apache License Version 2.0 which is available at + * https://www.apache.org/licenses/LICENSE-2.0 + * + * SPDX-License-Identifier: Apache-2.0 + ********************************************************************************/ + +#include "example_lib.hpp" +#include "score_log_bridge_init.hpp" + +int main() +{ + using namespace score::mw::log::rust; + + ScoreLoggerBuilder builder; + builder.Context("ABCD").ShowModule(true).ShowFile(true).ShowLine(true).SetAsDefaultLogger(); + + show_logs_global_logger(); + show_logs_local_logger(); + + return 0; +} diff --git a/score/mw/log/rust/score_log_bridge_init/src/ffi.rs b/score/mw/log/rust/score_log_bridge_init/src/ffi.rs new file mode 100644 index 0000000..1acd0e9 --- /dev/null +++ b/score/mw/log/rust/score_log_bridge_init/src/ffi.rs @@ -0,0 +1,50 @@ +// +// Copyright (c) 2025 Contributors to the Eclipse Foundation +// +// See the NOTICE file(s) distributed with this work for additional +// information regarding copyright ownership. +// +// This program and the accompanying materials are made available under the +// terms of the Apache License Version 2.0 which is available at +// +// +// SPDX-License-Identifier: Apache-2.0 +// + +use core::ffi::c_char; +use core::slice::from_raw_parts; +use score_log_bridge::ScoreLoggerBuilder; + +#[no_mangle] +extern "C" fn set_default_logger( + context_ptr: *const c_char, + context_size: usize, + show_module: *const bool, + show_file: *const bool, + show_line: *const bool, +) { + let mut builder = ScoreLoggerBuilder::new(); + + // Set parameters if non-null (option-like). + if !context_ptr.is_null() { + let context = unsafe { + let slice = from_raw_parts(context_ptr.cast(), context_size); + str::from_utf8_unchecked(slice) + }; + builder = builder.context(context); + } + + if !show_module.is_null() { + builder = builder.show_module(unsafe { *show_module }); + } + + if !show_file.is_null() { + builder = builder.show_file(unsafe { *show_file }); + } + + if !show_line.is_null() { + builder = builder.show_line(unsafe { *show_line }); + } + + builder.set_as_default_logger(); +} diff --git a/score/mw/log/rust/score_log_bridge_init/src/score_log_bridge_init.cpp b/score/mw/log/rust/score_log_bridge_init/src/score_log_bridge_init.cpp new file mode 100644 index 0000000..97e4122 --- /dev/null +++ b/score/mw/log/rust/score_log_bridge_init/src/score_log_bridge_init.cpp @@ -0,0 +1,67 @@ +/******************************************************************************** + * Copyright (c) 2025 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * This program and the accompanying materials are made available under the + * terms of the Apache License Version 2.0 which is available at + * https://www.apache.org/licenses/LICENSE-2.0 + * + * SPDX-License-Identifier: Apache-2.0 + ********************************************************************************/ + +#include "score_log_bridge_init.hpp" + +extern "C" void set_default_logger(const char* context_ptr, + size_t context_size, + const bool* show_module, + const bool* show_file, + const bool* show_line); + +namespace score::mw::log::rust +{ + +ScoreLoggerBuilder& ScoreLoggerBuilder::Context(const std::string& context) noexcept +{ + context_ = context; + return *this; +} + +ScoreLoggerBuilder& ScoreLoggerBuilder::ShowModule(bool show_module) noexcept +{ + show_module_ = show_module; + return *this; +} + +ScoreLoggerBuilder& ScoreLoggerBuilder::ShowFile(bool show_file) noexcept +{ + show_file_ = show_file; + return *this; +} + +ScoreLoggerBuilder& ScoreLoggerBuilder::ShowLine(bool show_line) noexcept +{ + show_line_ = show_line; + return *this; +} + +void ScoreLoggerBuilder::SetAsDefaultLogger() noexcept +{ + const char* context_ptr{nullptr}; + size_t context_size{0}; + if (context_) + { + auto value{context_.value()}; + context_ptr = value.c_str(); + context_size = value.size(); + } + + const bool* show_module{show_module_ ? &show_module_.value() : nullptr}; + const bool* show_file{show_file_ ? &show_file_.value() : nullptr}; + const bool* show_line{show_line_ ? &show_line_.value() : nullptr}; + + set_default_logger(context_ptr, context_size, show_module, show_file, show_line); +} + +} // namespace score::mw::log::rust diff --git a/score/mw/log/rust/score_log_bridge_init/src/score_log_bridge_init.hpp b/score/mw/log/rust/score_log_bridge_init/src/score_log_bridge_init.hpp new file mode 100644 index 0000000..05439ca --- /dev/null +++ b/score/mw/log/rust/score_log_bridge_init/src/score_log_bridge_init.hpp @@ -0,0 +1,65 @@ +/******************************************************************************** + * Copyright (c) 2025 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * This program and the accompanying materials are made available under the + * terms of the Apache License Version 2.0 which is available at + * https://www.apache.org/licenses/LICENSE-2.0 + * + * SPDX-License-Identifier: Apache-2.0 + ********************************************************************************/ + +#pragma once + +#include +#include + +namespace score::mw::log::rust +{ + +/// @brief +/// Builder for logger used by Rust libraries. +/// +/// @note +/// If parameter is not set explicitly then Rust-side default is used. +/// Only global logger setup is allowed. +/// `config` method is not exposed. +class ScoreLoggerBuilder final +{ + public: + /// @brief Set context for the logger. + /// @param context + /// Context name. + /// Only ASCII characters are allowed. + /// Max 4 characters are used. Rest of the provided string will be trimmed. + /// @return This builder. + ScoreLoggerBuilder& Context(const std::string& context) noexcept; + + /// @brief Show module name in logs. + /// @param show_module Value to set. + /// @return This builder. + ScoreLoggerBuilder& ShowModule(bool show_module) noexcept; + + /// @brief Show file name in logs. + /// @param show_module Value to set. + /// @return This builder. + ScoreLoggerBuilder& ShowFile(bool show_file) noexcept; + + /// @brief Show line number in logs. + /// @param show_module Value to set. + /// @return This builder. + ScoreLoggerBuilder& ShowLine(bool show_line) noexcept; + + /// @brief Initialize default logger with provided parameters. + void SetAsDefaultLogger() noexcept; + + private: + std::optional context_; + std::optional show_module_; + std::optional show_file_; + std::optional show_line_; +}; + +} // namespace score::mw::log::rust