Skip to content

Commit c6ba882

Browse files
committed
log: mw_log implementation
- Replacement for `log` crate. - Additional common types implementation. - Selectable safety level features (`asil_b`, `qm`). - Unit tests.
1 parent 2838088 commit c6ba882

File tree

27 files changed

+2155
-72
lines changed

27 files changed

+2155
-72
lines changed

Cargo.lock

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +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+
114
[workspace]
215
resolver = "2"
316
# Split to default members without tests and examples.
417
# Used when executing cargo from project root.
518
default-members = [
619
"src/containers",
20+
"src/log/mw_log",
721
"src/log/mw_log_fmt",
822
"src/log/mw_log_fmt_macro",
923
]
1024
# Include tests and examples as a member for IDE support and Bazel builds.
11-
members = ["src/containers", "src/log/mw_log_fmt", "src/log/mw_log_fmt_macro"]
12-
25+
members = [
26+
"src/containers",
27+
"src/log/mw_log",
28+
"src/log/mw_log_fmt",
29+
"src/log/mw_log_fmt_macro",
30+
"examples/log_example",
31+
]
1332

1433
[workspace.package]
1534
version = "0.0.1"
1635
edition = "2021"
1736
license-file = "LICENSE.md"
1837
authors = ["S-CORE Contributors"]
1938

20-
2139
[workspace.dependencies]
40+
mw_log = { path = "src/log/mw_log" }
2241
mw_log_fmt = { path = "src/log/mw_log_fmt" }
2342
mw_log_fmt_macro = { path = "src/log/mw_log_fmt_macro" }
2443

25-
2644
[workspace.lints.clippy]
2745
std_instead_of_core = "warn"
2846
alloc_instead_of_core = "warn"
2947

30-
3148
[workspace.lints.rust]
3249
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(loom)'] }
3350
missing_docs = "warn"

MODULE.bazel

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ bazel_dep(name = "score_rust_policies", version = "0.0.3")
3333

3434
bazel_dep(name = "score_process", version = "1.4.0", dev_dependency = True)
3535
bazel_dep(name = "score_platform", version = "0.5.1", dev_dependency = True) # This is main score repo
36-
bazel_dep(name = "score_virtualization", version = "0.0.1", dev_dependency = True)
36+
# bazel_dep(name = "score_virtualization", version = "0.0.1", dev_dependency = True)
3737

3838
# Toolchains and extensions
3939
bazel_dep(name = "score_toolchains_gcc", version = "0.5", dev_dependency = True)
@@ -57,11 +57,11 @@ git_override(
5757
remote = "https://github.com/eclipse-score/toolchains_rust.git",
5858
)
5959

60-
git_override(
61-
module_name = "score_virtualization",
62-
commit = "99d3f153c43796b67a63e82aad1ede6a881aa6af",
63-
remote = "https://github.com/qorix-group/score_virtualization.git",
64-
)
60+
# git_override(
61+
# module_name = "score_virtualization",
62+
# commit = "99d3f153c43796b67a63e82aad1ede6a881aa6af",
63+
# remote = "https://github.com/qorix-group/score_virtualization.git",
64+
# )
6565

