cli: fix not being able to redirect traces to a file from inline query

This commit is contained in:
Jussi Saurio
2025-07-16 13:55:33 +03:00
parent 99d61aad3c
commit 2fbb21fc17
3 changed files with 13 additions and 15 deletions

View File

@@ -16,7 +16,7 @@ use comfy_table::{Attribute, Cell, CellAlignment, ContentArrangement, Row, Table
use rustyline::{error::ReadlineError, history::DefaultHistory, Editor}; use rustyline::{error::ReadlineError, history::DefaultHistory, Editor};
use std::{ use std::{
fmt, fmt,
io::{self, BufRead as _, Write}, io::{self, BufRead as _, IsTerminal, Write},
path::PathBuf, path::PathBuf,
sync::{ sync::{
atomic::{AtomicUsize, Ordering}, atomic::{AtomicUsize, Ordering},
@@ -24,7 +24,7 @@ use std::{
}, },
time::{Duration, Instant}, time::{Duration, Instant},
}; };
use tracing::level_filters::LevelFilter;
use tracing_appender::non_blocking::WorkerGuard; use tracing_appender::non_blocking::WorkerGuard;
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, EnvFilter}; use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, EnvFilter};
use turso_core::{Connection, Database, LimboError, OpenFlags, Statement, StepResult, Value}; use turso_core::{Connection, Database, LimboError, OpenFlags, Statement, StepResult, Value};
@@ -111,7 +111,7 @@ macro_rules! query_internal {
} }
impl Limbo { impl Limbo {
pub fn new() -> anyhow::Result<Self> { pub fn new() -> anyhow::Result<(Self, WorkerGuard)> {
let opts = Opts::parse(); let opts = Opts::parse();
let db_file = opts let db_file = opts
.database .database
@@ -164,8 +164,9 @@ impl Limbo {
rl: None, rl: None,
config: Some(config), config: Some(config),
}; };
let guard = app.init_tracing()?;
app.first_run(sql, quiet)?; app.first_run(sql, quiet)?;
Ok(app) Ok((app, guard))
} }
pub fn with_config(mut self, config: Config) -> Self { pub fn with_config(mut self, config: Config) -> Self {
@@ -891,7 +892,10 @@ impl Limbo {
false, false,
) )
} else { } else {
(tracing_appender::non_blocking(std::io::stderr()), true) (
tracing_appender::non_blocking(std::io::stderr()),
IsTerminal::is_terminal(&std::io::stderr()),
)
}; };
// Disable rustyline traces // Disable rustyline traces
if let Err(e) = tracing_subscriber::registry() if let Err(e) = tracing_subscriber::registry()
@@ -902,12 +906,7 @@ impl Limbo {
.with_thread_ids(true) .with_thread_ids(true)
.with_ansi(should_emit_ansi), .with_ansi(should_emit_ansi),
) )
.with( .with(EnvFilter::from_default_env().add_directive("rustyline=off".parse().unwrap()))
EnvFilter::builder()
.with_default_directive(LevelFilter::OFF.into())
.from_env_lossy()
.add_directive("rustyline=off".parse().unwrap()),
)
.try_init() .try_init()
{ {
println!("Unable to setup tracing appender: {e:?}"); println!("Unable to setup tracing appender: {e:?}");

View File

@@ -25,8 +25,8 @@ impl TerminalDetector {
impl TerminalDetector { impl TerminalDetector {
/// Detects terminal background using ANSI escape sequences on Unix systems /// Detects terminal background using ANSI escape sequences on Unix systems
pub fn detect_theme() -> TerminalTheme { pub fn detect_theme() -> TerminalTheme {
// Only works on interactive terminals // Only works on interactive terminals where both stdin and stdout are terminals
if !io::stdin().is_terminal() { if !io::stdin().is_terminal() || !io::stdout().is_terminal() {
return TerminalTheme::Unknown; // No colors for non-interactive return TerminalTheme::Unknown; // No colors for non-interactive
} }

View File

@@ -26,8 +26,7 @@ pub static HOME_DIR: LazyLock<PathBuf> =
pub static HISTORY_FILE: LazyLock<PathBuf> = LazyLock::new(|| HOME_DIR.join(".limbo_history")); pub static HISTORY_FILE: LazyLock<PathBuf> = LazyLock::new(|| HOME_DIR.join(".limbo_history"));
fn main() -> anyhow::Result<()> { fn main() -> anyhow::Result<()> {
let mut app = app::Limbo::new()?; let (mut app, _guard) = app::Limbo::new()?;
let _guard = app.init_tracing()?;
if std::io::IsTerminal::is_terminal(&std::io::stdin()) { if std::io::IsTerminal::is_terminal(&std::io::stdin()) {
let mut rl = Editor::with_config(rustyline_config())?; let mut rl = Editor::with_config(rustyline_config())?;