Ruby: write errors to json log

This commit is contained in:
Arthur Baars
2023-01-20 18:46:01 +01:00
parent d5e60dfb22
commit 2b9bc3c7e3
4 changed files with 201 additions and 161 deletions

View File

@@ -25,22 +25,19 @@ use tree_sitter::{Language, Parser, Range};
* of cores available on the machine to determine how many threads to use
* (minimum of 1). If unspecified, should be considered as set to -1."
*/
fn num_codeql_threads() -> usize {
fn num_codeql_threads() -> Result<usize, String> {
let threads_str = std::env::var("CODEQL_THREADS").unwrap_or_else(|_| "-1".to_owned());
match threads_str.parse::<i32>() {
Ok(num) if num <= 0 => {
let reduction = -num as usize;
std::cmp::max(1, num_cpus::get() - reduction)
Ok(std::cmp::max(1, num_cpus::get() - reduction))
}
Ok(num) => num as usize,
Ok(num) => Ok(num as usize),
Err(_) => {
tracing::error!(
"Unable to parse CODEQL_THREADS value '{}'; defaulting to 1 thread.",
&threads_str
);
1
}
Err(_) => Err(format!(
"Unable to parse CODEQL_THREADS value '{}'",
&threads_str
)),
}
}
@@ -60,7 +57,6 @@ fn encoding_from_name(encoding_name: &str) -> Option<&(dyn encoding::Encoding +
}
fn main() -> std::io::Result<()> {
let diagnostics = diagnostics::DiagnosticLoggers::new("ruby");
tracing_subscriber::fmt()
.with_target(false)
.without_time()
@@ -70,7 +66,21 @@ fn main() -> std::io::Result<()> {
.unwrap_or_else(|_| tracing_subscriber::EnvFilter::new("ruby_extractor=warn")),
)
.init();
let num_threads = num_codeql_threads();
let diagnostics = diagnostics::DiagnosticLoggers::new("ruby");
let logger = &mut diagnostics.logger();
let num_threads = match num_codeql_threads() {
Ok(num) => num,
Err(e) => {
logger.write(
&logger
.message("configuration-error", "Configuration error")
.text(&format!("{}; defaulting to 1 thread.", e))
.status_page()
.severity(diagnostics::Severity::Warning),
);
1
}
};
tracing::info!(
"Using {} {}",
num_threads,
@@ -80,6 +90,20 @@ fn main() -> std::io::Result<()> {
"threads"
}
);
let trap_compression = match trap::Compression::from_env("CODEQL_RUBY_TRAP_COMPRESSION") {
Ok(x) => x,
Err(e) => {
logger.write(
&logger
.message("configuration-error", "Configuration error")
.text(&format!("{}; using gzip.", e))
.status_page()
.severity(diagnostics::Severity::Warning),
);
trap::Compression::Gzip
}
};
rayon::ThreadPoolBuilder::new()
.num_threads(num_threads)
.build_global()
@@ -102,7 +126,6 @@ fn main() -> std::io::Result<()> {
.value_of("output-dir")
.expect("missing --output-dir");
let trap_dir = PathBuf::from(trap_dir);
let trap_compression = trap::Compression::from_env("CODEQL_RUBY_TRAP_COMPRESSION");
let file_list = matches.value_of("file-list").expect("missing --file-list");
let file_list = fs::File::open(file_list)?;
@@ -174,20 +197,35 @@ fn main() -> std::io::Result<()> {
Ok(str) => source = str.as_bytes().to_owned(),
Err(msg) => {
needs_conversion = false;
tracing::warn!(
"{}: character decoding failure: {} ({})",
&path.to_string_lossy(),
msg,
&encoding_name
diagnostics_writer.write(
&logger
.message(
"character-encoding-error",
"Character encoding error",
)
.text(&format!(
"{}: character decoding failure: {} ({})",
&path.to_string_lossy(),
msg,
&encoding_name
))
.status_page()
.severity(diagnostics::Severity::Warning),
);
}
}
}
} else {
tracing::warn!(
"{}: unknown character encoding: '{}'",
&path.to_string_lossy(),
&encoding_name
diagnostics_writer.write(
&logger
.message("character-encoding-error", "Character encoding error")
.text(&format!(
"{}: unknown character encoding: '{}'",
&path.to_string_lossy(),
&encoding_name
))
.status_page()
.severity(diagnostics::Severity::Warning),
);
}
}