6666
archive_override(
6767
module_name = "rust_qnx8_toolchain",

docs/baselibs_rust/log/architecture/_assets/interface.puml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,10 @@ package log <<component>> {
2121
}
2222

2323
class mw_log <<module>> {
24-
+set_logger(logger: &'static dyn Log) : Result<(), SetLoggerError>
25-
+set_boxed_logger(logger: Box<dyn Log>) : Result<(), SetLoggerError>
24+
+set_global_logger(logger: Box<dyn Log>) : Result<(), SetLoggerError>
2625
+set_max_level(level: LevelFilter) : ()
2726
+max_level() : LevelFilter
28-
+logger() : &'static dyn Log
27+
+global_logger() : &'static dyn Log
2928

3029
+log!(context: &str, level: Level, ...) : ()
3130
+log_enabled!(context: &str, level: Level) : bool

docs/baselibs_rust/log/detailed_design/_assets/class_diagram.puml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,10 @@ package "mw_log crate" {
6363
class mw_log <<module>> {
6464
{static} -LOGGER: &dyn Log
6565

66-
+set_logger(logger: &'static dyn Log) : Result<(), SetLoggerError>
67-
+set_boxed_logger(logger: Box<dyn Log>) : Result<(), SetLoggerError>
66+
+set_global_logger(logger: Box<dyn Log>) : Result<(), SetLoggerError>
6867
+set_max_level(level: LevelFilter) : ()
6968
+max_level() : LevelFilter
70-
+logger() : &'static dyn Log
69+
+global_logger() : &'static dyn Log
7170

7271
+log!(context: &str, level: Level, ...) : ()
7372
+log_enabled!(context: &str, level: Level) : bool
@@ -83,8 +82,8 @@ package "mw_log crate" {
8382
Functions are used for global logger configuration.
8483

8584
<code>
86-
let logger = &ExampleLogger;
87-
set_logger(logger).unwrap();
85+
let logger = Box::new(ExampleLogger);
86+
set_global_logger(logger).unwrap();
8887
set_max_level(LevelFilter::Fatal);
8988
</code>
9089

docs/baselibs_rust/log/detailed_design/_assets/log_op.puml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ end box
2323
actor -> mw_log : log!()
2424

2525
alt no-user-provided-logger
26-
mw_log -> mw_log : logger()
26+
mw_log -> mw_log : global_logger()
2727
end
2828

2929
alt no-user-provided-context

examples/log_example/BUILD

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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_rust//rust:defs.bzl", "rust_binary")
15+
16+
rust_binary(
17+
name = "log_example",
18+
srcs = glob(["src/**/*.rs"]),
19+
deps = [
20+
"//src/log/mw_log",
21+
],
22+
)

examples/log_example/Cargo.toml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
[package]
15+
name = "log_example"
16+
version.workspace = true
17+
authors.workspace = true
18+
edition.workspace = true
19+
20+
[dependencies]
21+
mw_log.workspace = true
22+
23+
[lints]
24+
workspace = true

examples/log_example/src/logger.rs

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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::fmt::Write;
15+
use mw_log::fmt::{write, Error, FormatSpec, Result as FmtResult, ScoreWrite};
16+
use mw_log::{max_level, Log, Metadata, Record};
17+
18+
struct StringWriter {
19+
buffer: String,
20+
}
21+
22+
impl StringWriter {
23+
pub fn new() -> Self {
24+
Self { buffer: String::new() }
25+
}
26+
27+
pub fn get(&self) -> &str {
28+
self.buffer.as_str()
29+
}
30+
}
31+
32+
impl ScoreWrite for StringWriter {
33+
fn write_bool(&mut self, v: &bool, _spec: &FormatSpec) -> FmtResult {
34+
write!(self.buffer, "{}", v).map_err(|_| Error)
35+
}
36+
37+
fn write_f32(&mut self, v: &f32, _spec: &FormatSpec) -> FmtResult {
38+
write!(self.buffer, "{}", v).map_err(|_| Error)
39+
}
40+
41+
fn write_f64(&mut self, v: &f64, _spec: &FormatSpec) -> FmtResult {
42+
write!(self.buffer, "{}", v).map_err(|_| Error)
43+
}
44+
45+
fn write_i8(&mut self, v: &i8, _spec: &FormatSpec) -> FmtResult {
46+
write!(self.buffer, "{}", v).map_err(|_| Error)
47+
}
48+
49+
fn write_i16(&mut self, v: &i16, _spec: &FormatSpec) -> FmtResult {
50+
write!(self.buffer, "{}", v).map_err(|_| Error)
51+
}
52+
53+
fn write_i32(&mut self, v: &i32, _spec: &FormatSpec) -> FmtResult {
54+
write!(self.buffer, "{}", v).map_err(|_| Error)
55+
}
56+
57+
fn write_i64(&mut self, v: &i64, _spec: &FormatSpec) -> FmtResult {
58+
write!(self.buffer, "{}", v).map_err(|_| Error)
59+
}
60+
61+
fn write_u8(&mut self, v: &u8, _spec: &FormatSpec) -> FmtResult {
62+
write!(self.buffer, "{}", v).map_err(|_| Error)
63+
}
64+
65+
fn write_u16(&mut self, v: &u16, _spec: &FormatSpec) -> FmtResult {
66+
write!(self.buffer, "{}", v).map_err(|_| Error)
67+
}
68+
69+
fn write_u32(&mut self, v: &u32, _spec: &FormatSpec) -> FmtResult {
70+
write!(self.buffer, "{}", v).map_err(|_| Error)
71+
}
72+
73+
fn write_u64(&mut self, v: &u64, _spec: &FormatSpec) -> FmtResult {
74+
write!(self.buffer, "{}", v).map_err(|_| Error)
75+
}
76+
77+
fn write_str(&mut self, v: &str, _spec: &FormatSpec) -> FmtResult {
78+
write!(self.buffer, "{}", v).map_err(|_| Error)
79+
}
80+
}
81+
82+
pub struct ExampleLogger;
83+
84+
impl Log for ExampleLogger {
85+
fn enabled(&self, metadata: &Metadata) -> bool {
86+
metadata.level() <= max_level()
87+
}
88+
89+
fn context(&self) -> &str {
90+
"EXAMPLE"
91+
}
92+
93+
fn log(&self, record: &Record) {
94+
if !self.enabled(record.metadata()) {
95+
return;
96+
}
97+
98+
// Create writer and write log data.
99+
let mut writer = StringWriter::new();
100+
let _ = write(&mut writer, *record.args());
101+
102+
// Show to stderr.
103+
eprintln!("{}", writer.get());
104+
}
105+
106+
fn flush(&self) {
107+
// No-op.
108+
}
109+
}

examples/log_example/src/main.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
//! Example app containing basic logger implementation.
15+
16+
mod logger;
17+
18+
use crate::logger::ExampleLogger;
19+
use mw_log::{debug, error, fatal, info, trace, warn, LevelFilter};
20+
21+
fn main() {
22+
// Initialize logger.
23+
mw_log::set_max_level(LevelFilter::Info);
24+
let result = mw_log::set_global_logger(Box::new(ExampleLogger));
25+
if result.is_err() {
26+
panic!("unable to set logger")
27+
}
28+
29+
// Example logs.
30+
trace!("trace log - hidden!");
31+
debug!("debug log - hidden!");
32+
info!("info log");
33+
warn!("warn log");
34+
error!("error log");
35+
fatal!("fatal log");
36+
}

0 commit comments

Comments
 (0)