Improve desktop IPC logging (#11864)

* Improve desktop IPC logging

* Log error

* Force file to only log info, like the desktop app does

* use ?
This commit is contained in:
Daniel García 2024-11-14 17:45:19 +01:00 committed by GitHub
parent ef127fd26e
commit d0f24dc41f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 38 additions and 6 deletions

View File

@ -1,4 +1,8 @@
use std::{error::Error, path::Path, vec};
use std::{
error::Error,
path::{Path, PathBuf},
vec,
};
use futures::TryFutureExt;
@ -29,6 +33,7 @@ pub enum MessageType {
}
pub struct Server {
pub path: PathBuf,
cancel_token: CancellationToken,
server_to_clients_send: broadcast::Sender<String>,
}
@ -66,6 +71,7 @@ impl Server {
// Create the server and start listening for incoming connections
// in a separate task to avoid blocking the current task
let server = Server {
path: path.to_owned(),
cancel_token: cancel_token.clone(),
server_to_clients_send,
};

View File

@ -109,6 +109,8 @@ export declare namespace ipc {
* @param callback This function will be called whenever a message is received from a client.
*/
static listen(name: string, callback: (error: null | Error, message: IpcMessage) => void): Promise<IpcServer>
/** Return the path to the IPC server. */
getPath(): string
/** Stop the IPC server. */
stop(): void
/**

View File

@ -487,6 +487,12 @@ pub mod ipc {
Ok(IpcServer { server })
}
/// Return the path to the IPC server.
#[napi]
pub fn get_path(&self) -> String {
self.server.path.to_string_lossy().to_string()
}
/// Stop the IPC server.
#[napi]
pub fn stop(&self) -> napi::Result<()> {

View File

@ -8,14 +8,14 @@ use tokio_util::codec::LengthDelimitedCodec;
#[cfg(target_os = "macos")]
embed_plist::embed_info_plist!("../../../resources/info.desktop_proxy.plist");
fn init_logging(log_path: &Path, level: log::LevelFilter) {
fn init_logging(log_path: &Path, console_level: LevelFilter, file_level: LevelFilter) {
use simplelog::{ColorChoice, CombinedLogger, Config, SharedLogger, TermLogger, TerminalMode};
let config = Config::default();
let mut loggers: Vec<Box<dyn SharedLogger>> = Vec::new();
loggers.push(TermLogger::new(
level,
console_level,
config.clone(),
TerminalMode::Stderr,
ColorChoice::Auto,
@ -23,7 +23,7 @@ fn init_logging(log_path: &Path, level: log::LevelFilter) {
match std::fs::File::create(log_path) {
Ok(file) => {
loggers.push(simplelog::WriteLogger::new(level, config, file));
loggers.push(simplelog::WriteLogger::new(file_level, config, file));
}
Err(e) => {
eprintln!("Can't create file: {}", e);
@ -57,7 +57,12 @@ async fn main() {
path
};
init_logging(&log_path, LevelFilter::Info);
let level = std::env::var("PROXY_LOG_LEVEL")
.ok()
.and_then(|s| s.parse().ok())
.unwrap_or(LevelFilter::Info);
init_logging(&log_path, level, LevelFilter::Info);
info!("Starting Bitwarden IPC Proxy.");

View File

@ -93,11 +93,23 @@ export class NativeMessagingMain {
break;
}
case ipc.IpcMessageType.Message:
this.windowMain.win.webContents.send("nativeMessaging", JSON.parse(msg.message));
try {
const msgJson = JSON.parse(msg.message);
this.logService.debug("Native messaging message:", msgJson);
this.windowMain.win?.webContents.send("nativeMessaging", msgJson);
} catch (e) {
this.logService.warning("Error processing message:", e, msg.message);
}
break;
default:
this.logService.warning("Unknown message type:", msg.kind, msg.message);
break;
}
});
this.logService.info("Native messaging server started at:", this.ipcServer.getPath());
ipcMain.on("nativeMessagingReply", (event, msg) => {
if (msg != null) {
this.send(msg);
@ -110,6 +122,7 @@ export class NativeMessagingMain {
}
send(message: object) {
this.logService.debug("Native messaging reply:", message);
this.ipcServer?.send(JSON.stringify(message));
}