Skip to content

Commit ea6385c

Browse files
authored
fix(ml-cellar): replace println to logger (#14)
Signed-off-by: scepter914 <scepter914@gmail.com>
1 parent a1370fe commit ea6385c

11 files changed

Lines changed: 131 additions & 34 deletions

File tree

.github/CONTRIBUTING.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ If you want to add or change code, please open an issue first:
7070
- If you already have an approach in mind, describe it briefly so we can discuss.
7171

7272
This helps ensure that your work aligns with the project’s direction before you invest your time.
73+
If necessary for troubleshooting, please provide the debug log output when reporting the issue.
74+
75+
```sh
76+
RUST_LOG=debug ml-cellar init
77+
```
7378

7479
### 2. Implementation
7580

Cargo.lock

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

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ toml = "0.9.8"
1717
walkdir = "2.5.0"
1818
glob = "0.3.3"
1919
chrono = "0.4.43"
20+
log = "0.4.29"
21+
env_logger = "0.11.8"
2022

2123
[dev-dependencies]
2224
assert_cmd = "2.1.1"

src/bin/ml-cellar/check.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ pub fn check_required_files(config: &RackConfig, target_directory_path: &Path) {
77
for required_filename in &config.artifact.required_files {
88
let required_file_path = target_directory_path.join(required_filename);
99
if !required_file_path.exists() {
10-
eprintln!("Required file of {:?} is missing", required_file_path);
10+
log::error!("Required file of {:?} is missing", required_file_path);
1111
std::process::exit(1);
1212
}
1313
}
14-
println!("Required files are present");
14+
log::info!("All required files are confirmed");
1515
}
1616

1717
pub fn check_optional_files(config: &RackConfig, target_directory_path: &PathBuf) {
@@ -31,12 +31,12 @@ pub fn check_optional_files(config: &RackConfig, target_directory_path: &PathBuf
3131
if !config.artifact.is_required_file(&file_path)
3232
&& !config.artifact.is_optional_file(&file_path)
3333
{
34-
eprintln!("File {:?} is neither required nor optional", file_path);
34+
log::error!("File {:?} is neither required nor optional", file_path);
3535
std::process::exit(1);
3636
}
3737
}
3838

39-
println!("All files are required files or optional files");
39+
log::info!("All files are required files or optional files");
4040
}
4141

4242
pub fn check_version(config: &RackConfig, config_dir: &PathBuf, target_directory_path: &Path) {
@@ -48,28 +48,29 @@ pub fn check_version(config: &RackConfig, config_dir: &PathBuf, target_directory
4848
.project
4949
.is_valid_version(&target_directory_path_from_config_dir)
5050
{
51-
println!("Version format is valid");
51+
log::info!("Version format is valid");
5252
} else {
53-
eprintln!(
53+
log::error!(
5454
"Version format is invalid at {:?}. Please check ProjectConfig of {:?} ",
55-
target_directory_path_from_config_dir, config.project
55+
target_directory_path_from_config_dir,
56+
config.project
5657
);
5758
std::process::exit(1);
5859
}
5960
}
6061

6162
pub fn write_result_to_readme(config: &RackConfig, target_directory_path: &PathBuf) {
6263
if config.document.template_file.is_some() {
63-
println!("Write result of {:?} to README", target_directory_path);
64+
log::info!("Write result of {:?} to README", target_directory_path);
6465
}
6566
}
6667

6768
pub fn check(target_directory_path: PathBuf) {
6869
if !target_directory_path.exists() {
69-
eprintln!("{:?} does not exist", target_directory_path);
70+
log::error!("{:?} does not exist", target_directory_path);
7071
std::process::exit(1);
7172
} else {
72-
println!(
73+
log::info!(
7374
"Checking artifacts in rack at {:?}",
7475
target_directory_path.display()
7576
);
@@ -78,6 +79,5 @@ pub fn check(target_directory_path: PathBuf) {
7879
check_optional_files(&config, &target_directory_path);
7980
check_version(&config, &config_dir, &target_directory_path);
8081
write_result_to_readme(&config, &target_directory_path);
81-
println!("All artifacts are valid.");
8282
}
8383
}

src/bin/ml-cellar/clone.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ pub fn clone(repository: String) {
1212

1313
match status {
1414
Ok(s) if s.success() => {
15-
println!("git clone done");
15+
log::info!("git clone done");
1616
}
1717
Ok(s) => {
18-
eprintln!("git clone failed: {s}");
18+
log::error!("git clone failed: {s}");
1919
std::process::exit(1);
2020
}
2121
Err(e) => {
22-
eprintln!("failed to execute git: {e}");
22+
log::error!("failed to execute git: {e}");
2323
std::process::exit(1);
2424
}
2525
}

src/bin/ml-cellar/init.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ pub fn init() {
1515

1616
match status {
1717
Ok(s) if s.success() => {
18-
println!("git init done");
18+
log::info!("git init done");
1919
}
2020
Ok(s) => {
21-
eprintln!("git init failed: {s}");
21+
log::error!("git init failed: {s}");
2222
std::process::exit(1);
2323
}
2424
Err(e) => {
25-
eprintln!("failed to execute git: {e}");
25+
log::error!("failed to execute git: {e}");
2626
std::process::exit(1);
2727
}
2828
}
@@ -39,14 +39,14 @@ pub fn init() {
3939

4040
match status {
4141
Ok(s) if s.success() => {
42-
println!("git branch -M main done");
42+
log::info!("git branch -M main done");
4343
}
4444
Ok(s) => {
45-
eprintln!("git branch -M failed: {s}");
45+
log::error!("git branch -M failed: {s}");
4646
std::process::exit(1);
4747
}
4848
Err(e) => {
49-
eprintln!("failed to execute git: {e}");
49+
log::error!("failed to execute git: {e}");
5050
std::process::exit(1);
5151
}
5252
}

src/bin/ml-cellar/main.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
use clap::{Parser, Subcommand};
2+
use env_logger::{Builder, Env};
3+
use log::LevelFilter;
4+
use std::io::Write;
25
use std::path::PathBuf;
36

47
mod check;
@@ -45,6 +48,32 @@ enum Commands {
4548
},
4649
}
4750
fn main() {
51+
Builder::from_env(Env::default().default_filter_or("info"))
52+
.format(|buf, record| {
53+
let timestamp = buf.timestamp();
54+
let level = record.level();
55+
let level_style = buf.default_level_style(level);
56+
57+
if log::max_level() >= LevelFilter::Debug {
58+
let target = record.target();
59+
let file = record.file().unwrap_or("____unknown");
60+
let file = file.strip_prefix("src/").unwrap_or(file);
61+
let line = record.line().unwrap_or(0);
62+
63+
writeln!(
64+
buf,
65+
"[{timestamp} {level_style}{level}{level_style:#} {target}] [{file} {line}] {}",
66+
record.args()
67+
)
68+
} else {
69+
writeln!(
70+
buf,
71+
"[{timestamp} {level_style}{level}{level_style:#}] {}",
72+
record.args()
73+
)
74+
}
75+
})
76+
.init();
4877
let cli = Cli::parse();
4978
match cli.command {
5079
Commands::Init {} => {

src/bin/ml-cellar/materialize.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ pub fn materialize_all() {
1212

1313
match status {
1414
Ok(s) if s.success() => {
15-
println!("git lfs pull done");
15+
log::info!("git lfs pull done");
1616
}
1717
Ok(s) => {
18-
eprintln!("git lfs pull failed: {s}");
18+
log::error!("git lfs pull failed: {s}");
1919
std::process::exit(1);
2020
}
2121
Err(e) => {
22-
eprintln!("failed to execute git: {e}");
22+
log::error!("failed to execute git: {e}");
2323
std::process::exit(1);
2424
}
2525
}
@@ -38,14 +38,14 @@ pub fn materialize(path: PathBuf) {
3838

3939
match status {
4040
Ok(s) if s.success() => {
41-
println!("git lfs pull -I {} done", path.display());
41+
log::info!("git lfs pull -I {} done", path.display());
4242
}
4343
Ok(s) => {
44-
eprintln!("git lfs pull failed: {s}");
44+
log::error!("git lfs pull failed: {s}");
4545
std::process::exit(1);
4646
}
4747
Err(e) => {
48-
eprintln!("failed to execute git: {e}");
48+
log::error!("failed to execute git: {e}");
4949
std::process::exit(1);
5050
}
5151
}

src/bin/ml-cellar/rack.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ use ml_cellar::rack::RackConfig;
66

77
pub fn rack(path: PathBuf) {
88
if path.exists() {
9-
eprintln!("{:?} already exists", path);
9+
log::error!("{:?} already exists", path);
1010
std::process::exit(1);
1111
} else {
1212
let rack_name = path.file_name().unwrap().to_str().unwrap();
1313

1414
std::fs::create_dir_all(&path).expect("failed to create the directory for the rack");
15-
println!("Create rack at {}", path.display());
15+
log::info!("Create rack at {}", path.display());
1616

1717
// Make README.md
1818
let readme_path = path.join("README.md");

src/rack.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,14 +151,14 @@ pub fn load_config(path: &std::path::Path) -> (RackConfig, PathBuf) {
151151
let candidate = dir.join("config.toml");
152152
if candidate.is_file() {
153153
// Found config.toml, read and parse it
154-
println!("Loading config from {:?}", candidate);
154+
log::info!("Loading config from {:?}", candidate);
155155
let config_content = std::fs::read_to_string(&candidate).unwrap();
156156
return (toml::from_str(&config_content).unwrap(), dir);
157157
}
158158
match dir.parent() {
159159
Some(parent) => dir = parent.to_path_buf(),
160160
None => {
161-
eprintln!(
161+
log::error!(
162162
"config.toml not found; reached filesystem root at {:?}",
163163
dir
164164
);

0 commit comments

Comments
 (0)