Skip to content

Commit

Permalink
Added logger initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
rm-dr committed Feb 24, 2024
1 parent 53daa91 commit 3536977
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 33 deletions.
111 changes: 111 additions & 0 deletions src/bin/tectonic/log.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
use core::fmt;

use nu_ansi_term::{Color, Style};
use tracing::{
field::{Field, Visit},
Level, Subscriber,
};
use tracing_subscriber::{
fmt::{format, FormatEvent, FormatFields},
registry::LookupSpan,
};

/// Utility struct, used to read log data
#[derive(Default)]
pub struct StringVisitor {
source: Option<String>,
message: String,
}

impl Visit for StringVisitor {
fn record_debug(&mut self, field: &Field, value: &dyn fmt::Debug) {
match field.name() {
"message" => self.message = format!("{:?}", value),
_ => {}
}
}

fn record_str(&mut self, field: &Field, value: &str) {
match field.name() {
"tectonic_log_source" => {
self.source = Some(value.to_string());
}
_ => {}
}
}
}

/// Utility struct, formats log lines
pub struct LogFormatter {
/// If true, use ansi styling.
/// If false, use no styling.
with_ansi: bool,
}

impl LogFormatter {
pub fn new(with_ansi: bool) -> Self {
Self { with_ansi }
}
}

impl<S, N> FormatEvent<S, N> for LogFormatter
where
S: Subscriber + for<'a> LookupSpan<'a>,
N: for<'a> FormatFields<'a> + 'static,
{
fn format_event(
&self,
_ctx: &tracing_subscriber::fmt::FmtContext<'_, S, N>,
mut writer: format::Writer<'_>,
event: &tracing::Event<'_>,
) -> fmt::Result {
let m = event.metadata();

let level_str = match *m.level() {
Level::TRACE => "TRACE",
Level::DEBUG => "DEBUG",
Level::INFO => "INFO",
Level::WARN => "WARN",
Level::ERROR => "ERROR",
};

// Write log level
if self.with_ansi {
let level_sty = Style::new();
let level_sty = match *m.level() {
Level::TRACE => level_sty.fg(Color::White),
Level::DEBUG => level_sty.fg(Color::Blue),
Level::INFO => level_sty.fg(Color::Green),
Level::WARN => level_sty.fg(Color::Yellow),
Level::ERROR => level_sty.fg(Color::Red),
};
// Pad BEFORE painting so ansi chars don't mess with padding.
write!(writer, "{} ", level_sty.paint(format!("{:<5}", level_str)))?;
} else {
write!(writer, "{:<5} ", level_str)?;
}

// Get log content
let mut sv = StringVisitor::default();
event.record(&mut sv);

// Write log source and message
if self.with_ansi {
write!(
writer,
"{} {}",
Color::DarkGray.paint(format!("{:<8}", sv.source.unwrap_or("".to_string()))),
sv.message
)?;
} else {
write!(
writer,
"{:<8} {}",
sv.source.unwrap_or("".to_string()),
sv.message
)?;
}

writeln!(writer)
}
}
21 changes: 7 additions & 14 deletions src/bin/tectonic/main.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
// src/bin/tectonic/main.rs -- Command-line driver for the Tectonic engine.
// Copyright 2016-2023 the Tectonic Project
// Licensed under the MIT License.
use nu_ansi_term::{Color, Style};
use std::{env, fmt::Debug, process};
use structopt::StructOpt;
use tracing_subscriber::{
self,
fmt::{format, FormatEvent, FormatFields},
registry::LookupSpan,
};
use tracing::error;

use tectonic::{config::PersistentConfig, unstable_opts};

mod compile;
mod log;
mod watch;

#[cfg(feature = "serialization")]
Expand All @@ -39,14 +35,6 @@ struct CliOptions {
#[structopt(short = "X")]
use_v2: bool,

/// How much chatter to print when running
#[structopt(long = "chatter", short, name = "level", default_value = "default", possible_values(&["default", "minimal"]))]
chatter_level: String,

/// Enable/disable colorful log output
#[structopt(long = "color", name = "when", default_value = "auto", possible_values(&["always", "auto", "never"]))]
cli_color: String,

/// Use this URL to find resource files instead of the default
#[structopt(takes_value(true), long, short, name = "url", overrides_with = "url")]
// TODO add URL validation
Expand All @@ -68,6 +56,11 @@ struct PeekUnstableOptions {
fn main() {
let os_args: Vec<_> = env::args_os().collect();

// Set up logger
tracing_subscriber::fmt()
.event_format(log::LogFormatter::new(true))
.init();

// A hack so that you can just run `tectonic -Z help` without getting a
// usage error about a missing input file specification. If
// `from_unstable_args()` sees a `help` option, it will print the usage and
Expand Down
19 changes: 0 additions & 19 deletions src/bin/tectonic/v2cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,25 +41,6 @@ use watchexec_signals::Signal;
setting(AppSettings::NoBinaryName)
)]
struct V2CliOptions {
/// How much chatter to print when running
#[structopt(
long = "chatter",
short,
name = "level",
default_value = "default",
possible_values(&["default", "minimal"])
)]
chatter_level: String,

/// Control colorization of output
#[structopt(
long = "color",
name = "when",
default_value = "auto",
possible_values(&["always", "auto", "never"])
)]
cli_color: String,

/// Use this URL to find resource files instead of the default
#[structopt(
takes_value(true),
Expand Down

0 comments on commit 3536977

Please sign in to comment.