Skip to content

Commit

Permalink
fix(windows): redirect stdout and stderr from c++ to get whisper errors
Browse files Browse the repository at this point in the history
  • Loading branch information
thewh1teagle committed Aug 25, 2024
1 parent 83bfe6d commit 98354ae
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 29 deletions.
16 changes: 14 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion desktop/src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,16 @@ openssl = { version = "0.10.63", features = ["vendored"] }
# Windows
[target.'cfg(windows)'.dependencies]
vibe_core = { path = "../../core", features = [] }
winreg = "0.52.0"
# Used to attach to console
windows = { version = "0.56.0", features = [
"Win32_System_Console",
"Win32_Foundation",
] }
winreg = "0.52.0"
# Used to redirect stdout/stderr from c++ to the attached console. otherwise whisper errors won't show
libc = "0.2.158"
libc-stdhandle = "0.1.0"


# macOS
[target.'cfg(target_os = "macos")'.dependencies]
Expand Down
42 changes: 22 additions & 20 deletions desktop/src-tauri/src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,41 +1,43 @@
use clap::Parser;
use eyre::{Context, ContextCompat, Result};
use once_cell::sync::Lazy;
use serde_json::Value;
use std::path::{Path, PathBuf};
use std::process;
use std::time::Instant;
use std::{env, process};
use tauri::AppHandle;
use vibe_core::config::TranscribeOptions;
use vibe_core::transcribe;

use crate::cmd::get_models_folder;
use crate::server;
use std::sync::atomic::AtomicBool;
use std::sync::atomic::Ordering;

pub static IS_CLI: Lazy<AtomicBool> = Lazy::new(|| AtomicBool::new(false));

/// Attach to console if cli detected in Windows
#[cfg(windows)]
pub fn attach_console() {
use windows::Win32::System::Console::{AttachConsole, ATTACH_PARENT_PROCESS};
if env::var("RUST_LOG").is_ok() || is_cli_detected() {
// we ignore the result here because
// if the app started from a command line, like cmd or powershell,
// it will attach sucessfully which is what we want
// but if we were started from something like explorer,
// it will fail to attach console which is also what we want.
let _ = unsafe { AttachConsole(ATTACH_PARENT_PROCESS) };
}
}

pub fn is_cli_detected() -> bool {
// Get the command-line arguments as an iterator
let args: Vec<String> = env::args().collect();

// Check if any argument starts with "--"
for arg in &args {
if arg.starts_with("--") || arg == "-h" {
return true;
let attach_result = unsafe { AttachConsole(ATTACH_PARENT_PROCESS) };
if attach_result.is_ok() {
IS_CLI.store(true, Ordering::Relaxed);
// Wer'e in CLI
// Experimental: redirect stdout and stderr to the new console. otherwise c++ bindings writes won't show.
// https://users.rust-lang.org/t/stderr-write-from-c-bindings-missing-on-windows/116582
unsafe {
let conout = std::ffi::CString::new("CONOUT$").expect("CString::new failed");
let stdout = libc_stdhandle::stdout();
let stderr = libc_stdhandle::stderr();
let mode = std::ffi::CString::new("w").unwrap();
libc::freopen(conout.as_ptr(), mode.as_ptr(), stdout);
libc::freopen(conout.as_ptr(), mode.as_ptr(), stderr);
}
tracing::debug!("CLI detected. attached console successfuly");
} else {
tracing::debug!("No CLI detected.");
}
false
}

/// Simple program to greet a person
Expand Down
11 changes: 8 additions & 3 deletions desktop/src-tauri/src/panic_hook.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
use crate::{
cli,
cli::IS_CLI,
cmd::is_portable,
config,
utils::{get_current_dir, LogError},
};
use chrono::Local;
use eyre::{eyre, Context, Result};
use std::{panic, path::PathBuf, sync::Arc};
use std::{
panic,
path::PathBuf,
sync::{atomic::Ordering, Arc},
};
use tauri::{AppHandle, Manager};

fn get_log_path(app: &AppHandle) -> Result<PathBuf> {
Expand Down Expand Up @@ -46,7 +50,8 @@ pub fn set_panic_hook(app: &AppHandle) -> Result<()> {
.context("write")
.map_err(|e| eyre!("{:?}", e))
.log_error();
if !cli::is_cli_detected() {
// Open the log path in release mode
if !cfg!(debug_assertions) && !IS_CLI.load(Ordering::Relaxed) {
showfile::show_path_in_file_manager(log_path.as_path());
}
}));
Expand Down
6 changes: 3 additions & 3 deletions desktop/src-tauri/src/setup.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use crate::{
cli,
cli::{self, IS_CLI},
config::STORE_FILENAME,
panic_hook,
utils::{get_issue_url, LogError},
};
use eyre::eyre;
use once_cell::sync::Lazy;
use std::fs;
use std::{fs, sync::atomic::Ordering};
use tauri::{App, Manager};
use tauri_plugin_dialog::DialogExt;
use tauri_plugin_shell::ShellExt;
Expand Down Expand Up @@ -105,7 +105,7 @@ pub fn setup(app: &App) -> Result<(), Box<dyn std::error::Error>> {
tracing::debug!("COMMIT_HASH: {}", env!("COMMIT_HASH"));

let app_handle = app.app_handle().clone();
if cli::is_cli_detected() {
if IS_CLI.load(Ordering::Relaxed) {
tauri::async_runtime::spawn(async move {
cli::run(&app_handle).await.map_err(|e| eyre!("{:?}", e)).log_error();
});
Expand Down

0 comments on commit 98354ae

Please sign in to comment.