Skip to content

Commit 7933d32

Browse files
committed
refactor: Replace Logger with 3 functions
1 parent d547341 commit 7933d32

File tree

1 file changed

+60
-66
lines changed

1 file changed

+60
-66
lines changed

environment/src/lib.rs

Lines changed: 60 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,21 @@
11
use colored::*;
22

3-
pub struct Logger;
4-
5-
impl Logger {
6-
pub fn info(msg: &str) {
7-
println!("{} {}", "[xtask]".green().bold(), msg);
8-
}
3+
pub fn info(msg: &str) {
4+
println!("{} {}", "[xtask]".green().bold(), msg);
5+
}
96

10-
pub fn error(msg: &str) {
11-
println!("{} {}", "[xtask]".red().bold(), msg);
12-
}
7+
pub fn error(msg: &str) {
8+
println!("{} {}", "[xtask]".red().bold(), msg);
9+
}
1310

14-
pub fn warning(msg: &str) {
15-
println!("{} {}", "[xtask]".yellow().bold(), msg);
16-
}
11+
pub fn warning(msg: &str) {
12+
println!("{} {}", "[xtask]".yellow().bold(), msg);
1713
}
1814

1915
pub fn install_env(env: &str) {
2016
match env {
2117
"Python" => {
22-
Logger::info("Checking if Python is already installed...");
18+
info("Checking if Python is already installed...");
2319
println!();
2420

2521
// Check if python or python3 is installed
@@ -40,11 +36,11 @@ pub fn install_env(env: &str) {
4036
};
4137

4238
if check_installed {
43-
Logger::info("Python is already installed. No need to reinstall.");
39+
info("Python is already installed. No need to reinstall.");
4440
return;
4541
}
4642

47-
Logger::info("Python not detected. Installing Python environment...");
43+
info("Python not detected. Installing Python environment...");
4844

4945
#[cfg(target_os = "windows")]
5046
let output = std::process::Command::new("powershell")
@@ -90,26 +86,26 @@ pub fn install_env(env: &str) {
9086
.arg("brew install python")
9187
.status()
9288
} else {
93-
Logger::error(
89+
error(
9490
"No supported package manager found (apt-get, dnf, yum, pacman). Please install Python manually.",
9591
);
9692
return;
9793
}
9894
};
9995

10096
match output {
101-
Ok(status) if status.success() => Logger::info("Python installation completed!"),
102-
Ok(status) => Logger::error(&format!(
97+
Ok(status) if status.success() => info("Python installation completed!"),
98+
Ok(status) => error(&format!(
10399
"Python installation failed, exit code: {:?}",
104100
status.code()
105101
)),
106-
Err(e) => Logger::error(&format!(
102+
Err(e) => error(&format!(
107103
"Error occurred while running install command: {e}"
108104
)),
109105
}
110106
}
111107
"xmake" => {
112-
Logger::info("Checking if xmake is already installed...");
108+
info("Checking if xmake is already installed...");
113109
println!();
114110

115111
// Check if xmake is installed
@@ -121,11 +117,11 @@ pub fn install_env(env: &str) {
121117
.unwrap_or(false);
122118

123119
if check_installed {
124-
Logger::info("xmake is already installed. No need to reinstall.");
120+
info("xmake is already installed. No need to reinstall.");
125121
return;
126122
}
127123

128-
Logger::info("xmake not detected. Installing xmake environment...");
124+
info("xmake not detected. Installing xmake environment...");
129125

130126
#[cfg(target_os = "windows")]
131127
let output = std::process::Command::new("powershell")
@@ -142,18 +138,18 @@ pub fn install_env(env: &str) {
142138
.status();
143139

144140
match output {
145-
Ok(status) if status.success() => Logger::info("xmake installation completed!"),
146-
Ok(status) => Logger::error(&format!(
141+
Ok(status) if status.success() => info("xmake installation completed!"),
142+
Ok(status) => error(&format!(
147143
"xmake installation failed, exit code: {:?}",
148144
status.code()
149145
)),
150-
Err(e) => Logger::error(&format!(
146+
Err(e) => error(&format!(
151147
"Error occurred while running install command: {e}"
152148
)),
153149
}
154150
}
155151
"CUDA" => {
156-
Logger::info("Checking if CUDA Toolkit is already installed...");
152+
info("Checking if CUDA Toolkit is already installed...");
157153
println!();
158154

159155
// Check if cuda toolkit is installed
@@ -165,7 +161,7 @@ pub fn install_env(env: &str) {
165161
.unwrap_or(false);
166162

167163
if check_installed {
168-
Logger::info("CUDA Toolkit is already installed. No need to reinstall.");
164+
info("CUDA Toolkit is already installed. No need to reinstall.");
169165
return;
170166
}
171167

@@ -183,57 +179,57 @@ pub fn install_env(env: &str) {
183179
let smi_info = String::from_utf8_lossy(&output.stdout);
184180
for line in smi_info.lines() {
185181
if line.contains("CUDA Version") {
186-
Logger::info(&format!("Detected by nvidia-smi: {line}"));
182+
info(&format!("Detected by nvidia-smi: {line}"));
187183
}
188184
if let Some(idx) = line.find("CUDA Version:") {
189185
// extract the CUDA version number
190186
let version_str = line[idx + "CUDA Version:".len()..]
191187
.split_whitespace()
192188
.next()
193189
.unwrap_or("");
194-
Logger::info(&format!(
190+
info(&format!(
195191
"The highest CUDA version supported by your driver is {version_str}"
196192
));
197-
Logger::warning(
193+
warning(
198194
"You can also visit https://docs.nvidia.com/cuda/cuda-toolkit-release-notes/index.html to find the CUDA version compatible with your GPU driver.",
199195
);
200196
}
201197
}
202-
Logger::info(
198+
info(
203199
"Please make sure to install a CUDA Toolkit version compatible with your driver.",
204200
);
205201
} else {
206-
Logger::error(
202+
error(
207203
"nvidia-smi not found. Please make sure you have an NVIDIA GPU and drivers installed.",
208204
);
209205
}
210206

211207
println!();
212-
Logger::warning(
208+
warning(
213209
"Please visit https://developer.nvidia.com/cuda-toolkit-archive to select and download the appropriate CUDA version for your driver.",
214210
);
215211
}
216212
"OpenCL" => {
217-
Logger::info(
213+
info(
218214
"The current automatic installation script only supports OpenCL installation for Intel CPU on Windows or Ubuntu systems.",
219215
);
220-
Logger::info("Checking if OpenCL is already installed...");
216+
info("Checking if OpenCL is already installed...");
221217
println!();
222218

223219
// Check if OpenCL is installed
224220
#[cfg(target_os = "windows")]
225221
{
226222
let clinfo_path = std::path::Path::new("clinfo.exe");
227223
if !clinfo_path.exists() {
228-
Logger::info("Downloading clinfo tool...");
224+
info("Downloading clinfo tool...");
229225
let download_status = std::process::Command::new("curl")
230226
.args(["-o", "clinfo.exe", "https://github.com/ahoylabs/clinfo/releases/download/master-d2baa06/clinfo.exe"])
231227
.status();
232228

233229
if let Err(e) = download_status {
234-
Logger::error(&format!("Failed to download clinfo: {}", e));
235-
Logger::warning("You may need to enable proxy.");
236-
Logger::warning(
230+
error(&format!("Failed to download clinfo: {}", e));
231+
warning("You may need to enable proxy.");
232+
warning(
237233
"You can also manually download from https://github.com/ahoylabs/clinfo/releases/download/master-d2baa06/clinfo.exe",
238234
);
239235
return;
@@ -251,19 +247,19 @@ pub fn install_env(env: &str) {
251247
{
252248
if let Some(number) = line.split_whitespace().last() {
253249
if number == "0" {
254-
Logger::info("OpenCL is not installed.");
250+
info("OpenCL is not installed.");
255251
} else {
256-
Logger::info(&format!(
252+
info(&format!(
257253
"OpenCL is installed. Number of platforms: {}",
258254
number
259255
));
260256
return;
261257
}
262258
} else {
263-
Logger::error("Failed to parse the number of platforms.");
259+
error("Failed to parse the number of platforms.");
264260
}
265261
} else {
266-
Logger::error("Failed to find 'Number of platforms' in clinfo output.");
262+
error("Failed to find 'Number of platforms' in clinfo output.");
267263
}
268264
}
269265
#[cfg(not(target_os = "windows"))]
@@ -280,20 +276,18 @@ pub fn install_env(env: &str) {
280276

281277
if !has_cmd("clinfo") {
282278
if has_cmd("apt") {
283-
Logger::info("Installing clinfo tool...");
279+
info("Installing clinfo tool...");
284280
let install_status = std::process::Command::new("sh")
285281
.arg("-c")
286282
.arg("sudo apt update && sudo apt install opencl-headers ocl-icd-opencl-dev -y")
287283
.status();
288284

289285
if let Err(e) = install_status {
290-
Logger::error(&format!("Failed to install clinfo: {}", e));
286+
error(&format!("Failed to install clinfo: {}", e));
291287
return;
292288
}
293289
} else {
294-
Logger::error(
295-
"Unsupported package manager. Please install clinfo manually.",
296-
);
290+
error("Unsupported package manager. Please install clinfo manually.");
297291
return;
298292
}
299293
}
@@ -309,23 +303,23 @@ pub fn install_env(env: &str) {
309303
{
310304
if let Some(number) = line.split_whitespace().last() {
311305
if number == "0" {
312-
Logger::info("OpenCL is not installed.");
306+
info("OpenCL is not installed.");
313307
} else {
314-
Logger::info(&format!(
308+
info(&format!(
315309
"OpenCL is installed. Number of platforms: {}",
316310
number
317311
));
318312
return;
319313
}
320314
} else {
321-
Logger::error("Failed to parse the number of platforms.");
315+
error("Failed to parse the number of platforms.");
322316
}
323317
} else {
324-
Logger::error("Failed to find 'Number of platforms' in clinfo output.");
318+
error("Failed to find 'Number of platforms' in clinfo output.");
325319
}
326320
}
327321

328-
Logger::info("OpenCL not detected. Installing OpenCL environment...");
322+
info("OpenCL not detected. Installing OpenCL environment...");
329323

330324
#[cfg(target_os = "windows")]
331325
{
@@ -334,18 +328,18 @@ pub fn install_env(env: &str) {
334328
.status();
335329

336330
if let Err(e) = download_status {
337-
Logger::error(&format!(
331+
error(&format!(
338332
"Failed to download w_opencl_runtime_p_2025.1.0.972: {}",
339333
e
340334
));
341-
Logger::warning("You may need to enable proxy.");
342-
Logger::warning(
335+
warning("You may need to enable proxy.");
336+
warning(
343337
"You can also manually download from https://registrationcenter-download.intel.com/akdlm/IRC_NAS/b6dccdb7-b503-41ea-bd4b-a78e9c2d8dd6/w_opencl_runtime_p_2025.1.0.972.exe",
344338
);
345339
return;
346340
}
347341

348-
Logger::warning(
342+
warning(
349343
"Download completed. Please manually execute 'w_opencl_runtime_p_2025.1.0.972.exe' to install OpenCL for Intel CPU.",
350344
);
351345
}
@@ -362,18 +356,18 @@ pub fn install_env(env: &str) {
362356
};
363357

364358
if has_cmd("apt") {
365-
Logger::info("Installing opencl-headers...");
359+
info("Installing opencl-headers...");
366360
let install_status = std::process::Command::new("sh")
367361
.arg("-c")
368362
.arg("sudo apt update && sudo apt install opencl-headers ocl-icd-opencl-dev -y")
369363
.status();
370364

371365
if let Err(e) = install_status {
372-
Logger::error(&format!("Failed to install OpenCL: {}", e));
366+
error(&format!("Failed to install OpenCL: {}", e));
373367
return;
374368
}
375369

376-
Logger::info("Installing Intel OpenCL runtime...");
370+
info("Installing Intel OpenCL runtime...");
377371
let setup_status = std::process::Command::new("sh")
378372
.arg("-c")
379373
.arg(
@@ -385,23 +379,23 @@ pub fn install_env(env: &str) {
385379
.status();
386380

387381
if let Err(e) = setup_status {
388-
Logger::error(&format!("Failed to set up Intel OpenCL repository: {}", e));
382+
error(&format!("Failed to set up Intel OpenCL repository: {}", e));
389383
return;
390384
}
391385

392-
Logger::warning(
386+
warning(
393387
"Intel OpenCL runtime installation requires a proxy and may take time.",
394388
);
395-
Logger::warning(
389+
warning(
396390
"Please manually execute the following command after enabling the proxy:",
397391
);
398-
Logger::warning("sudo apt install -y intel-oneapi-runtime-opencl");
392+
warning("sudo apt install -y intel-oneapi-runtime-opencl");
399393
} else {
400-
Logger::error("Unsupported package manager. Please install OpenCL manually.");
394+
error("Unsupported package manager. Please install OpenCL manually.");
401395
}
402396
}
403397
}
404-
_ => Logger::error(&format!(
398+
_ => error(&format!(
405399
"Automatic installation for this environment is not supported: {env}"
406400
)),
407401
}

0 commit comments

Comments
 (0)