Skip to content

Commit

Permalink
Merge pull request #44 from jonathanmorley/remove-newlines
Browse files Browse the repository at this point in the history
remove newlines from non-line-indented output
  • Loading branch information
oli-obk committed May 23, 2023
2 parents e9dfaa7 + 396a36b commit 483cc0a
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 1 deletion.
79 changes: 79 additions & 0 deletions examples/no-indent.rs
@@ -0,0 +1,79 @@
use tracing::{debug, error, info, instrument, span, warn, Level};
use tracing_subscriber::{layer::SubscriberExt, registry::Registry};
use tracing_tree::HierarchicalLayer;

fn main() {
let layer = HierarchicalLayer::default()
.with_writer(std::io::stdout)
.with_indent_amount(2)
.with_thread_names(true)
.with_thread_ids(true)
.with_verbose_exit(false)
.with_verbose_entry(false)
.with_targets(true);

let subscriber = Registry::default().with(layer);
tracing::subscriber::set_global_default(subscriber).unwrap();

let app_span = span!(Level::TRACE, "hierarchical-example", version = %0.1);
let _e = app_span.enter();

let server_span = span!(Level::TRACE, "server", host = "localhost", port = 8080);
let _e2 = server_span.enter();
info!("starting");
std::thread::sleep(std::time::Duration::from_millis(300));
info!("listening");
let peer1 = span!(Level::TRACE, "conn", peer_addr = "82.9.9.9", port = 42381);
peer1.in_scope(|| {
debug!("connected");
std::thread::sleep(std::time::Duration::from_millis(300));
debug!(length = 2, "message received");
});
drop(peer1);
let peer2 = span!(Level::TRACE, "conn", peer_addr = "8.8.8.8", port = 18230);
peer2.in_scope(|| {
std::thread::sleep(std::time::Duration::from_millis(300));
debug!("connected");
});
drop(peer2);
let peer3 = span!(
Level::TRACE,
"foomp",
normal_var = 43,
"{} <- format string",
42
);
peer3.in_scope(|| {
error!("hello");
});
drop(peer3);
let peer1 = span!(Level::TRACE, "conn", peer_addr = "82.9.9.9", port = 42381);
peer1.in_scope(|| {
warn!(algo = "xor", "weak encryption requested");
std::thread::sleep(std::time::Duration::from_millis(300));
debug!(length = 8, "response sent");
debug!("disconnected");
});
drop(peer1);
let peer2 = span!(Level::TRACE, "conn", peer_addr = "8.8.8.8", port = 18230);
peer2.in_scope(|| {
debug!(length = 5, "message received");
std::thread::sleep(std::time::Duration::from_millis(300));
debug!(length = 8, "response sent");
debug!("disconnected");
});
drop(peer2);
warn!("internal error");
info!("exit");
}

#[instrument]
fn call_a(name: &str) {
info!(name, "got a name");
call_b(name)
}

#[instrument]
fn call_b(name: &str) {
info!(name, "got a name");
}
21 changes: 21 additions & 0 deletions examples/no-indent.stdout
@@ -0,0 +1,21 @@
1:main no_indent::hierarchical-example version=0.1
1:main no_indent::server host="localhost", port=8080
1:main Xms INFO no_indent starting
1:main Xms INFO no_indent listening
1:main no_indent::conn peer_addr="82.9.9.9", port=42381
1:main Xms DEBUG no_indent connected
1:main Xms DEBUG no_indent message received, length=2
1:main no_indent::conn peer_addr="8.8.8.8", port=18230
1:main Xms DEBUG no_indent connected
1:main no_indent::foomp 42 <- format string, normal_var=43
1:main Xms ERROR no_indent hello
1:main no_indent::conn peer_addr="82.9.9.9", port=42381
1:main Xms WARN no_indent weak encryption requested, algo="xor"
1:main Xms DEBUG no_indent response sent, length=8
1:main Xms DEBUG no_indent disconnected
1:main no_indent::conn peer_addr="8.8.8.8", port=18230
1:main Xms DEBUG no_indent message received, length=5
1:main Xms DEBUG no_indent response sent, length=8
1:main Xms DEBUG no_indent disconnected
1:main Xs WARN no_indent internal error
1:main Xs INFO no_indent exit
4 changes: 3 additions & 1 deletion src/format.rs
Expand Up @@ -167,11 +167,12 @@ impl Buffers {
}

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

// Render something when wraparound occurs so the user is aware of it
if config.indent_lines {
self.current_buf.push('\n');

match style {
SpanMode::Close { .. } | SpanMode::PostClose => {
if indent > 0 && (indent + 1) % config.wraparound == 0 {
Expand Down Expand Up @@ -417,6 +418,7 @@ fn indent_block(
let indent_str = String::from(" ").repeat(indent_spaces);
for line in lines {
buf.push_str(prefix);
buf.push(' ');
buf.push_str(&indent_str);
buf.push_str(line);
buf.push('\n');
Expand Down

0 comments on commit 483cc0a

Please sign in to comment.