Skip to content

Add logging over JSON-RPC #23326

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions native_locator/src/logging.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

use crate::messaging;
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, PartialEq, Debug, Eq, Clone)]
pub enum LogLevel {
#[serde(rename = "debug")]
Debug,
#[serde(rename = "info")]
Info,
#[serde(rename = "warning")]
Warning,
#[serde(rename = "error")]
Error,
}

#[derive(Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Log {
pub message: String,
pub level: LogLevel,
}

#[derive(Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct LogMessage {
pub jsonrpc: String,
pub method: String,
pub params: Log,
}

impl LogMessage {
pub fn new(message: String, level: LogLevel) -> Self {
Self {
jsonrpc: "2.0".to_string(),
method: "log".to_string(),
params: Log { message, level },
}
}
}

pub fn log_debug(message: &str) {
messaging::send_message(LogMessage::new(message.to_string(), LogLevel::Debug));
}

pub fn log_info(message: &str) {
messaging::send_message(LogMessage::new(message.to_string(), LogLevel::Info));
}

pub fn log_warning(message: &str) {
messaging::send_message(LogMessage::new(message.to_string(), LogLevel::Warning));
}

pub fn log_error(message: &str) {
messaging::send_message(LogMessage::new(message.to_string(), LogLevel::Error));
}

pub fn log_msg(message: &str, level: LogLevel) {
messaging::send_message(LogMessage::new(message.to_string(), level));
}
20 changes: 19 additions & 1 deletion native_locator/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,41 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

use std::time::SystemTime;

mod common_python;
mod conda;
mod known;
mod logging;
mod messaging;
mod utils;
mod windows_python;

fn main() {
let now = SystemTime::now();
logging::log_info("Starting Native Locator");

// Finds python on PATH
common_python::find_and_report();

// finds conda binary and conda environments
// Finds conda binary and conda environments
conda::find_and_report();

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

match now.elapsed() {
Ok(elapsed) => {
logging::log_info(&format!(
"Native Locator took {} milliseconds.",
elapsed.as_millis()
));
}
Err(e) => {
logging::log_error(&format!("Error getting elapsed time: {:?}", e));
}
}

messaging::send_message(messaging::ExitMessage::new());
}
1 change: 0 additions & 1 deletion native_locator/src/messaging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

use serde::{Deserialize, Serialize};


#[derive(Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct EnvManager {
Expand Down
Loading