Skip to content

Commit c49f942

Browse files
author
Roland Peelen
committed
🔊 - Move logging to env_logger
This adds the ability to use log levels for CI. Running `RUST_LOG=warn rewatch build` will only use `warn` and up. See the [env_logger](https://docs.rs/env_logger/latest/env_logger/) crate for more info.
1 parent f74c893 commit c49f942

File tree

7 files changed

+57
-54
lines changed

7 files changed

+57
-54
lines changed

src/build.rs

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use ahash::AHashSet;
1616
use build_types::*;
1717
use console::style;
1818
use indicatif::{ProgressBar, ProgressStyle};
19+
use log::{log_enabled, Level::Info};
1920
use serde::Serialize;
2021
use std::fmt;
2122
use std::io::{stdout, Write};
@@ -148,13 +149,13 @@ pub fn initialize_build(
148149
let root_config_name = packages::get_package_name(&project_root);
149150
let rescript_version = helpers::get_rescript_version(&bsc_path);
150151

151-
print!("{}{}Building package tree...", style("[1/7]").bold().dim(), TREE);
152+
log::info!("{}{}Building package tree...", style("[1/7]").bold().dim(), TREE);
152153
let _ = stdout().flush();
153154
let timing_package_tree = Instant::now();
154155
let packages = packages::make(filter, &project_root, &workspace_root);
155156
let timing_package_tree_elapsed = timing_package_tree.elapsed();
156157

157-
println!(
158+
log::info!(
158159
"{}{} {}Built package tree in {:.2}s",
159160
LINE_CLEAR,
160161
style("[1/7]").bold().dim(),
@@ -170,7 +171,7 @@ pub fn initialize_build(
170171

171172
let timing_source_files = Instant::now();
172173

173-
print!(
174+
log::info!(
174175
"{} {}Finding source files...",
175176
style("[2/7]").bold().dim(),
176177
LOOKING_GLASS
@@ -186,7 +187,7 @@ pub fn initialize_build(
186187
);
187188
packages::parse_packages(&mut build_state);
188189
let timing_source_files_elapsed = timing_source_files.elapsed();
189-
println!(
190+
log::info!(
190191
"{}{} {}Found source files in {:.2}s",
191192
LINE_CLEAR,
192193
style("[2/7]").bold().dim(),
@@ -196,7 +197,7 @@ pub fn initialize_build(
196197
.as_secs_f64()
197198
);
198199

199-
print!(
200+
log::info!(
200201
"{} {}Reading compile state...",
201202
style("[3/7]").bold().dim(),
202203
COMPILE_STATE
@@ -205,7 +206,7 @@ pub fn initialize_build(
205206
let timing_compile_state = Instant::now();
206207
let compile_assets_state = read_compile_state::read(&mut build_state);
207208
let timing_compile_state_elapsed = timing_compile_state.elapsed();
208-
println!(
209+
log::info!(
209210
"{}{} {}Read compile state {:.2}s",
210211
LINE_CLEAR,
211212
style("[3/7]").bold().dim(),
@@ -215,15 +216,15 @@ pub fn initialize_build(
215216
.as_secs_f64()
216217
);
217218

218-
print!(
219+
log::info!(
219220
"{} {}Cleaning up previous build...",
220221
style("[4/7]").bold().dim(),
221222
SWEEP
222223
);
223224
let timing_cleanup = Instant::now();
224225
let (diff_cleanup, total_cleanup) = clean::cleanup_previous_build(&mut build_state, compile_assets_state);
225226
let timing_cleanup_elapsed = timing_cleanup.elapsed();
226-
println!(
227+
log::info!(
227228
"{}{} {}Cleaned {}/{} {:.2}s",
228229
LINE_CLEAR,
229230
style("[4/7]").bold().dim(),
@@ -263,7 +264,11 @@ pub fn incremental_build(
263264
) -> Result<(), IncrementalBuildError> {
264265
logs::initialize(&build_state.packages);
265266
let num_dirty_modules = build_state.modules.values().filter(|m| is_dirty(m)).count() as u64;
266-
let pb = ProgressBar::new(num_dirty_modules);
267+
let pb = if log_enabled!(Info) {
268+
ProgressBar::new(num_dirty_modules)
269+
} else {
270+
ProgressBar::hidden()
271+
};
267272
let mut current_step = if only_incremental { 1 } else { 5 };
268273
let total_steps = if only_incremental { 3 } else { 7 };
269274
pb.set_style(
@@ -281,26 +286,26 @@ pub fn incremental_build(
281286

282287
match result_asts {
283288
Ok(err) => {
284-
println!(
289+
log::info!(
285290
"{}{} {}Parsed {} source files in {:.2}s",
286291
LINE_CLEAR,
287292
format_step(current_step, total_steps),
288293
CODE,
289294
num_dirty_modules,
290295
default_timing.unwrap_or(timing_ast_elapsed).as_secs_f64()
291296
);
292-
print!("{}", &err);
297+
log::warn!("{}", &err);
293298
}
294299
Err(err) => {
295300
logs::finalize(&build_state.packages);
296-
println!(
301+
log::error!(
297302
"{}{} {}Error parsing source files in {:.2}s",
298303
LINE_CLEAR,
299304
format_step(current_step, total_steps),
300305
CROSS,
301306
default_timing.unwrap_or(timing_ast_elapsed).as_secs_f64()
302307
);
303-
print!("{}", &err);
308+
log::error!("{}", &err);
304309
return Err(IncrementalBuildError::SourceFileParseError);
305310
}
306311
}
@@ -309,7 +314,7 @@ pub fn incremental_build(
309314
let timing_deps_elapsed = timing_deps.elapsed();
310315
current_step += 1;
311316

312-
println!(
317+
log::info!(
313318
"{}{} {}Collected deps in {:.2}s",
314319
LINE_CLEAR,
315320
format_step(current_step, total_steps),
@@ -339,15 +344,12 @@ pub fn incremental_build(
339344
// }
340345

341346
let start_compiling = Instant::now();
342-
let pb = ProgressBar::new(build_state.modules.len().try_into().unwrap());
343-
pb.set_style(
344-
ProgressStyle::with_template(&format!(
345-
"{} {}Compiling... {{spinner}} {{pos}}/{{len}} {{msg}}",
346-
format_step(current_step, total_steps),
347-
SWORDS
348-
))
349-
.unwrap(),
350-
);
347+
let pb = if log_enabled!(Info) {
348+
ProgressBar::new(build_state.modules.len().try_into().unwrap())
349+
} else {
350+
ProgressBar::hidden()
351+
};
352+
351353
let (compile_errors, compile_warnings, num_compiled_modules) =
352354
compile::compile(build_state, || pb.inc(1), |size| pb.set_length(size));
353355
let compile_duration = start_compiling.elapsed();
@@ -359,17 +361,17 @@ pub fn incremental_build(
359361
pb.finish();
360362
if !compile_errors.is_empty() {
361363
if helpers::contains_ascii_characters(&compile_warnings) {
362-
println!("{}", &compile_warnings);
364+
log::error!("{}", &compile_warnings);
363365
}
364-
println!(
366+
log::info!(
365367
"{}{} {}Compiled {} modules in {:.2}s",
366368
LINE_CLEAR,
367369
format_step(current_step, total_steps),
368370
CROSS,
369371
num_compiled_modules,
370372
default_timing.unwrap_or(compile_duration).as_secs_f64()
371373
);
372-
print!("{}", &compile_errors);
374+
log::error!("{}", &compile_errors);
373375
// mark the original files as dirty again, because we didn't complete a full build
374376
for (module_name, module) in build_state.modules.iter_mut() {
375377
if tracked_dirty_modules.contains(module_name) {
@@ -378,7 +380,7 @@ pub fn incremental_build(
378380
}
379381
Err(IncrementalBuildError::CompileError)
380382
} else {
381-
println!(
383+
log::info!(
382384
"{}{} {}Compiled {} modules in {:.2}s",
383385
LINE_CLEAR,
384386
format_step(current_step, total_steps),
@@ -387,7 +389,7 @@ pub fn incremental_build(
387389
default_timing.unwrap_or(compile_duration).as_secs_f64()
388390
);
389391
if helpers::contains_ascii_characters(&compile_warnings) {
390-
print!("{}", &compile_warnings);
392+
log::warn!("{}", &compile_warnings);
391393
}
392394
Ok(())
393395
}
@@ -433,7 +435,7 @@ pub fn build(
433435
match incremental_build(&mut build_state, default_timing, true, false, create_sourcedirs) {
434436
Ok(_) => {
435437
let timing_total_elapsed = timing_total.elapsed();
436-
println!(
438+
log::info!(
437439
"\n{}{}Finished Compilation in {:.2}s",
438440
LINE_CLEAR,
439441
SPARKLES,

src/build/clean.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -332,14 +332,14 @@ pub fn clean(path: &str, bsc_path: Option<String>) {
332332
let rescript_version = helpers::get_rescript_version(&bsc_path);
333333

334334
let timing_clean_compiler_assets = Instant::now();
335-
print!(
335+
log::info!(
336336
"{} {} Cleaning compiler assets...",
337337
style("[1/2]").bold().dim(),
338338
SWEEP
339339
);
340340
std::io::stdout().flush().unwrap();
341341
packages.iter().for_each(|(_, package)| {
342-
print!(
342+
log::info!(
343343
"{}{} {} Cleaning {}...",
344344
LINE_CLEAR,
345345
style("[1/2]").bold().dim(),
@@ -358,7 +358,7 @@ pub fn clean(path: &str, bsc_path: Option<String>) {
358358
});
359359
let timing_clean_compiler_assets_elapsed = timing_clean_compiler_assets.elapsed();
360360

361-
println!(
361+
log::info!(
362362
"{}{} {}Cleaned compiler assets in {:.2}s",
363363
LINE_CLEAR,
364364
style("[1/2]").bold().dim(),
@@ -368,7 +368,7 @@ pub fn clean(path: &str, bsc_path: Option<String>) {
368368
std::io::stdout().flush().unwrap();
369369

370370
let timing_clean_mjs = Instant::now();
371-
print!("{} {} Cleaning mjs files...", style("[2/2]").bold().dim(), SWEEP);
371+
log::info!("{} {} Cleaning mjs files...", style("[2/2]").bold().dim(), SWEEP);
372372
std::io::stdout().flush().unwrap();
373373
let mut build_state = BuildState::new(
374374
project_root.to_owned(),
@@ -381,7 +381,7 @@ pub fn clean(path: &str, bsc_path: Option<String>) {
381381
packages::parse_packages(&mut build_state);
382382
clean_mjs_files(&build_state);
383383
let timing_clean_mjs_elapsed = timing_clean_mjs.elapsed();
384-
println!(
384+
log::info!(
385385
"{}{} {}Cleaned mjs files in {:.2}s",
386386
LINE_CLEAR,
387387
style("[2/2]").bold().dim(),

src/build/packages.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ pub fn read_folders(
150150
if metadata.file_type().is_dir() && recurse {
151151
match read_folders(filter, package_dir, &new_path, recurse) {
152152
Ok(s) => map.extend(s),
153-
Err(e) => println!("Error reading directory: {}", e),
153+
Err(e) => log::error!("Error reading directory: {}", e),
154154
}
155155
}
156156

@@ -167,8 +167,8 @@ pub fn read_folders(
167167
);
168168
}
169169

170-
Ok(_) => println!("Filtered: {:?}", name),
171-
Err(ref e) => println!("Error reading directory: {}", e),
170+
Ok(_) => log::info!("Filtered: {:?}", name),
171+
Err(ref e) => log::error!("Error reading directory: {}", e),
172172
},
173173
_ => (),
174174
}
@@ -309,7 +309,7 @@ fn read_dependencies(
309309
let (bsconfig, canonical_path) =
310310
match read_dependency(package_name, parent_path, project_root, &workspace_root) {
311311
Err(error) => {
312-
print!(
312+
log::error!(
313313
"{} {} Error building package tree. {}",
314314
style("[1/2]").bold().dim(),
315315
CROSS,
@@ -453,12 +453,12 @@ pub fn get_source_files(
453453
match read_folders(filter, package_dir, path_dir, recurse) {
454454
Ok(files) => map.extend(files),
455455
Err(_e) if type_ == &Some("dev".to_string()) => {
456-
println!(
456+
log::warn!(
457457
"Could not read folder: {}... Probably ok as type is dev",
458458
path_dir.to_string_lossy()
459459
)
460460
}
461-
Err(_e) => println!("Could not read folder: {}...", path_dir.to_string_lossy()),
461+
Err(_e) => log::error!("Could not read folder: {}...", path_dir.to_string_lossy()),
462462
}
463463
}
464464

@@ -666,7 +666,7 @@ pub fn parse_packages(build_state: &mut BuildState) {
666666
implementation_filename.pop();
667667
match source_files.get(&implementation_filename) {
668668
None => {
669-
println!(
669+
log::warn!(
670670
"{}Warning: No implementation file found for interface file (skipping): {}",
671671
LINE_CLEAR, file
672672
)
@@ -799,7 +799,7 @@ pub fn validate_packages_dependencies(packages: &AHashMap<String, Package>) -> b
799799
});
800800
}
801801
for (package_name, unallowed_deps) in detected_unallowed_dependencies.iter() {
802-
println!(
802+
log::error!(
803803
"\n{}: {} has the following unallowed dependencies:",
804804
console::style("Error").red(),
805805
console::style(package_name).bold()
@@ -813,7 +813,7 @@ pub fn validate_packages_dependencies(packages: &AHashMap<String, Package>) -> b
813813
.iter()
814814
.for_each(|(deps_type, map)| {
815815
if !map.is_empty() {
816-
println!(
816+
log::info!(
817817
"{} dependencies: {}",
818818
console::style(deps_type).bold().dim(),
819819
console::style(map.join(" \n -")).bold().dim()
@@ -824,7 +824,7 @@ pub fn validate_packages_dependencies(packages: &AHashMap<String, Package>) -> b
824824
let has_any_unallowed_dependent = detected_unallowed_dependencies.len() > 0;
825825

826826
if has_any_unallowed_dependent {
827-
println!(
827+
log::error!(
828828
"\nUpdate the {} value in the {} of the unallowed dependencies to solve the issue!",
829829
console::style("unallowed_dependents").bold().dim(),
830830
console::style("bsconfig.json").bold().dim()

src/build/parse.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,8 @@ fn generate_ast(
340340
Ok((ast_path, None))
341341
}
342342
} else {
343-
println!("Parsing file {}...", filename);
343+
log::info!("Parsing file {}...", filename);
344+
344345
Err(format!(
345346
"Could not find canonicalize_string_path for file {} in package {}",
346347
filename, package.name

src/cmd.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::time::Instant;
77
pub fn run(command_string: String) {
88
let start_subcommand = Instant::now();
99

10-
print!(
10+
log::info!(
1111
"{} {}Running subcommand... \n{}\n",
1212
style("[...]").bold().dim(),
1313
COMMAND,
@@ -39,11 +39,11 @@ pub fn run(command_string: String) {
3939
}
4040

4141
for line in std_err {
42-
println!("{}", line.unwrap());
42+
eprintln!("{}", line.unwrap());
4343
}
4444

4545
let subcommand_duration = start_subcommand.elapsed();
46-
println!(
46+
log::info!(
4747
"{}{} {}Ran subcommand in {:.2}s",
4848
LINE_CLEAR,
4949
style("[...]").bold().dim(),

src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ fn main() {
8282

8383
match lock::get(&folder) {
8484
lock::Lock::Error(ref e) => {
85-
eprintln!("Error while trying to get lock: {e}");
85+
log::error!("Error while trying to get lock: {e}");
8686
std::process::exit(1)
8787
}
8888
lock::Lock::Aquired(_) => match command {
@@ -96,7 +96,7 @@ fn main() {
9696
args.bsc_path,
9797
) {
9898
Err(e) => {
99-
eprintln!("Error Building: {e}");
99+
log::error!("Error Building: {e}");
100100
std::process::exit(1)
101101
}
102102
Ok(_) => {

0 commit comments

Comments
 (0)