Skip to content

Commit 27a0d7d

Browse files
committed
log: add Rust log init for C++
- Add lib for log init when Rust lib is used by C++. - Add example.
1 parent 8d14d2b commit 27a0d7d

7 files changed

Lines changed: 382 additions & 0 deletions

File tree

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# *******************************************************************************
2+
# Copyright (c) 2025 Contributors to the Eclipse Foundation
3+
#
4+
# See the NOTICE file(s) distributed with this work for additional
5+
# information regarding copyright ownership.
6+
#
7+
# This program and the accompanying materials are made available under the
8+
# terms of the Apache License Version 2.0 which is available at
9+
# https://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# SPDX-License-Identifier: Apache-2.0
12+
# *******************************************************************************
13+
14+
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library")
15+
load("@rules_rust//rust:defs.bzl", "rust_static_library")
16+
17+
rust_static_library(
18+
name = "ffi",
19+
srcs = ["src/ffi.rs"],
20+
edition = "2021",
21+
visibility = ["//visibility:private"],
22+
deps = [
23+
"//score/mw/log/rust/score_log_bridge",
24+
],
25+
)
26+
27+
cc_library(
28+
name = "score_log_bridge_init",
29+
srcs = ["src/score_log_bridge_init.cpp"],
30+
hdrs = ["src/score_log_bridge_init.hpp"],
31+
includes = ["src/"],
32+
visibility = ["//visibility:public"],
33+
deps = [
34+
":ffi",
35+
# Link dependency required by `:ffi`.
36+
"//score/mw/log/detail/common:recorder_factory",
37+
],
38+
)
39+
40+
# Example consists of Rust library, C++ library, C++ application.
41+
42+
rust_static_library(
43+
name = "example_lib_rs",
44+
srcs = ["examples/example_lib.rs"],
45+
edition = "2021",
46+
visibility = ["//visibility:private"],
47+
deps = [
48+
"//score/mw/log/rust/score_log_bridge",
49+
"@score_baselibs_rust//src/log/score_log",
50+
],
51+
)
52+
53+
cc_library(
54+
name = "example_lib_cpp",
55+
srcs = ["examples/example_lib.hpp"],
56+
visibility = ["//visibility:private"],
57+
deps = [":example_lib_rs"],
58+
)
59+
60+
cc_binary(
61+
name = "example",
62+
srcs = ["examples/main.cpp"],
63+
linkopts = select({
64+
"@platforms//os:qnx": [
65+
"-lsocket",
66+
],
67+
"//conditions:default": [],
68+
}),
69+
visibility = ["//visibility:public"],
70+
deps = [
71+
":example_lib_cpp",
72+
":score_log_bridge_init",
73+
],
74+
)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/********************************************************************************
2+
* Copyright (c) 2025 Contributors to the Eclipse Foundation
3+
*
4+
* See the NOTICE file(s) distributed with this work for additional
5+
* information regarding copyright ownership.
6+
*
7+
* This program and the accompanying materials are made available under the
8+
* terms of the Apache License Version 2.0 which is available at
9+
* https://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* SPDX-License-Identifier: Apache-2.0
12+
********************************************************************************/
13+
14+
#pragma once
15+
16+
extern "C" {
17+
void show_logs_global_logger();
18+
void show_logs_local_logger();
19+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
//
2+
// Copyright (c) 2025 Contributors to the Eclipse Foundation
3+
//
4+
// See the NOTICE file(s) distributed with this work for additional
5+
// information regarding copyright ownership.
6+
//
7+
// This program and the accompanying materials are made available under the
8+
// terms of the Apache License Version 2.0 which is available at
9+
// <https://www.apache.org/licenses/LICENSE-2.0>
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
14+
//! Module contains functions printing example logs.
15+
//! Based on `//score/mw/log/rust/score_log_bridge:example`.
16+
17+
use score_log::{debug, error, fatal, info, trace, warn, Log};
18+
use score_log_bridge::ScoreLoggerBuilder;
19+
20+
/// Show example logs using global logger.
21+
#[no_mangle]
22+
extern "C" fn show_logs_global_logger() {
23+
// Regular log usage.
24+
trace!("This is a trace log - hidden");
25+
debug!("This is a debug log - hidden");
26+
info!("This is an info log");
27+
warn!("This is a warn log");
28+
error!("This is an error log");
29+
fatal!("This is a fatal log");
30+
31+
// Log with modified context.
32+
trace!(context: "EX1", "This is a trace log - hidden");
33+
debug!(context: "EX1", "This is a debug log - hidden");
34+
info!(context: "EX1", "This is an info log");
35+
warn!(context: "EX1", "This is a warn log");
36+
error!(context: "EX1", "This is an error log");
37+
fatal!(context: "EX1", "This is a fatal log");
38+
39+
// Log with numeric values.
40+
let x1 = 123.4;
41+
let x2 = 111;
42+
let x3 = true;
43+
let x4 = -0x3Fi8;
44+
error!(
45+
"This is an error log with numeric values: {} {} {} {:x}",
46+
x1, x2, x3, x4,
47+
);
48+
}
49+
50+
/// Show example logs using function-logger logger instance.
51+
#[no_mangle]
52+
extern "C" fn show_logs_local_logger() {
53+
// Use logger instance with modified context.
54+
let logger = ScoreLoggerBuilder::new()
55+
.context("ALFA")
56+
.show_module(false)
57+
.show_file(true)
58+
.show_line(false)
59+
.build();
60+
61+
// Log with provided logger.
62+
trace!(
63+
logger: logger,
64+
"This is a trace log - hidden"
65+
);
66+
debug!(logger: logger, "This is a debug log - hidden");
67+
info!(logger: logger, "This is an info log");
68+
warn!(logger: logger, "This is a warn log");
69+
error!(logger: logger, "This is an error log");
70+
fatal!(logger: logger, "This is an fatal log");
71+
72+
// Log with provided logger and modified context.
73+
trace!(logger: logger, context: "EX2", "This is a trace log - hidden");
74+
debug!(logger: logger, context: "EX2", "This is a debug log - hidden");
75+
info!(logger: logger, context: "EX2", "This is an info log");
76+
warn!(logger: logger, context: "EX2", "This is a warn log");
77+
error!(logger: logger, context: "EX2", "This is an error log");
78+
fatal!(logger: logger, context: "EX2", "This is an fatal log");
79+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/********************************************************************************
2+
* Copyright (c) 2025 Contributors to the Eclipse Foundation
3+
*
4+
* See the NOTICE file(s) distributed with this work for additional
5+
* information regarding copyright ownership.
6+
*
7+
* This program and the accompanying materials are made available under the
8+
* terms of the Apache License Version 2.0 which is available at
9+
* https://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* SPDX-License-Identifier: Apache-2.0
12+
********************************************************************************/
13+
14+
#include "example_lib.hpp"
15+
#include "score_log_bridge_init.hpp"
16+
17+
int main()
18+
{
19+
using namespace score::mw::log::rust;
20+
21+
ScoreLoggerBuilder builder;
22+
builder.Context("ABCD").ShowModule(true).ShowFile(true).ShowLine(true).SetAsDefaultLogger();
23+
24+
show_logs_global_logger();
25+
show_logs_local_logger();
26+
27+
return 0;
28+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
//
2+
// Copyright (c) 2025 Contributors to the Eclipse Foundation
3+
//
4+
// See the NOTICE file(s) distributed with this work for additional
5+
// information regarding copyright ownership.
6+
//
7+
// This program and the accompanying materials are made available under the
8+
// terms of the Apache License Version 2.0 which is available at
9+
// <https://www.apache.org/licenses/LICENSE-2.0>
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
14+
use core::ffi::c_char;
15+
use core::slice::from_raw_parts;
16+
use score_log_bridge::ScoreLoggerBuilder;
17+
18+
#[no_mangle]
19+
extern "C" fn set_default_logger(
20+
context_ptr: *const c_char,
21+
context_size: usize,
22+
show_module: *const bool,
23+
show_file: *const bool,
24+
show_line: *const bool,
25+
) {
26+
let mut builder = ScoreLoggerBuilder::new();
27+
28+
// Set parameters if non-null (option-like).
29+
if !context_ptr.is_null() {
30+
let context = unsafe {
31+
let slice = from_raw_parts(context_ptr.cast(), context_size);
32+
str::from_utf8_unchecked(slice)
33+
};
34+
builder = builder.context(context);
35+
}
36+
37+
if !show_module.is_null() {
38+
builder = builder.show_module(unsafe { *show_module });
39+
}
40+
41+
if !show_file.is_null() {
42+
builder = builder.show_file(unsafe { *show_file });
43+
}
44+
45+
if !show_line.is_null() {
46+
builder = builder.show_line(unsafe { *show_line });
47+
}
48+
49+
builder.set_as_default_logger();
50+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/********************************************************************************
2+
* Copyright (c) 2025 Contributors to the Eclipse Foundation
3+
*
4+
* See the NOTICE file(s) distributed with this work for additional
5+
* information regarding copyright ownership.
6+
*
7+
* This program and the accompanying materials are made available under the
8+
* terms of the Apache License Version 2.0 which is available at
9+
* https://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* SPDX-License-Identifier: Apache-2.0
12+
********************************************************************************/
13+
14+
#include "score_log_bridge_init.hpp"
15+
16+
extern "C" void set_default_logger(const char* context_ptr,
17+
size_t context_size,
18+
const bool* show_module,
19+
const bool* show_file,
20+
const bool* show_line);
21+
22+
namespace score::mw::log::rust
23+
{
24+
25+
ScoreLoggerBuilder& ScoreLoggerBuilder::Context(const std::string& context) noexcept
26+
{
27+
context_ = context;
28+
return *this;
29+
}
30+
31+
ScoreLoggerBuilder& ScoreLoggerBuilder::ShowModule(bool show_module) noexcept
32+
{
33+
show_module_ = show_module;
34+
return *this;
35+
}
36+
37+
ScoreLoggerBuilder& ScoreLoggerBuilder::ShowFile(bool show_file) noexcept
38+
{
39+
show_file_ = show_file;
40+
return *this;
41+
}
42+
43+
ScoreLoggerBuilder& ScoreLoggerBuilder::ShowLine(bool show_line) noexcept
44+
{
45+
show_line_ = show_line;
46+
return *this;
47+
}
48+
49+
void ScoreLoggerBuilder::SetAsDefaultLogger() noexcept
50+
{
51+
const char* context_ptr{nullptr};
52+
size_t context_size{0};
53+
if (context_)
54+
{
55+
auto value{context_.value()};
56+
context_ptr = value.c_str();
57+
context_size = value.size();
58+
}
59+
60+
const bool* show_module{show_module_ ? &show_module_.value() : nullptr};
61+
const bool* show_file{show_file_ ? &show_file_.value() : nullptr};
62+
const bool* show_line{show_line_ ? &show_line_.value() : nullptr};
63+
64+
set_default_logger(context_ptr, context_size, show_module, show_file, show_line);
65+
}
66+
67+
} // namespace score::mw::log::rust
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/********************************************************************************
2+
* Copyright (c) 2025 Contributors to the Eclipse Foundation
3+
*
4+
* See the NOTICE file(s) distributed with this work for additional
5+
* information regarding copyright ownership.
6+
*
7+
* This program and the accompanying materials are made available under the
8+
* terms of the Apache License Version 2.0 which is available at
9+
* https://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* SPDX-License-Identifier: Apache-2.0
12+
********************************************************************************/
13+
14+
#pragma once
15+
16+
#include <optional>
17+
#include <string>
18+
19+
namespace score::mw::log::rust
20+
{
21+
22+
/// @brief
23+
/// Builder for logger used by Rust libraries.
24+
///
25+
/// @note
26+
/// If parameter is not set explicitly then Rust-side default is used.
27+
/// Only global logger setup is allowed.
28+
/// `config` method is not exposed.
29+
class ScoreLoggerBuilder final
30+
{
31+
public:
32+
/// @brief Set context for the logger.
33+
/// @param context
34+
/// Context name.
35+
/// Only ASCII characters are allowed.
36+
/// Max 4 characters are used. Rest of the provided string will be trimmed.
37+
/// @return This builder.
38+
ScoreLoggerBuilder& Context(const std::string& context) noexcept;
39+
40+
/// @brief Show module name in logs.
41+
/// @param show_module Value to set.
42+
/// @return This builder.
43+
ScoreLoggerBuilder& ShowModule(bool show_module) noexcept;
44+
45+
/// @brief Show file name in logs.
46+
/// @param show_module Value to set.
47+
/// @return This builder.
48+
ScoreLoggerBuilder& ShowFile(bool show_file) noexcept;
49+
50+
/// @brief Show line number in logs.
51+
/// @param show_module Value to set.
52+
/// @return This builder.
53+
ScoreLoggerBuilder& ShowLine(bool show_line) noexcept;
54+
55+
/// @brief Initialize default logger with provided parameters.
56+
void SetAsDefaultLogger() noexcept;
57+
58+
private:
59+
std::optional<std::string> context_;
60+
std::optional<bool> show_module_;
61+
std::optional<bool> show_file_;
62+
std::optional<bool> show_line_;
63+
};
64+
65+
} // namespace score::mw::log::rust

0 commit comments

Comments
 (0)