mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-28 12:55:05 +00:00
Move error and warning messages into log macro (#2669)
This commit is contained in:
parent
75fad989f4
commit
81abc5d7d8
5 changed files with 61 additions and 56 deletions
|
@ -5,7 +5,9 @@ use std::path::PathBuf;
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use configparser::ini::Ini;
|
use configparser::ini::Ini;
|
||||||
|
|
||||||
use ruff::flake8_to_ruff::{self, ExternalConfig};
|
use ruff::flake8_to_ruff::{self, ExternalConfig};
|
||||||
|
use ruff::logging::{set_up_logging, LogLevel};
|
||||||
|
|
||||||
#[derive(Parser)]
|
#[derive(Parser)]
|
||||||
#[command(
|
#[command(
|
||||||
|
@ -27,6 +29,8 @@ struct Args {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() -> Result<()> {
|
fn main() -> Result<()> {
|
||||||
|
set_up_logging(&LogLevel::Default)?;
|
||||||
|
|
||||||
let args = Args::parse();
|
let args = Args::parse();
|
||||||
|
|
||||||
// Read the INI file.
|
// Read the INI file.
|
||||||
|
|
|
@ -3,6 +3,7 @@ use std::path::Path;
|
||||||
|
|
||||||
use anyhow::{anyhow, Result};
|
use anyhow::{anyhow, Result};
|
||||||
use colored::Colorize;
|
use colored::Colorize;
|
||||||
|
use log::error;
|
||||||
use rustpython_parser::error::ParseError;
|
use rustpython_parser::error::ParseError;
|
||||||
use rustpython_parser::lexer::LexResult;
|
use rustpython_parser::lexer::LexResult;
|
||||||
|
|
||||||
|
@ -247,17 +248,12 @@ pub fn add_noqa_to_path(path: &Path, settings: &Settings) -> Result<usize> {
|
||||||
|
|
||||||
// Log any parse errors.
|
// Log any parse errors.
|
||||||
if let Some(err) = error {
|
if let Some(err) = error {
|
||||||
#[allow(clippy::print_stderr)]
|
error!(
|
||||||
{
|
"{}{}{} {err:?}",
|
||||||
eprintln!(
|
"Failed to parse ".bold(),
|
||||||
"{}{} {}{}{} {err:?}",
|
fs::relativize_path(path).bold(),
|
||||||
"error".red().bold(),
|
":".bold()
|
||||||
":".bold(),
|
);
|
||||||
"Failed to parse ".bold(),
|
|
||||||
fs::relativize_path(path).bold(),
|
|
||||||
":".bold()
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add any missing `# noqa` pragmas.
|
// Add any missing `# noqa` pragmas.
|
||||||
|
|
|
@ -1,19 +1,18 @@
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
|
use colored::Colorize;
|
||||||
use fern;
|
use fern;
|
||||||
|
use log::Level;
|
||||||
|
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! warn_user_once {
|
macro_rules! warn_user_once {
|
||||||
($($arg:tt)*) => {
|
($($arg:tt)*) => {
|
||||||
use colored::Colorize;
|
use colored::Colorize;
|
||||||
|
use log::warn;
|
||||||
|
|
||||||
static WARNED: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);
|
static WARNED: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);
|
||||||
if !WARNED.swap(true, std::sync::atomic::Ordering::SeqCst) {
|
if !WARNED.swap(true, std::sync::atomic::Ordering::SeqCst) {
|
||||||
let message = format!("{}", format_args!($($arg)*));
|
let message = format!("{}", format_args!($($arg)*));
|
||||||
eprintln!(
|
warn!("{}", message.bold());
|
||||||
"{}{} {}",
|
|
||||||
"warning".yellow().bold(),
|
|
||||||
":".bold(),
|
|
||||||
message.bold(),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -22,13 +21,10 @@ macro_rules! warn_user_once {
|
||||||
macro_rules! warn_user {
|
macro_rules! warn_user {
|
||||||
($($arg:tt)*) => {
|
($($arg:tt)*) => {
|
||||||
use colored::Colorize;
|
use colored::Colorize;
|
||||||
|
use log::warn;
|
||||||
|
|
||||||
let message = format!("{}", format_args!($($arg)*));
|
let message = format!("{}", format_args!($($arg)*));
|
||||||
eprintln!(
|
warn!("{}", message.bold());
|
||||||
"{}{} {}",
|
|
||||||
"warning".yellow().bold(),
|
|
||||||
":".bold(),
|
|
||||||
message.bold(),
|
|
||||||
);
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -74,14 +70,32 @@ impl LogLevel {
|
||||||
|
|
||||||
pub fn set_up_logging(level: &LogLevel) -> Result<()> {
|
pub fn set_up_logging(level: &LogLevel) -> Result<()> {
|
||||||
fern::Dispatch::new()
|
fern::Dispatch::new()
|
||||||
.format(|out, message, record| {
|
.format(|out, message, record| match record.level() {
|
||||||
out.finish(format_args!(
|
Level::Error => {
|
||||||
"{}[{}][{}] {}",
|
out.finish(format_args!(
|
||||||
chrono::Local::now().format("[%Y-%m-%d][%H:%M:%S]"),
|
"{}{} {}",
|
||||||
record.target(),
|
"error".red().bold(),
|
||||||
record.level(),
|
":".bold(),
|
||||||
message
|
message
|
||||||
));
|
));
|
||||||
|
}
|
||||||
|
Level::Warn => {
|
||||||
|
out.finish(format_args!(
|
||||||
|
"{}{} {}",
|
||||||
|
"warning".yellow().bold(),
|
||||||
|
":".bold(),
|
||||||
|
message
|
||||||
|
));
|
||||||
|
}
|
||||||
|
Level::Info | Level::Debug | Level::Trace => {
|
||||||
|
out.finish(format_args!(
|
||||||
|
"{}[{}][{}] {}",
|
||||||
|
chrono::Local::now().format("[%Y-%m-%d][%H:%M:%S]"),
|
||||||
|
record.target(),
|
||||||
|
record.level(),
|
||||||
|
message
|
||||||
|
));
|
||||||
|
}
|
||||||
})
|
})
|
||||||
.level(level.level_filter())
|
.level(level.level_filter())
|
||||||
.level_for("globset", log::LevelFilter::Warn)
|
.level_for("globset", log::LevelFilter::Warn)
|
||||||
|
|
|
@ -7,7 +7,7 @@ use std::path::Path;
|
||||||
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use colored::Colorize;
|
use colored::Colorize;
|
||||||
use log::debug;
|
use log::{debug, error};
|
||||||
use ruff::linter::{lint_fix, lint_only, LinterResult};
|
use ruff::linter::{lint_fix, lint_only, LinterResult};
|
||||||
use ruff::message::Message;
|
use ruff::message::Message;
|
||||||
use ruff::settings::{flags, AllSettings, Settings};
|
use ruff::settings::{flags, AllSettings, Settings};
|
||||||
|
@ -105,17 +105,12 @@ pub fn lint_path(
|
||||||
|
|
||||||
if let Some(err) = parse_error {
|
if let Some(err) = parse_error {
|
||||||
// Notify the user of any parse errors.
|
// Notify the user of any parse errors.
|
||||||
#[allow(clippy::print_stderr)]
|
error!(
|
||||||
{
|
"{}{}{} {err}",
|
||||||
eprintln!(
|
"Failed to parse ".bold(),
|
||||||
"{}{} {}{}{} {err}",
|
fs::relativize_path(path).bold(),
|
||||||
"error".red().bold(),
|
":".bold()
|
||||||
":".bold(),
|
);
|
||||||
"Failed to parse ".bold(),
|
|
||||||
fs::relativize_path(path).bold(),
|
|
||||||
":".bold()
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Purge the cache.
|
// Purge the cache.
|
||||||
cache::del(path, package.as_ref(), settings, autofix.into());
|
cache::del(path, package.as_ref(), settings, autofix.into());
|
||||||
|
@ -210,15 +205,10 @@ pub fn lint_stdin(
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Some(err) = parse_error {
|
if let Some(err) = parse_error {
|
||||||
#[allow(clippy::print_stderr)]
|
error!(
|
||||||
{
|
"Failed to parse {}: {err}",
|
||||||
eprintln!(
|
path.map_or_else(|| "-".into(), fs::relativize_path).bold()
|
||||||
"{}{} Failed to parse {}: {err}",
|
);
|
||||||
"error".red().bold(),
|
|
||||||
":".bold(),
|
|
||||||
path.map_or_else(|| "-".into(), fs::relativize_path).bold()
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(Diagnostics { messages, fixed })
|
Ok(Diagnostics { messages, fixed })
|
||||||
|
|
|
@ -3,16 +3,17 @@ use std::path::PathBuf;
|
||||||
use std::process::ExitCode;
|
use std::process::ExitCode;
|
||||||
use std::sync::mpsc::channel;
|
use std::sync::mpsc::channel;
|
||||||
|
|
||||||
|
use anyhow::Result;
|
||||||
|
use clap::{CommandFactory, Parser, Subcommand};
|
||||||
|
use colored::Colorize;
|
||||||
|
use notify::{recommended_watcher, RecursiveMode, Watcher};
|
||||||
|
|
||||||
use ::ruff::logging::{set_up_logging, LogLevel};
|
use ::ruff::logging::{set_up_logging, LogLevel};
|
||||||
use ::ruff::resolver::PyprojectDiscovery;
|
use ::ruff::resolver::PyprojectDiscovery;
|
||||||
use ::ruff::settings::types::SerializationFormat;
|
use ::ruff::settings::types::SerializationFormat;
|
||||||
use ::ruff::settings::CliSettings;
|
use ::ruff::settings::CliSettings;
|
||||||
use ::ruff::{fix, fs, warn_user_once};
|
use ::ruff::{fix, fs, warn_user_once};
|
||||||
use anyhow::Result;
|
|
||||||
use args::{Args, CheckArgs, Command};
|
use args::{Args, CheckArgs, Command};
|
||||||
use clap::{CommandFactory, Parser, Subcommand};
|
|
||||||
use colored::Colorize;
|
|
||||||
use notify::{recommended_watcher, RecursiveMode, Watcher};
|
|
||||||
use printer::{Printer, Violations};
|
use printer::{Printer, Violations};
|
||||||
|
|
||||||
pub(crate) mod args;
|
pub(crate) mod args;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue