Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sort IDs of log msgs by severity to allow runtime filtering #843

Merged
merged 3 commits into from
Jun 10, 2024
Merged
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

- [#847]: `decoder`: Fix log format width specifier not working as expected
- [#845]: `decoder`: fix println!() records being printed with formatting
- [#843]: `defmt`: Sort IDs of log msgs by severity to allow runtime filtering by severity

[#847]: https://github.com/knurling-rs/defmt/pull/847
[#845]: https://github.com/knurling-rs/defmt/pull/845
[#843]: https://github.com/knurling-rs/defmt/pull/843

## [v0.3.8] - 2024-05-17

Expand Down
17 changes: 17 additions & 0 deletions defmt/defmt.x.in
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,23 @@ SECTIONS
/* Format implementations for primitives like u8 */
*(.defmt.prim.*);

/* We order the ids of the log messages by severity and put markers in between, so that we can filter logs at runtime by severity */
__DEFMT_MARKER_TRACE_START = .;
*(.defmt.trace.*);
__DEFMT_MARKER_TRACE_END = .;
__DEFMT_MARKER_DEBUG_START = .;
*(.defmt.debug.*);
__DEFMT_MARKER_DEBUG_END = .;
__DEFMT_MARKER_INFO_START = .;
*(.defmt.info.*);
__DEFMT_MARKER_INFO_END = .;
__DEFMT_MARKER_WARN_START = .;
*(.defmt.warn.*);
__DEFMT_MARKER_WARN_END = .;
__DEFMT_MARKER_ERROR_START = .;
*(.defmt.error.*);
__DEFMT_MARKER_ERROR_END = .;

/* Everything user-defined */
*(.defmt.*);

Expand Down
46 changes: 46 additions & 0 deletions defmt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,3 +401,49 @@ pub fn flush() {
}
}
}

#[cfg(not(feature = "unstable-test"))]
#[doc(hidden)]
pub struct IdRanges {
pub trace: core::ops::Range<u16>,
pub debug: core::ops::Range<u16>,
pub info: core::ops::Range<u16>,
pub warn: core::ops::Range<u16>,
pub error: core::ops::Range<u16>,
}

#[cfg(not(feature = "unstable-test"))]
impl IdRanges {
pub fn get() -> Self {
extern "C" {
static __DEFMT_MARKER_TRACE_START: u8;
static __DEFMT_MARKER_TRACE_END: u8;
static __DEFMT_MARKER_DEBUG_START: u8;
static __DEFMT_MARKER_DEBUG_END: u8;
static __DEFMT_MARKER_INFO_START: u8;
static __DEFMT_MARKER_INFO_END: u8;
static __DEFMT_MARKER_WARN_START: u8;
static __DEFMT_MARKER_WARN_END: u8;
static __DEFMT_MARKER_ERROR_START: u8;
static __DEFMT_MARKER_ERROR_END: u8;
}

let trace_start = unsafe { &__DEFMT_MARKER_TRACE_START as *const u8 as u16 };
let trace_end = unsafe { &__DEFMT_MARKER_TRACE_END as *const u8 as u16 };
let debug_start = unsafe { &__DEFMT_MARKER_DEBUG_START as *const u8 as u16 };
let debug_end = unsafe { &__DEFMT_MARKER_DEBUG_END as *const u8 as u16 };
let info_start = unsafe { &__DEFMT_MARKER_INFO_START as *const u8 as u16 };
let info_end = unsafe { &__DEFMT_MARKER_INFO_END as *const u8 as u16 };
let warn_start = unsafe { &__DEFMT_MARKER_WARN_START as *const u8 as u16 };
let warn_end = unsafe { &__DEFMT_MARKER_WARN_END as *const u8 as u16 };
let error_start = unsafe { &__DEFMT_MARKER_ERROR_START as *const u8 as u16 };
let error_end = unsafe { &__DEFMT_MARKER_ERROR_END as *const u8 as u16 };
Self {
trace: trace_start..trace_end,
debug: debug_start..debug_end,
info: info_start..info_end,
warn: warn_start..warn_end,
error: error_start..error_end,
}
}
}
20 changes: 15 additions & 5 deletions macros/src/construct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ pub(crate) fn escaped_expr_string(expr: &Expr) -> String {
.replace('}', "}}")
}

pub(crate) fn interned_string(string: &str, tag: &str, is_log_statement: bool) -> TokenStream2 {
pub(crate) fn interned_string(
string: &str,
tag: &str,
is_log_statement: bool,
prefix: Option<&str>,
) -> TokenStream2 {
// NOTE we rely on this variable name when extracting file location information from the DWARF
// without it we have no other mean to differentiate static variables produced by `info!` vs
// produced by `intern!` (or `internp`)
Expand All @@ -39,7 +44,7 @@ pub(crate) fn interned_string(string: &str, tag: &str, is_log_statement: bool) -
let var_addr = if cfg!(feature = "unstable-test") {
quote!({ defmt::export::fetch_add_string_index() })
} else {
let var_item = static_variable(&var_name, string, tag);
let var_item = static_variable(&var_name, string, tag, prefix);
quote!({
#var_item
&#var_name as *const u8 as u16
Expand Down Expand Up @@ -69,10 +74,15 @@ pub(crate) fn linker_section(for_macos: bool, prefix: Option<&str>, symbol: &str
format!(".defmt{sub_section}")
}

pub(crate) fn static_variable(name: &Ident2, data: &str, tag: &str) -> TokenStream2 {
pub(crate) fn static_variable(
name: &Ident2,
data: &str,
tag: &str,
prefix: Option<&str>,
) -> TokenStream2 {
let sym_name = mangled_symbol_name(tag, data);
let section = linker_section(false, None, &sym_name);
let section_for_macos = linker_section(true, None, &sym_name);
let section = linker_section(false, prefix, &sym_name);
let section_for_macos = linker_section(true, prefix, &sym_name);

quote!(
#[cfg_attr(target_os = "macos", link_section = #section_for_macos)]
Expand Down
2 changes: 1 addition & 1 deletion macros/src/derives/format/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub(crate) fn encode_struct_data(ident: &Ident, data: &DataStruct) -> syn::Resul
}
}));

let format_tag = construct::interned_string(&format_string, "derived", false);
let format_tag = construct::interned_string(&format_string, "derived", false, None);
Ok(EncodeData {
format_tag,
stmts,
Expand Down
4 changes: 2 additions & 2 deletions macros/src/derives/format/codegen/enum_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub(crate) fn encode(ident: &Ident, data: &DataEnum) -> syn::Result<EncodeData>
if data.variants.is_empty() {
return Ok(EncodeData {
stmts: vec![quote!(match *self {})],
format_tag: construct::interned_string("!", "derived", false),
format_tag: construct::interned_string("!", "derived", false, None),
where_predicates: vec![],
});
}
Expand Down Expand Up @@ -49,7 +49,7 @@ pub(crate) fn encode(ident: &Ident, data: &DataEnum) -> syn::Result<EncodeData>
))
}

let format_tag = construct::interned_string(&format_string, "derived", false);
let format_tag = construct::interned_string(&format_string, "derived", false, None);
let stmts = vec![quote!(match self {
#(#match_arms)*
})];
Expand Down
2 changes: 1 addition & 1 deletion macros/src/function_like/intern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ use crate::construct;

pub(crate) fn expand(args: TokenStream) -> TokenStream {
let literal = parse_macro_input!(args as LitStr);
construct::interned_string(&literal.value(), "str", false).into()
construct::interned_string(&literal.value(), "str", false, None).into()
}
3 changes: 2 additions & 1 deletion macros/src/function_like/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ pub(crate) fn expand_parsed(level: Level, args: Args) -> TokenStream2 {
args.format_string.span(),
);

let header = construct::interned_string(&format_string, level.as_str(), true);
let header =
construct::interned_string(&format_string, level.as_str(), true, Some(level.as_str()));
let env_filter = EnvFilter::from_env_var();

if let Some(filter_check) = env_filter.path_check(level) {
Expand Down
2 changes: 1 addition & 1 deletion macros/src/function_like/println.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub(crate) fn expand_parsed(args: Args) -> TokenStream2 {
args.format_string.span(),
);

let header = construct::interned_string(&format_string, "println", true);
let header = construct::interned_string(&format_string, "println", true, None);
quote!({
match (#(&(#formatting_exprs)),*) {
(#(#patterns),*) => {
Expand Down
2 changes: 1 addition & 1 deletion macros/src/function_like/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub(crate) fn expand(args: TokenStream) -> TokenStream {
log_args.format_string.span(),
);

let format_tag = construct::interned_string(&format_string, "write", false);
let format_tag = construct::interned_string(&format_string, "write", false, None);
quote!({
let _typecheck_formatter: defmt::Formatter<'_> = #formatter;
match (#(&(#formatting_exprs)),*) {
Expand Down
2 changes: 1 addition & 1 deletion macros/src/items/bitflags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub(crate) fn expand(input: TokenStream) -> TokenStream {
construct::crate_local_disambiguator(),
cargo::crate_name(),
);
let format_tag = construct::interned_string(&format_string, "bitflags", false);
let format_tag = construct::interned_string(&format_string, "bitflags", false, None);

let ident = input.ident();
let ty = input.ty();
Expand Down
2 changes: 1 addition & 1 deletion macros/src/items/timestamp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub(crate) fn expand(args: TokenStream) -> TokenStream {
);

let var_name = format_ident!("S");
let var_item = construct::static_variable(&var_name, &format_string, "timestamp");
let var_item = construct::static_variable(&var_name, &format_string, "timestamp", None);

quote!(
const _: () = {
Expand Down
Loading