Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions score/mw/log/rust/score_log_bridge/examples/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
//

use score_log::{debug, error, fatal, info, trace, warn, Log};
use score_log_bridge::ScoreLoggerBuilder;
use score_log_bridge::ScoreLogBridgeBuilder;
use std::path::PathBuf;

fn main() {
Expand All @@ -24,8 +24,8 @@ fn main() {
.join("config")
.join("logging.json");

// Initialize `ScoreLogger` as a default logger.
ScoreLoggerBuilder::new()
// Initialize `ScoreLogBridge` as a default logger.
ScoreLogBridgeBuilder::new()
.show_module(false)
.show_file(true)
.show_line(false)
Expand Down Expand Up @@ -59,7 +59,7 @@ fn main() {
);

// Use logger instance with modified context.
let logger = ScoreLoggerBuilder::new()
let logger = ScoreLogBridgeBuilder::new()
.context("ALFA")
.show_module(false)
.show_file(true)
Expand Down
4 changes: 2 additions & 2 deletions score/mw/log/rust/score_log_bridge/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@
#![warn(clippy::alloc_instead_of_core)]

mod ffi;
mod score_logger;
mod score_log_bridge;

pub use crate::score_logger::{ScoreLogger, ScoreLoggerBuilder};
pub use crate::score_log_bridge::{ScoreLogBridge, ScoreLogBridgeBuilder};
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ use score_log::{Log, Metadata, Record};
use std::env::{set_var, var_os};
use std::path::PathBuf;

/// Builder for the [`ScoreLogger`].
pub struct ScoreLoggerBuilder {
/// Builder for the [`ScoreLogBridge`].
pub struct ScoreLogBridgeBuilder {
context: Context,
show_module: bool,
show_file: bool,
show_line: bool,
config_path: Option<PathBuf>,
}

impl ScoreLoggerBuilder {
impl ScoreLogBridgeBuilder {
/// Create builder with default parameters.
///
/// # Panics
Expand All @@ -39,7 +39,7 @@ impl ScoreLoggerBuilder {
Self::default()
}

/// Set context for the [`ScoreLogger`].
/// Set context for the [`ScoreLogBridge`].
///
/// Only ASCII characters are allowed.
/// Max 4 characters are used. Rest of the provided string will be trimmed.
Expand Down Expand Up @@ -77,10 +77,10 @@ impl ScoreLoggerBuilder {
self
}

/// Build the [`ScoreLogger`] with provided context and configuration.
pub fn build(self) -> ScoreLogger {
/// Build the [`ScoreLogBridge`] with provided context and configuration.
pub fn build(self) -> ScoreLogBridge {
let recorder = Recorder::new();
ScoreLogger {
ScoreLogBridge {
context: self.context,
show_module: self.show_module,
show_file: self.show_file,
Expand All @@ -89,7 +89,7 @@ impl ScoreLoggerBuilder {
}
}

/// Build the [`ScoreLogger`] and set it as the default logger.
/// Build the [`ScoreLogBridge`] and set it as the default logger.
///
/// # Safety
///
Expand Down Expand Up @@ -126,7 +126,7 @@ impl ScoreLoggerBuilder {
}
}

impl Default for ScoreLoggerBuilder {
impl Default for ScoreLogBridgeBuilder {
/// Create builder with default parameters.
///
/// # Panics
Expand Down Expand Up @@ -154,22 +154,22 @@ impl Default for ScoreLoggerBuilder {
}

/// C++-based logger implementation.
pub struct ScoreLogger {
pub struct ScoreLogBridge {
context: Context,
show_module: bool,
show_file: bool,
show_line: bool,
recorder: Recorder,
}

impl ScoreLogger {
impl ScoreLogBridge {
/// Current log level for provided context.
pub(crate) fn log_level(&self, context: &Context) -> LogLevel {
self.recorder.log_level(context)
}
}

impl Log for ScoreLogger {
impl Log for ScoreLogBridge {
fn enabled(&self, metadata: &Metadata) -> bool {
let context = Context::from(metadata.context());
self.log_level(&context) >= metadata.level().into()
Expand Down Expand Up @@ -222,13 +222,13 @@ impl Log for ScoreLogger {

#[cfg(test)]
mod tests {
use crate::ScoreLoggerBuilder;
use crate::ScoreLogBridgeBuilder;
use std::env::var_os;
use std::path::PathBuf;

#[test]
fn test_builder_new() {
let builder = ScoreLoggerBuilder::new();
let builder = ScoreLogBridgeBuilder::new();
assert_eq!(builder.context, "DFLT".into());
assert!(!builder.show_module);
assert!(!builder.show_file);
Expand All @@ -238,7 +238,7 @@ mod tests {

#[test]
fn test_builder_default() {
let builder = ScoreLoggerBuilder::default();
let builder = ScoreLogBridgeBuilder::default();
assert_eq!(builder.context, "DFLT".into());
assert!(!builder.show_module);
assert!(!builder.show_file);
Expand All @@ -248,7 +248,7 @@ mod tests {

#[test]
fn test_builder_context() {
let builder = ScoreLoggerBuilder::new().context("NEW_CONTEXT");
let builder = ScoreLogBridgeBuilder::new().context("NEW_CONTEXT");
assert_eq!(builder.context, "NEW_CONTEXT".into());
assert!(!builder.show_module);
assert!(!builder.show_file);
Expand All @@ -258,7 +258,7 @@ mod tests {

#[test]
fn test_builder_show_module() {
let builder = ScoreLoggerBuilder::new().show_module(true);
let builder = ScoreLogBridgeBuilder::new().show_module(true);
assert_eq!(builder.context, "DFLT".into());
assert!(builder.show_module);
assert!(!builder.show_file);
Expand All @@ -268,7 +268,7 @@ mod tests {

#[test]
fn test_builder_show_file() {
let builder = ScoreLoggerBuilder::new().show_file(true);
let builder = ScoreLogBridgeBuilder::new().show_file(true);
assert_eq!(builder.context, "DFLT".into());
assert!(!builder.show_module);
assert!(builder.show_file);
Expand All @@ -278,7 +278,7 @@ mod tests {

#[test]
fn test_builder_show_line() {
let builder = ScoreLoggerBuilder::new().show_line(true);
let builder = ScoreLogBridgeBuilder::new().show_line(true);
assert_eq!(builder.context, "DFLT".into());
assert!(!builder.show_module);
assert!(!builder.show_file);
Expand All @@ -288,7 +288,7 @@ mod tests {

#[test]
fn test_builder_config_path() {
let builder = ScoreLoggerBuilder::new().config(PathBuf::from("/some/path"));
let builder = ScoreLogBridgeBuilder::new().config(PathBuf::from("/some/path"));
assert_eq!(builder.context, "DFLT".into());
assert!(!builder.show_module);
assert!(!builder.show_file);
Expand All @@ -298,7 +298,7 @@ mod tests {

#[test]
fn test_builder_chained() {
let builder = ScoreLoggerBuilder::new()
let builder = ScoreLogBridgeBuilder::new()
.context("NEW_CONTEXT")
.show_module(true)
.show_file(true)
Expand All @@ -313,7 +313,7 @@ mod tests {

#[test]
fn test_builder_build() {
let logger = ScoreLoggerBuilder::new()
let logger = ScoreLogBridgeBuilder::new()
.context("NEW_CONTEXT")
.show_module(true)
.show_file(true)
Expand All @@ -332,7 +332,7 @@ mod tests {
assert!(var_os(KEY).is_none());

let config_path = PathBuf::from("/some/path");
ScoreLoggerBuilder::new()
ScoreLogBridgeBuilder::new()
.context("NEW_CONTEXT")
.show_module(true)
.show_file(true)
Expand Down
67 changes: 67 additions & 0 deletions score/mw/log/rust/score_log_bridge_cpp_init/BUILD
Original file line number Diff line number Diff line change
@@ -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
# *******************************************************************************

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 = ["ffi.rs"],
edition = "2021",
visibility = ["//visibility:private"],
deps = [
"//score/mw/log/rust/score_log_bridge",
],
)

cc_library(
name = "score_log_bridge_cpp_init",
srcs = ["score_log_bridge_init.cpp"],
hdrs = ["score_log_bridge_init.h"],
include_prefix = "score/mw/log/rust",
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",
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_binary(
name = "example",
srcs = ["examples/main.cpp"],
linkopts = select({
"@platforms//os:qnx": [
"-lsocket",
],
"//conditions:default": [],
}),
visibility = ["//visibility:public"],
deps = [
":example_lib",
":score_log_bridge_cpp_init",
],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
//
// 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
//

//! 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::ScoreLogBridgeBuilder;

/// Show example logs.
#[no_mangle]
extern "C" fn show_logs() {
// 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,
);

// Use logger instance with modified context.
let logger = ScoreLogBridgeBuilder::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");
}
30 changes: 30 additions & 0 deletions score/mw/log/rust/score_log_bridge_cpp_init/examples/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/********************************************************************************
* 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/mw/log/rust/score_log_bridge_init.h"

extern "C" {
void show_logs();
}

int main()
{
using namespace score::mw::log::rust;

ScoreLogBridgeBuilder builder;
builder.Context("ABCD").ShowModule(true).ShowFile(true).ShowLine(true).SetAsDefaultLogger();

show_logs();

return 0;
}
Loading
Loading