Skip to content

Commit 32d2651

Browse files
karthiknadigDonJayamanne
authored andcommitted
Add logging over JSON-RPC (#23326)
1 parent 93f19f7 commit 32d2651

File tree

3 files changed

+81
-2
lines changed

3 files changed

+81
-2
lines changed

native_locator/src/logging.rs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
use crate::messaging;
5+
use serde::{Deserialize, Serialize};
6+
7+
#[derive(Serialize, Deserialize, PartialEq, Debug, Eq, Clone)]
8+
pub enum LogLevel {
9+
#[serde(rename = "debug")]
10+
Debug,
11+
#[serde(rename = "info")]
12+
Info,
13+
#[serde(rename = "warning")]
14+
Warning,
15+
#[serde(rename = "error")]
16+
Error,
17+
}
18+
19+
#[derive(Serialize, Deserialize)]
20+
#[serde(rename_all = "camelCase")]
21+
pub struct Log {
22+
pub message: String,
23+
pub level: LogLevel,
24+
}
25+
26+
#[derive(Serialize, Deserialize)]
27+
#[serde(rename_all = "camelCase")]
28+
pub struct LogMessage {
29+
pub jsonrpc: String,
30+
pub method: String,
31+
pub params: Log,
32+
}
33+
34+
impl LogMessage {
35+
pub fn new(message: String, level: LogLevel) -> Self {
36+
Self {
37+
jsonrpc: "2.0".to_string(),
38+
method: "log".to_string(),
39+
params: Log { message, level },
40+
}
41+
}
42+
}
43+
44+
pub fn log_debug(message: &str) {
45+
messaging::send_message(LogMessage::new(message.to_string(), LogLevel::Debug));
46+
}
47+
48+
pub fn log_info(message: &str) {
49+
messaging::send_message(LogMessage::new(message.to_string(), LogLevel::Info));
50+
}
51+
52+
pub fn log_warning(message: &str) {
53+
messaging::send_message(LogMessage::new(message.to_string(), LogLevel::Warning));
54+
}
55+
56+
pub fn log_error(message: &str) {
57+
messaging::send_message(LogMessage::new(message.to_string(), LogLevel::Error));
58+
}
59+
60+
pub fn log_msg(message: &str, level: LogLevel) {
61+
messaging::send_message(LogMessage::new(message.to_string(), level));
62+
}

native_locator/src/main.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,41 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
33

4+
use std::time::SystemTime;
5+
46
mod common_python;
57
mod conda;
68
mod known;
9+
mod logging;
710
mod messaging;
811
mod utils;
912
mod windows_python;
1013

1114
fn main() {
15+
let now = SystemTime::now();
16+
logging::log_info("Starting Native Locator");
17+
1218
// Finds python on PATH
1319
common_python::find_and_report();
1420

15-
// finds conda binary and conda environments
21+
// Finds conda binary and conda environments
1622
conda::find_and_report();
1723

1824
// Finds Windows Store, Known Path, and Registry pythons
1925
#[cfg(windows)]
2026
windows_python::find_and_report();
2127

28+
match now.elapsed() {
29+
Ok(elapsed) => {
30+
logging::log_info(&format!(
31+
"Native Locator took {} milliseconds.",
32+
elapsed.as_millis()
33+
));
34+
}
35+
Err(e) => {
36+
logging::log_error(&format!("Error getting elapsed time: {:?}", e));
37+
}
38+
}
39+
2240
messaging::send_message(messaging::ExitMessage::new());
2341
}

native_locator/src/messaging.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
use serde::{Deserialize, Serialize};
55

6-
76
#[derive(Serialize, Deserialize)]
87
#[serde(rename_all = "camelCase")]
98
pub struct EnvManager {

0 commit comments

Comments
 (0)