Skip to content

Commit 007511f

Browse files
Roland Peelenrolandpeelen
authored andcommitted
🔊 - 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 89a36b0 commit 007511f

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::fs::File;
@@ -149,13 +150,13 @@ pub fn initialize_build(
149150
let root_config_name = packages::get_package_name(&project_root);
150151
let rescript_version = helpers::get_rescript_version(&bsc_path);
151152

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

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

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

174-
print!(
175+
log::info!(
175176
"{} {}Finding source files...",
176177
style("[2/7]").bold().dim(),
177178
LOOKING_GLASS
@@ -187,7 +188,7 @@ pub fn initialize_build(
187188
);
188189
packages::parse_packages(&mut build_state);
189190
let timing_source_files_elapsed = timing_source_files.elapsed();
190-
println!(
191+
log::info!(
191192
"{}{} {}Found source files in {:.2}s",
192193
LINE_CLEAR,
193194
style("[2/7]").bold().dim(),
@@ -197,7 +198,7 @@ pub fn initialize_build(
197198
.as_secs_f64()
198199
);
199200

200-
print!(
201+
log::info!(
201202
"{} {}Reading compile state...",
202203
style("[3/7]").bold().dim(),
203204
COMPILE_STATE
@@ -206,7 +207,7 @@ pub fn initialize_build(
206207
let timing_compile_state = Instant::now();
207208
let compile_assets_state = read_compile_state::read(&mut build_state);
208209
let timing_compile_state_elapsed = timing_compile_state.elapsed();
209-
println!(
210+
log::info!(
210211
"{}{} {}Read compile state {:.2}s",
211212
LINE_CLEAR,
212213
style("[3/7]").bold().dim(),
@@ -216,15 +217,15 @@ pub fn initialize_build(
216217
.as_secs_f64()
217218
);
218219

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

283288
match result_asts {
284289
Ok(err) => {
285-
println!(
290+
log::info!(
286291
"{}{} {}Parsed {} source files in {:.2}s",
287292
LINE_CLEAR,
288293
format_step(current_step, total_steps),
289294
CODE,
290295
num_dirty_modules,
291296
default_timing.unwrap_or(timing_ast_elapsed).as_secs_f64()
292297
);
293-
print!("{}", &err);
298+
log::warn!("{}", &err);
294299
}
295300
Err(err) => {
296301
logs::finalize(&build_state.packages);
297-
println!(
302+
log::error!(
298303
"{}{} {}Error parsing source files in {:.2}s",
299304
LINE_CLEAR,
300305
format_step(current_step, total_steps),
301306
CROSS,
302307
default_timing.unwrap_or(timing_ast_elapsed).as_secs_f64()
303308
);
304-
print!("{}", &err);
309+
log::error!("{}", &err);
305310
return Err(IncrementalBuildError::SourceFileParseError);
306311
}
307312
}
@@ -310,7 +315,7 @@ pub fn incremental_build(
310315
let timing_deps_elapsed = timing_deps.elapsed();
311316
current_step += 1;
312317

313-
println!(
318+
log::info!(
314319
"{}{} {}Collected deps in {:.2}s",
315320
LINE_CLEAR,
316321
format_step(current_step, total_steps),
@@ -340,15 +345,12 @@ pub fn incremental_build(
340345
// }
341346

342347
let start_compiling = Instant::now();
343-
let pb = ProgressBar::new(build_state.modules.len().try_into().unwrap());
344-
pb.set_style(
345-
ProgressStyle::with_template(&format!(
346-
"{} {}Compiling... {{spinner}} {{pos}}/{{len}} {{msg}}",
347-
format_step(current_step, total_steps),
348-
SWORDS
349-
))
350-
.unwrap(),
351-
);
348+
let pb = if log_enabled!(Info) {
349+
ProgressBar::new(build_state.modules.len().try_into().unwrap())
350+
} else {
351+
ProgressBar::hidden()
352+
};
353+
352354
let (compile_errors, compile_warnings, num_compiled_modules) =
353355
compile::compile(build_state, || pb.inc(1), |size| pb.set_length(size));
354356
let compile_duration = start_compiling.elapsed();
@@ -360,17 +362,17 @@ pub fn incremental_build(
360362
pb.finish();
361363
if !compile_errors.is_empty() {
362364
if helpers::contains_ascii_characters(&compile_warnings) {
363-
println!("{}", &compile_warnings);
365+
log::error!("{}", &compile_warnings);
364366
}
365-
println!(
367+
log::info!(
366368
"{}{} {}Compiled {} modules in {:.2}s",
367369
LINE_CLEAR,
368370
format_step(current_step, total_steps),
369371
CROSS,
370372
num_compiled_modules,
371373
default_timing.unwrap_or(compile_duration).as_secs_f64()
372374
);
373-
print!("{}", &compile_errors);
375+
log::error!("{}", &compile_errors);
374376
// mark the original files as dirty again, because we didn't complete a full build
375377
for (module_name, module) in build_state.modules.iter_mut() {
376378
if tracked_dirty_modules.contains(module_name) {
@@ -379,7 +381,7 @@ pub fn incremental_build(
379381
}
380382
Err(IncrementalBuildError::CompileError)
381383
} else {
382-
println!(
384+
log::info!(
383385
"{}{} {}Compiled {} modules in {:.2}s",
384386
LINE_CLEAR,
385387
format_step(current_step, total_steps),
@@ -388,7 +390,7 @@ pub fn incremental_build(
388390
default_timing.unwrap_or(compile_duration).as_secs_f64()
389391
);
390392
if helpers::contains_ascii_characters(&compile_warnings) {
391-
print!("{}", &compile_warnings);
393+
log::warn!("{}", &compile_warnings);
392394
}
393395
Ok(())
394396
}
@@ -448,7 +450,7 @@ pub fn build(
448450
match incremental_build(&mut build_state, default_timing, true, false, create_sourcedirs) {
449451
Ok(_) => {
450452
let timing_total_elapsed = timing_total.elapsed();
451-
println!(
453+
log::info!(
452454
"\n{}{}Finished Compilation in {:.2}s",
453455
LINE_CLEAR,
454456
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)