Skip to content

Commit a8c0d26

Browse files
author
Paolo Tranquilli
committed
Rust: fix qltest logging
Also added a new `logging_color` option to disable coloring (which does not look very good in files).
1 parent 3487226 commit a8c0d26

File tree

7 files changed

+78
-4
lines changed

7 files changed

+78
-4
lines changed

Cargo.lock

Lines changed: 20 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/codeql-extractor.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,9 @@ options:
7878
Collect flame graph data using the `tracing-flame` crate. To render a flame graph
7979
or chart, run the `inferno-flamegraph` command. See also: https://crates.io/crates/tracing-flame
8080
type: string
81+
color:
82+
title: Enable or disable colored output
83+
description: >
84+
Enable or disable colored output in the extractor log. Currently the default is to enable it.
85+
type: string
86+
pattern: "^(yes|no)$"

rust/extractor/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,4 @@ toml = "0.8.19"
3939
tracing = "0.1.41"
4040
tracing-flame = "0.2.0"
4141
tracing-subscriber = "0.3.19"
42+
is-terminal = "0.4.15"

rust/extractor/src/config.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,24 @@ pub enum Compression {
3131
Gzip,
3232
}
3333

34+
#[derive(Debug, PartialEq, Eq, Default, Serialize, Deserialize, Clone, Copy, clap::ValueEnum)]
35+
#[serde(rename_all = "lowercase")]
36+
#[clap(rename_all = "lowercase")]
37+
pub enum Color {
38+
#[default]
39+
Yes,
40+
No,
41+
}
42+
43+
impl Color {
44+
pub fn yes(&self) -> bool {
45+
match self {
46+
Color::Yes => true,
47+
Color::No => false,
48+
}
49+
}
50+
}
51+
3452
impl From<Compression> for trap::Compression {
3553
fn from(val: Compression) -> Self {
3654
match val {
@@ -55,6 +73,7 @@ pub struct Config {
5573
pub cargo_all_targets: bool,
5674
pub logging_flamegraph: Option<PathBuf>,
5775
pub logging_verbosity: Option<String>,
76+
pub logging_color: Color,
5877
pub compression: Compression,
5978
pub inputs: Vec<PathBuf>,
6079
pub qltest: bool,

rust/extractor/src/main.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use crate::rust_analyzer::path_to_file_id;
33
use crate::trap::TrapId;
44
use anyhow::Context;
55
use archive::Archiver;
6+
use codeql_extractor::extractor::Color;
67
use ra_ap_hir::Semantics;
78
use ra_ap_ide_db::line_index::{LineCol, LineIndex};
89
use ra_ap_ide_db::RootDatabase;
@@ -201,6 +202,7 @@ fn main() -> anyhow::Result<()> {
201202
.with(codeql_extractor::extractor::default_subscriber_with_level(
202203
"single_arch",
203204
&cfg.logging_verbosity,
205+
Color::from(cfg.logging_color.yes()),
204206
))
205207
.with(flame_layer)
206208
.init();

rust/tools/qltest.sh

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@ set -eu
44

55
export RUST_BACKTRACE=full
66
QLTEST_LOG="$CODEQL_EXTRACTOR_RUST_LOG_DIR"/qltest.log
7-
if ! "$CODEQL_EXTRACTOR_RUST_ROOT/tools/$CODEQL_PLATFORM/extractor" --qltest >> "$QLTEST_LOG" 2>&1; then
7+
EXTRACTOR_OPTS=(
8+
--qltest
9+
--logging-verbosity=progress+
10+
--logging-color=no
11+
)
12+
if ! "$CODEQL_EXTRACTOR_RUST_ROOT/tools/$CODEQL_PLATFORM/extractor" "${EXTRACTOR_OPTS[@]}" >> "$QLTEST_LOG" 2>&1; then
813
cat "$QLTEST_LOG"
914
exit 1
1015
fi

shared/tree-sitter-extractor/src/extractor/mod.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::diagnostics;
22
use crate::file_paths;
33
use crate::node_types::{self, EntryKind, Field, NodeTypeMap, Storage, TypeName};
44
use crate::trap;
5+
use serde::{Deserialize, Serialize};
56
use std::collections::BTreeMap as Map;
67
use std::collections::BTreeSet as Set;
78
use std::env;
@@ -18,13 +19,33 @@ use tree_sitter::{Language, Node, Parser, Range, Tree};
1819

1920
pub mod simple;
2021

22+
#[derive(Clone, Copy, PartialEq, Eq, Debug, Serialize, Deserialize)]
23+
pub enum Color {
24+
Yes,
25+
No,
26+
}
27+
28+
impl Color {
29+
pub fn from(condition: bool) -> Self {
30+
if condition {
31+
Color::Yes
32+
} else {
33+
Color::No
34+
}
35+
}
36+
}
37+
2138
/// Sets the tracing level based on the environment variables
2239
/// `RUST_LOG` and `CODEQL_VERBOSITY` (prioritized in that order),
2340
/// falling back to `warn` if neither is set.
2441
pub fn set_tracing_level(language: &str) {
2542
let verbosity = env::var("CODEQL_VERBOSITY").ok();
2643
tracing_subscriber::registry()
27-
.with(default_subscriber_with_level(language, &verbosity))
44+
.with(default_subscriber_with_level(
45+
language,
46+
&verbosity,
47+
Color::Yes,
48+
))
2849
.init();
2950
}
3051

@@ -33,6 +54,7 @@ pub fn set_tracing_level(language: &str) {
3354
pub fn default_subscriber_with_level(
3455
language: &str,
3556
verbosity: &Option<String>,
57+
color: Color,
3658
) -> Filtered<
3759
tracing_subscriber::fmt::Layer<
3860
tracing_subscriber::Registry,
@@ -46,6 +68,7 @@ pub fn default_subscriber_with_level(
4668
.with_target(false)
4769
.without_time()
4870
.with_level(true)
71+
.with_ansi(color == Color::Yes)
4972
.with_filter(
5073
tracing_subscriber::EnvFilter::try_from_default_env().unwrap_or_else(
5174
|_| -> tracing_subscriber::EnvFilter {

0 commit comments

Comments
 (0)