11 releases

0.1.9 Dec 7, 2023
0.1.8 Aug 29, 2023
0.1.7 Jun 28, 2023
0.1.4 Mar 3, 2023
0.1.1 Jun 16, 2022

#290 in Development tools

Download history 277/week @ 2024-07-28 11/week @ 2024-08-04 14/week @ 2024-08-11 92/week @ 2024-08-18 67/week @ 2024-08-25 12/week @ 2024-09-01 9/week @ 2024-09-08 14/week @ 2024-09-15 19/week @ 2024-09-22 25/week @ 2024-09-29 371/week @ 2024-10-06 85/week @ 2024-10-13 34/week @ 2024-10-20 24/week @ 2024-10-27 33/week @ 2024-11-03 15/week @ 2024-11-10

133 downloads per month
Used in 6 crates (2 directly)

Apache-2.0…

16KB
266 lines

atomic-dbg

crates.io page docs.rs docs

This crate provides dbg, eprint, and eprintln, macros which work just like their counterparts in std, but which:

  • Write atomically, up to the greatest length supported on the platform.
  • Don't use locks (in userspace) or dynamic allocations.
  • Preserve libc's errno and Windows' last-error code value.

This means they can be used just about anywhere within a program, including inside allocator implementations, inside synchronization primitives, startup code, around FFI calls, inside signal handlers, and in the child process of a fork before an exec.

And, when multiple threads are printing, as long as they're within the length supported on the platform, the output is readable instead of potentially interleaved with other output.

For example, this code:

use atomic_dbg::dbg;

fn main() {
    dbg!(2, 3, 4);
}

Has this strace output:

write(2, "[examples/dbg.rs:4] 2 = 2\n[examples/dbg.rs:4] 3 = 3\n[examples/dbg.rs:4] 4 = 4\n", 78[examples/dbg.rs:4] 2 = 2

which is a single atomic write call.

For comparison, with std::dbg it looks like this:

write(2, "[", 1[)                        = 1
write(2, "examples/dbg.rs", 15examples/dbg.rs)         = 15
write(2, ":", 1:)                        = 1
write(2, "4", 14)                        = 1
write(2, "] ", 2] )                       = 2
write(2, "2", 12)                        = 1
write(2, " = ", 3 = )                      = 3
write(2, "2", 12)                        = 1
write(2, "\n", 1
)                       = 1
write(2, "[", 1[)                        = 1
write(2, "examples/dbg.rs", 15examples/dbg.rs)         = 15
write(2, ":", 1:)                        = 1
write(2, "4", 14)                        = 1
write(2, "] ", 2] )                       = 2
write(2, "3", 13)                        = 1
write(2, " = ", 3 = )                      = 3
write(2, "3", 13)                        = 1
write(2, "\n", 1
)                       = 1
write(2, "[", 1[)                        = 1
write(2, "examples/dbg.rs", 15examples/dbg.rs)         = 15
write(2, ":", 1:)                        = 1
write(2, "4", 14)                        = 1
write(2, "] ", 2] )                       = 2
write(2, "4", 14)                        = 1
write(2, " = ", 3 = )                      = 3
write(2, "4", 14)                        = 1
write(2, "\n", 1
)                       = 1

atomic-dbg is no_std, however like std, it uses the stderr file descriptor ambiently, assuming that it's open.

Logging

With the "log" feature enabled, atomic-dbg defines an atomic_dbg::log::init which installed a minimal logging implementation using the eprintln macro.

Dependencies

~1–11MB
~135K SLoC