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

Trim the logs in the temporary logfile #2789

Open
wants to merge 2 commits into
base: testnet3
Choose a base branch
from
Open
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
22 changes: 22 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ version = "1.0"
version = "1.28"
features = [ "rt" ]

[dependencies.tracing-appender]
version = "0.2"

[dependencies.tracing-subscriber]
version = "0.3"
features = [ "env-filter" ]
Expand Down
5 changes: 3 additions & 2 deletions cli/src/commands/start.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,9 @@ pub struct Start {
impl Start {
/// Starts the snarkOS node.
pub fn parse(self) -> Result<String> {
// Initialize the logger.
let log_receiver = crate::helpers::initialize_logger(self.verbosity, self.nodisplay, self.logfile.clone());
// Initialize the logger; the log guard is moved into the async block.
let (log_receiver, _log_guard) =
crate::helpers::initialize_logger(self.verbosity, self.nodisplay, self.logfile.clone());
// Initialize the runtime.
Self::runtime().block_on(async move {
// Clone the configurations.
Expand Down
31 changes: 31 additions & 0 deletions cli/src/helpers/log_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,34 @@ impl io::Write for LogWriter {
Ok(())
}
}

fn strip_newlines(buf: &[u8]) -> Vec<u8> {
// Remove all newlines and then append one if the buffer ends with one;
// it should always be the case, but it's cheap to make extra sure.
buf.iter()
.copied()
.filter(|&b| b != b'\n')
.chain(if matches!(buf.last(), Some(b'\n')) { Some(b'\n') } else { None })
.collect()
}

pub struct WriterWrapper<W: io::Write>(pub W);

impl<W: io::Write> io::Write for WriterWrapper<W> {
/// Writes the given buffer into the log writer.
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
let sanitized = strip_newlines(buf);
// Force all the bytes to be written at once, otherwise
// buffer accounting could fail, resulting in random
// artifacts being written additionally.
self.0.write_all(&sanitized)?;
// Report the unsanitized size as the number of bytes
// written for the same reason as above.
Ok(buf.len())
}

/// Flushes the log writer (no-op).
fn flush(&mut self) -> io::Result<()> {
self.0.flush()
}
}
15 changes: 11 additions & 4 deletions cli/src/helpers/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::helpers::LogWriter;
use crate::helpers::{LogWriter, WriterWrapper};

use crossterm::tty::IsTty;
use std::{fs::File, io, path::Path};
use tokio::sync::mpsc;
use tracing_appender::non_blocking::{NonBlockingBuilder, WorkerGuard};
use tracing_subscriber::{
layer::{Layer, SubscriberExt},
util::SubscriberInitExt,
Expand All @@ -34,7 +35,11 @@ use tracing_subscriber::{
/// 5 => info, debug, trace, snarkos_node_router=trace
/// 6 => info, debug, trace, snarkos_node_tcp=trace
/// ```
pub fn initialize_logger<P: AsRef<Path>>(verbosity: u8, nodisplay: bool, logfile: P) -> mpsc::Receiver<Vec<u8>> {
pub fn initialize_logger<P: AsRef<Path>>(
verbosity: u8,
nodisplay: bool,
logfile: P,
) -> (mpsc::Receiver<Vec<u8>>, WorkerGuard) {
match verbosity {
0 => std::env::set_var("RUST_LOG", "info"),
1 => std::env::set_var("RUST_LOG", "debug"),
Expand Down Expand Up @@ -85,6 +90,7 @@ pub fn initialize_logger<P: AsRef<Path>>(verbosity: u8, nodisplay: bool, logfile
// Create a file to write logs to.
let logfile =
File::options().append(true).create(true).open(logfile).expect("Failed to open the file for writing logs");
let (file_writer, guard) = NonBlockingBuilder::default().buffered_lines_limit(256).finish(WriterWrapper(logfile));

// Initialize the log channel.
let (log_sender, log_receiver) = mpsc::channel(1024);
Expand All @@ -109,13 +115,14 @@ pub fn initialize_logger<P: AsRef<Path>>(verbosity: u8, nodisplay: bool, logfile
// Add layer redirecting logs to the file
tracing_subscriber::fmt::Layer::default()
.with_ansi(false)
.with_writer(logfile)
.with_writer(file_writer)
.with_target(verbosity > 2)
.with_filter(filter2),
)
.try_init();

log_receiver
// The file logger guard must exist as long as the node.
(log_receiver, guard)
}

/// Returns the welcome message as a string.
Expand Down