Skip to content

Commit 483cc0a

Browse files
authored
Merge pull request #44 from jonathanmorley/remove-newlines
remove newlines from non-line-indented output
2 parents e9dfaa7 + 396a36b commit 483cc0a

File tree

3 files changed

+103
-1
lines changed

3 files changed

+103
-1
lines changed

examples/no-indent.rs

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
use tracing::{debug, error, info, instrument, span, warn, Level};
2+
use tracing_subscriber::{layer::SubscriberExt, registry::Registry};
3+
use tracing_tree::HierarchicalLayer;
4+
5+
fn main() {
6+
let layer = HierarchicalLayer::default()
7+
.with_writer(std::io::stdout)
8+
.with_indent_amount(2)
9+
.with_thread_names(true)
10+
.with_thread_ids(true)
11+
.with_verbose_exit(false)
12+
.with_verbose_entry(false)
13+
.with_targets(true);
14+
15+
let subscriber = Registry::default().with(layer);
16+
tracing::subscriber::set_global_default(subscriber).unwrap();
17+
18+
let app_span = span!(Level::TRACE, "hierarchical-example", version = %0.1);
19+
let _e = app_span.enter();
20+
21+
let server_span = span!(Level::TRACE, "server", host = "localhost", port = 8080);
22+
let _e2 = server_span.enter();
23+
info!("starting");
24+
std::thread::sleep(std::time::Duration::from_millis(300));
25+
info!("listening");
26+
let peer1 = span!(Level::TRACE, "conn", peer_addr = "82.9.9.9", port = 42381);
27+
peer1.in_scope(|| {
28+
debug!("connected");
29+
std::thread::sleep(std::time::Duration::from_millis(300));
30+
debug!(length = 2, "message received");
31+
});
32+
drop(peer1);
33+
let peer2 = span!(Level::TRACE, "conn", peer_addr = "8.8.8.8", port = 18230);
34+
peer2.in_scope(|| {
35+
std::thread::sleep(std::time::Duration::from_millis(300));
36+
debug!("connected");
37+
});
38+
drop(peer2);
39+
let peer3 = span!(
40+
Level::TRACE,
41+
"foomp",
42+
normal_var = 43,
43+
"{} <- format string",
44+
42
45+
);
46+
peer3.in_scope(|| {
47+
error!("hello");
48+
});
49+
drop(peer3);
50+
let peer1 = span!(Level::TRACE, "conn", peer_addr = "82.9.9.9", port = 42381);
51+
peer1.in_scope(|| {
52+
warn!(algo = "xor", "weak encryption requested");
53+
std::thread::sleep(std::time::Duration::from_millis(300));
54+
debug!(length = 8, "response sent");
55+
debug!("disconnected");
56+
});
57+
drop(peer1);
58+
let peer2 = span!(Level::TRACE, "conn", peer_addr = "8.8.8.8", port = 18230);
59+
peer2.in_scope(|| {
60+
debug!(length = 5, "message received");
61+
std::thread::sleep(std::time::Duration::from_millis(300));
62+
debug!(length = 8, "response sent");
63+
debug!("disconnected");
64+
});
65+
drop(peer2);
66+
warn!("internal error");
67+
info!("exit");
68+
}
69+
70+
#[instrument]
71+
fn call_a(name: &str) {
72+
info!(name, "got a name");
73+
call_b(name)
74+
}
75+
76+
#[instrument]
77+
fn call_b(name: &str) {
78+
info!(name, "got a name");
79+
}

examples/no-indent.stdout

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
1:main no_indent::hierarchical-example version=0.1
2+
1:main no_indent::server host="localhost", port=8080
3+
1:main Xms INFO no_indent starting
4+
1:main Xms INFO no_indent listening
5+
1:main no_indent::conn peer_addr="82.9.9.9", port=42381
6+
1:main Xms DEBUG no_indent connected
7+
1:main Xms DEBUG no_indent message received, length=2
8+
1:main no_indent::conn peer_addr="8.8.8.8", port=18230
9+
1:main Xms DEBUG no_indent connected
10+
1:main no_indent::foomp 42 <- format string, normal_var=43
11+
1:main Xms ERROR no_indent hello
12+
1:main no_indent::conn peer_addr="82.9.9.9", port=42381
13+
1:main Xms WARN no_indent weak encryption requested, algo="xor"
14+
1:main Xms DEBUG no_indent response sent, length=8
15+
1:main Xms DEBUG no_indent disconnected
16+
1:main no_indent::conn peer_addr="8.8.8.8", port=18230
17+
1:main Xms DEBUG no_indent message received, length=5
18+
1:main Xms DEBUG no_indent response sent, length=8
19+
1:main Xms DEBUG no_indent disconnected
20+
1:main Xs WARN no_indent internal error
21+
1:main Xs INFO no_indent exit

src/format.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -167,11 +167,12 @@ impl Buffers {
167167
}
168168

169169
pub(crate) fn indent_current(&mut self, indent: usize, config: &Config, style: SpanMode) {
170-
self.current_buf.push('\n');
171170
let prefix = config.prefix();
172171

173172
// Render something when wraparound occurs so the user is aware of it
174173
if config.indent_lines {
174+
self.current_buf.push('\n');
175+
175176
match style {
176177
SpanMode::Close { .. } | SpanMode::PostClose => {
177178
if indent > 0 && (indent + 1) % config.wraparound == 0 {
@@ -417,6 +418,7 @@ fn indent_block(
417418
let indent_str = String::from(" ").repeat(indent_spaces);
418419
for line in lines {
419420
buf.push_str(prefix);
421+
buf.push(' ');
420422
buf.push_str(&indent_str);
421423
buf.push_str(line);
422424
buf.push('\n');

0 commit comments

Comments
 (0)