Skip to content

Commit

Permalink
Merge pull request #147 from knurling-rs/default-timestamp
Browse files Browse the repository at this point in the history
Default zero timestamp
  • Loading branch information
japaric authored Aug 28, 2020
2 parents 817da64 + d105b00 commit 34abded
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 44 deletions.
32 changes: 0 additions & 32 deletions book/src/setup-app.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,38 +47,6 @@ The following `global_logger`s are provided as part of the project:

Information about how to write a `global_logger` can be found in the [`#[global_logger]` section](./global-logger.md).

## `#[timestamp]`

The application must link to or define a `timestamp` function.
All logs are timestamped; this function specifies how the timestamp is computed.
The function must have signature `fn() -> u64`; the returned value is the timestamp in microseconds (`0` = program started).
The function should be implemented using a non-decreasing hardware counter but here are two pure-software implementations that can be used as a placeholder:

No timestamps:

``` rust
# extern crate defmt;
#[defmt::timestamp]
fn no_timestamp() -> u64 {
0
}
```

Virtual timestamps:

``` rust
# extern crate defmt;
# use std::sync::atomic::{AtomicUsize, Ordering};
// WARNING may overflow and wrap-around in long lived apps
#[defmt::timestamp]
fn virtual_timestamp() -> u64 {
static COUNT: AtomicUsize = AtomicUsize::new(0);
COUNT.fetch_add(1, Ordering::Relaxed) as u64
}
```

More information about how to write a `timestamp` function can be found in the [`#[timestamp]` section](./timestamp.md).

## Enabling logging

All logging is *disabled* by default.
Expand Down
9 changes: 5 additions & 4 deletions book/src/timestamp.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
# #[timestamp]

*Applications* that, directly or transitively, use any of `defmt` logging macros need to define a `#[timestamp]` function or include one in their dependency graph.
*Applications* that, directly or transitively, use any of `defmt` logging macros may define a `#[timestamp]` function or include one in their dependency graph.

All logs are timestamped.
The `#[timestamp]` function specifies how the timestamp is computed.
By default (if no `#[timestamp]` function is provided), a timestamp of 0 will be used.
This function must have signature `fn() -> u64` and on each invocation *should* return a non-decreasing value.
The function is not `unsafe` meaning that it must be thread-safe and interrupt-safe.

## No timestamp
## No timestamp (default behavior)

To omit timestamp information use this `#[timestamp]` function:
When no `#[timestamp]` function is used, defmt will use one equivalent to this:

``` rust
# extern crate defmt;
Expand All @@ -21,7 +22,7 @@ fn timestamp() -> u64 {

## Atomic timestamp

A simple `timestamp` function that does not depend on device specific features and it's good enough for development is shown below:
A simple `timestamp` function that does not depend on device specific features and is good enough for development is shown below:

``` rust
# extern crate defmt;
Expand Down
3 changes: 2 additions & 1 deletion defmt.x.in
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/* exhaustively search for these symbols */
EXTERN(_defmt_acquire);
EXTERN(_defmt_release);
EXTERN(_defmt_timestamp);
EXTERN(__defmt_default_timestamp);
PROVIDE(_defmt_timestamp = __defmt_default_timestamp);

SECTIONS
{
Expand Down
7 changes: 1 addition & 6 deletions firmware/nrf52/src/bin/panic.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
#![no_std]
#![no_main]

use defmt_rtt as _; // <- global logger
use cortex_m::asm;
use cortex_m_rt::entry;
use defmt_rtt as _; // <- global logger
use nrf52840_hal as _; // <- memory layout
use panic_probe as _; // <- panicking behavior

#[defmt::timestamp]
fn timestamp() -> u64 {
0
}

#[entry]
fn main() -> ! {
defmt::info!("main");
Expand Down
8 changes: 7 additions & 1 deletion firmware/qemu/src/bin/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,13 @@ fn main() -> ! {
y: T,
}

defmt::info!("{:?}", S { x: Some(&48), y: 49u8 });
defmt::info!(
"{:?}",
S {
x: Some(&48),
y: 49u8
}
);
}

// plain generic enum
Expand Down
5 changes: 5 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -437,3 +437,8 @@ pub trait Format {
/// Writes the defmt representation of `self` to `fmt`.
fn format(&self, fmt: &mut Formatter);
}

#[export_name = "__defmt_default_timestamp"]
fn default_timestamp() -> u64 {
0
}

0 comments on commit 34abded

Please sign in to comment.