Skip to content

Commit 486f04c

Browse files
committed
log: mw_log implementation
- Replacement for `log` crate. - Additional common types implementation. - Selectable safety level features. - Unit tests.
1 parent 2838088 commit 486f04c

File tree

26 files changed

+3015
-53
lines changed

26 files changed

+3015
-53
lines changed

.github/workflows/lint_clippy.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,4 @@ jobs:
5858
uses: actions-rs/cargo@v1
5959
with:
6060
command: clippy
61-
args: --all-features --all-targets --workspace -- -D warnings
61+
args: --all-targets --features qm --workspace -- -D warnings

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: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,18 @@ resolver = "2"
44
# Used when executing cargo from project root.
55
default-members = [
66
"src/containers",
7+
"src/log/mw_log",
78
"src/log/mw_log_fmt",
89
"src/log/mw_log_fmt_macro",
910
]
1011
# 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+
members = [
13+
"src/containers",
14+
"src/log/mw_log",
15+
"src/log/mw_log_fmt",
16+
"src/log/mw_log_fmt_macro",
17+
"examples/log_example",
18+
]
1219

1320

1421
[workspace.package]
@@ -19,6 +26,7 @@ authors = ["S-CORE Contributors"]
1926

2027

2128
[workspace.dependencies]
29+
mw_log = { path = "src/log/mw_log" }
2230
mw_log_fmt = { path = "src/log/mw_log_fmt" }
2331
mw_log_fmt_macro = { path = "src/log/mw_log_fmt_macro" }
2432

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: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[package]
2+
name = "log_example"
3+
version.workspace = true
4+
authors.workspace = true
5+
edition.workspace = true
6+
7+
8+
[dependencies]
9+
mw_log.workspace = true
10+
11+
12+
[lints]
13+
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::{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+
true
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_logger(&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+
}

src/log/BUILD

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+
load("@bazel_skylib//rules:common_settings.bzl", "string_flag")
15+
16+
string_flag(
17+
name = "safety_level",
18+
build_setting_default = "asil_b",
19+
values = [
20+
"asil_b",
21+
"qm",
22+
],
23+
visibility = [":__subpackages__"],
24+
)
25+
26+
config_setting(
27+
name = "safety_level_qm",
28+
flag_values = {":safety_level": "qm"},
29+
visibility = [":__subpackages__"],
30+
)
31+
32+
config_setting(
33+
name = "safety_level_asil_b",
34+
flag_values = {":safety_level": "asil_b"},
35+
visibility = [":__subpackages__"],
36+
)

src/log/mw_log/BUILD

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
load("@rules_rust//rust:defs.bzl", "rust_library", "rust_test")
14+
15+
rust_library(
16+
name = "mw_log",
17+
srcs = glob(["**/*.rs"]),
18+
proc_macro_deps = [
19+
"//src/log/mw_log_fmt_macro",
20+
],
21+
visibility = ["//visibility:public"],
22+
deps = [
23+
"//src/log/mw_log_fmt",
24+
],
25+
)
26+
27+
rust_test(
28+
name = "tests",
29+
crate = "mw_log",
30+
tags = [
31+
"unit_tests",
32+
"ut",
33+
],
34+
)

src/log/mw_log/Cargo.toml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
[package]
2+
name = "mw_log"
3+
version.workspace = true
4+
authors.workspace = true
5+
readme.workspace = true
6+
edition.workspace = true
7+
8+
9+
[lib]
10+
path = "lib.rs"
11+
12+
13+
[features]
14+
max_level_off = []
15+
max_level_fatal = []
16+
max_level_error = []
17+
max_level_warn = []
18+
max_level_info = []
19+
max_level_debug = []
20+
max_level_trace = []
21+
22+
release_max_level_off = []
23+
release_max_level_fatal = []
24+
release_max_level_error = []
25+
release_max_level_warn = []
26+
release_max_level_info = []
27+
release_max_level_debug = []
28+
release_max_level_trace = []
29+
30+
31+
[dependencies]
32+
mw_log_fmt.workspace = true
33+
mw_log_fmt_macro.workspace = true
34+
35+
36+
[lints]
37+
workspace = true

0 commit comments

Comments
 (0)