Skip to content

Commit d2fb681

Browse files
committed
[all] Clean up all calls to tracing::info, debug, etc to be consistent
1 parent 3eef8f7 commit d2fb681

File tree

2 files changed

+131
-57
lines changed

2 files changed

+131
-57
lines changed

2.code-search

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,21 @@ CHANGELOG.md:
99
1071: tracing, macros for for logging, eg: `tracing::debug!`, `tracing::info!`, etc. See
1010

1111
cmdr/src/analytics_client.rs:
12-
113: tracing::debug!(
13-
121: tracing::error!(
14-
132: tracing::error!(
15-
176: tracing::debug!(
16-
195: tracing::debug!(
17-
202: tracing::error!(
18-
254: tracing::debug!(
19-
260: tracing::error!(
20-
268: tracing::error!(
21-
302: tracing::info!(
22-
309: tracing::info!(
23-
331: tracing::debug!(
24-
339: tracing::error!("GET request failed: {}", format!("{response:#?}").red());
25-
353: tracing::debug!(
26-
361: tracing::error!("POST request failed: {}", format!("{response:#?}").red());
12+
117: tracing::debug!(
13+
129: tracing::error!(
14+
145: tracing::error!(
15+
193: tracing::debug!(
16+
216: tracing::debug!(
17+
227: tracing::error!(
18+
285: tracing::debug!(
19+
297: tracing::error!(
20+
309: tracing::error!(
21+
347: tracing::info!(
22+
360: tracing::info!(
23+
387: tracing::debug!(
24+
399: tracing::error!(
25+
420: tracing::debug!(
26+
432: tracing::error!(
2727

2828
cmdr/src/bin/edi.rs:
2929
45: tracing::debug!("Start logging... cli_args {:?}", cli_arg);

cmdr/src/analytics_client.rs

Lines changed: 116 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ use r3bl_ansi_color::{green, magenta, red};
2727
use r3bl_core::{CommonError,
2828
CommonErrorType,
2929
CommonResult,
30-
call_if_true,
31-
friendly_random_id};
30+
friendly_random_id,
31+
string_storage};
3232
use reqwest::{Client, Response};
3333

3434
use crate::DEBUG_ANALYTICS_CLIENT_MOD;
@@ -109,18 +109,26 @@ pub mod config_folder {
109109
let result_create_dir_all = fs::create_dir_all(&config_folder_path);
110110
match result_create_dir_all {
111111
Ok(_) => {
112-
call_if_true!(DEBUG_ANALYTICS_CLIENT_MOD, {
112+
DEBUG_ANALYTICS_CLIENT_MOD.then(|| {
113+
let message = "Successfully created config folder.";
114+
let config_folder = string_storage!("{config_folder_path:?}");
115+
let config_folder_fmt = green(&config_folder);
116+
// % is Display, ? is Debug.
113117
tracing::debug!(
114-
"Successfully created config folder: {}",
115-
green(&format!("{config_folder_path:?}"))
118+
message = %message,
119+
config_folder_path = %config_folder_fmt
116120
);
117121
});
118122
Ok(config_folder_path)
119123
}
120124
Err(error) => {
125+
let message = "Could not create config folder.";
126+
let error = string_storage!("{error:?}");
127+
let error_fmt = red(&error);
128+
// % is Display, ? is Debug.
121129
tracing::error!(
122-
"Could not create config folder.\n{}",
123-
red(&format!("{error:?}"))
130+
message = %message,
131+
error_fmt = %error_fmt,
124132
);
125133
CommonError::new_error_result_with_only_type(
126134
CommonErrorType::ConfigFolderCountNotBeCreated,
@@ -129,9 +137,14 @@ pub mod config_folder {
129137
}
130138
}
131139
None => {
140+
let message = "Could not get config folder.";
141+
let config_folder_path =
142+
string_storage!("{:?}", try_get_config_folder_path());
143+
let config_folder_fmt = red(&config_folder_path);
144+
// % is Display, ? is Debug.
132145
tracing::error!(
133-
"Could not get config folder.\n{}",
134-
red(&format!("{:?}", try_get_config_folder_path()))
146+
message = %message,
147+
config_folder_path = %config_folder_fmt
135148
);
136149
CommonError::new_error_result_with_only_type(
137150
CommonErrorType::ConfigFolderPathCouldNotBeGenerated,
@@ -172,10 +185,14 @@ pub mod proxy_machine_id {
172185
let result = file_io::try_read_file_contents(&id_file_path);
173186
match result {
174187
Ok(contents) => {
175-
call_if_true!(DEBUG_ANALYTICS_CLIENT_MOD, {
188+
DEBUG_ANALYTICS_CLIENT_MOD.then(|| {
189+
let message = "Successfully read proxy machine ID from file.";
190+
let contents = string_storage!("{contents:?}");
191+
let contents_fmt = green(&contents);
192+
// % is Display, ? is Debug.
176193
tracing::debug!(
177-
"Successfully read proxy machine ID from file: {}",
178-
green(&format!("{contents:?}"))
194+
message = %message,
195+
contents = %contents_fmt
179196
);
180197
});
181198
contents
@@ -190,18 +207,26 @@ pub mod proxy_machine_id {
190207
"".to_string(),
191208
AnalyticsAction::MachineIdProxyCreate,
192209
);
193-
194-
call_if_true!(DEBUG_ANALYTICS_CLIENT_MOD, {
210+
DEBUG_ANALYTICS_CLIENT_MOD.then(|| {
211+
let message =
212+
"Successfully wrote proxy machine ID to file.";
213+
let new_id = string_storage!("{new_id:?}");
214+
let new_id_fmt = green(&new_id);
215+
// % is Display, ? is Debug.
195216
tracing::debug!(
196-
"Successfully wrote proxy machine ID to file: {}",
197-
green(&format!("{new_id:?}"))
217+
message = %message,
218+
new_id = %new_id_fmt
198219
);
199220
});
200221
}
201222
Err(error) => {
223+
let message = "Could not write proxy machine ID to file.";
224+
let error = string_storage!("{error:?}");
225+
let error_fmt = red(&error);
226+
// % is Display, ? is Debug.
202227
tracing::error!(
203-
"Could not write proxy machine ID to file.\n{}",
204-
red(&format!("{error:?}"))
228+
message = %message,
229+
error = %error_fmt
205230
);
206231
}
207232
}
@@ -251,23 +276,39 @@ pub mod report_analytics {
251276
.await;
252277
match result {
253278
Ok(_) => {
254-
tracing::debug!(
255-
"Successfully reported analytics event to r3bl-base.\n{}",
256-
green(&format!("{json:#?}"))
257-
);
279+
DEBUG_ANALYTICS_CLIENT_MOD.then(|| {
280+
let message =
281+
"Successfully reported analytics event to r3bl-base.";
282+
let json = string_storage!("{json:#?}");
283+
let json_fmt = green(&json);
284+
// % is Display, ? is Debug.
285+
tracing::debug!(
286+
message = %message,
287+
json = %json_fmt
288+
);
289+
});
258290
}
259291
Err(error) => {
292+
let message =
293+
"Could not report analytics event to r3bl-base.";
294+
let error = string_storage!("{error:#?}");
295+
let error_fmt = red(&error);
296+
// % is Display, ? is Debug.
260297
tracing::error!(
261-
"Could not report analytics event to r3bl-base.\n{}",
262-
red(&format!("{error:#?}"))
298+
message = %message,
299+
error = %error_fmt
263300
);
264301
}
265302
}
266303
}
267304
Err(error) => {
305+
let message = "Could not serialize analytics event to JSON.";
306+
let error = string_storage!("{error:#?}");
307+
let error_fmt = red(&error);
308+
// % is Display, ? is Debug.
268309
tracing::error!(
269-
"Could not report analytics event to r3bl-base.\n{}",
270-
red(&format!("{error:#?}"))
310+
message = %message,
311+
error = %error_fmt
271312
);
272313
}
273314
}
@@ -299,17 +340,28 @@ pub mod upgrade_check {
299340
if let Ok(response) = result {
300341
if let Ok(body_text) = response.text().await {
301342
let latest_version = body_text.trim().to_string();
302-
tracing::info!(
303-
"\n📦📦📦\nLatest version of cmdr is: {}",
304-
magenta(&latest_version)
305-
);
343+
DEBUG_ANALYTICS_CLIENT_MOD.then(|| {
344+
let message = "\n📦📦📦\nLatest version of cmdr";
345+
let version_fmt = magenta(&latest_version);
346+
// % is Display, ? is Debug.
347+
tracing::info!(
348+
message = %message,
349+
version = %version_fmt
350+
);
351+
});
306352
let current_version = UPDATE_IF_NOT_THIS_VERSION.to_string();
307353
if latest_version != current_version {
308354
UPDATE_REQUIRED.store(true, std::sync::atomic::Ordering::Relaxed);
309-
tracing::info!(
310-
"\n💿💿💿\nThere is a new version of cmdr available: {}",
311-
magenta(&latest_version)
312-
);
355+
DEBUG_ANALYTICS_CLIENT_MOD.then(|| {
356+
let message =
357+
"\n💿💿💿\nThere is a new version of cmdr available";
358+
let version_fmt = magenta(&latest_version);
359+
// % is Display, ? is Debug.
360+
tracing::info!(
361+
message = %message,
362+
version = %version_fmt
363+
);
364+
});
313365
}
314366
}
315367
}
@@ -327,16 +379,27 @@ pub mod http_client {
327379
let response = client.get(url).send().await?;
328380
if response.status().is_success() {
329381
// Handle successful response.
330-
call_if_true!(DEBUG_ANALYTICS_CLIENT_MOD, {
382+
DEBUG_ANALYTICS_CLIENT_MOD.then(|| {
383+
let message = "GET request succeeded.";
384+
let response = string_storage!("{response:#?}");
385+
let response_fmt = green(&response);
386+
// % is Display, ? is Debug.
331387
tracing::debug!(
332-
"GET request succeeded: {}",
333-
green(&format!("{response:#?}"))
388+
message = %message,
389+
response = %response_fmt
334390
);
335391
});
336392
Ok(response)
337393
} else {
338394
// Handle error response.
339-
tracing::error!("GET request failed: {}", red(&format!("{response:#?}")));
395+
let message = "GET request failed.";
396+
let response_msg = string_storage!("{response:#?}");
397+
let response_msg_fmt = red(&response_msg);
398+
// % is Display, ? is Debug.
399+
tracing::error!(
400+
message = %message,
401+
response = %response_msg_fmt
402+
);
340403
response.error_for_status()
341404
}
342405
}
@@ -349,16 +412,27 @@ pub mod http_client {
349412
let response = client.post(url).json(data).send().await?;
350413
if response.status().is_success() {
351414
// Handle successful response.
352-
call_if_true!(DEBUG_ANALYTICS_CLIENT_MOD, {
415+
DEBUG_ANALYTICS_CLIENT_MOD.then(|| {
416+
let message = "POST request succeeded.";
417+
let response_msg = string_storage!("{response:#?}");
418+
let response_msg_fmt = green(&response_msg);
419+
// % is Display, ? is Debug.
353420
tracing::debug!(
354-
"POST request succeeded: {}",
355-
green(&format!("{response:#?}"))
421+
message = %message,
422+
response = %response_msg_fmt
356423
);
357424
});
358425
Ok(response)
359426
} else {
360427
// Handle error response.
361-
tracing::error!("POST request failed: {}", red(&format!("{response:#?}")));
428+
let message = "POST request failed.";
429+
let response_msg = string_storage!("{response:#?}");
430+
let response_msg_fmt = red(&response_msg);
431+
// % is Display, ? is Debug.
432+
tracing::error!(
433+
message = %message,
434+
response = %response_msg_fmt
435+
);
362436
response.error_for_status()
363437
}
364438
}

0 commit comments

Comments
 (0)