Skip to content

Commit a523be4

Browse files
committed
Tree-sitter: Add set_tracing_level to shared extractor module
1 parent 3f66b63 commit a523be4

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

shared/tree-sitter-extractor/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ flate2 = "1.0"
99
globset = "0.4"
1010
tree-sitter = ">= 0.22.6"
1111
tracing = "0.1"
12+
tracing-subscriber = { version = "0.3.3", features = ["env-filter"] }
1213
rayon = "1.5.0"
1314
regex = "1.7.1"
1415
encoding = "0.2"

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,44 @@ use crate::node_types::{self, EntryKind, Field, NodeTypeMap, Storage, TypeName};
44
use crate::trap;
55
use std::collections::BTreeMap as Map;
66
use std::collections::BTreeSet as Set;
7+
use std::env;
78
use std::path::Path;
89

910
use tree_sitter::{Language, Node, Parser, Range, Tree};
1011

1112
pub mod simple;
1213

14+
/// Sets the tracing level based on the environment variables
15+
/// `RUST_LOG` and `CODEQL_VERBOSITY` (prioritized in that order),
16+
/// falling back to `warn` if neither is set.
17+
pub fn set_tracing_level(language: &str) -> () {
18+
tracing_subscriber::fmt()
19+
.with_target(false)
20+
.without_time()
21+
.with_level(true)
22+
.with_env_filter(
23+
tracing_subscriber::EnvFilter::try_from_default_env().unwrap_or_else(
24+
|_| -> tracing_subscriber::EnvFilter {
25+
let verbosity = env::var("CODEQL_VERBOSITY")
26+
.map(|v| match v.to_lowercase().as_str() {
27+
"off" | "errors" => "error",
28+
"warnings" => "warn",
29+
"info" | "progress" => "info",
30+
"debug" | "progress+" => "debug",
31+
"trace" | "progress++" | "progress+++" => "trace",
32+
_ => "warn",
33+
})
34+
.unwrap_or_else(|_| "warn");
35+
tracing_subscriber::EnvFilter::new(format!(
36+
"{}_extractor={}",
37+
language, verbosity
38+
))
39+
},
40+
),
41+
)
42+
.init();
43+
}
44+
1345
pub fn populate_file(writer: &mut trap::Writer, absolute_path: &Path) -> trap::Label {
1446
let (file_label, fresh) = writer.global_id(&trap::full_id_for_file(
1547
&file_paths::normalize_path(absolute_path),

0 commit comments

Comments
 (0)