Skip to content
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
140 changes: 111 additions & 29 deletions RustApp/Cargo.lock

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

5 changes: 4 additions & 1 deletion RustApp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ resvg = "0.47"
rubato = "3"
#https://github.com/tauri-apps/fix-path-env-rs/pull/19
fix-path-env = { git = "https://github.com/wiiznokes/fix-path-env-rs", rev = "8612a0d8590843dbe9c6326d26292963e6a97f2a" }
env_logger = "0.11"
rnnoise2 = "0.1.2"
open = "5"
speexdsp = { git = "https://github.com/wiiznokes/speexdsp-rs-fork", branch = "helper-for-preprocess_ctl", features = [
Expand All @@ -112,6 +111,10 @@ fslock = "0.2"
interprocess = { version = "2", features = ["tokio"] }
async-stream = "0.3"
itertools = "0.14"
tracing = "0.1.44"
tracing-subscriber = { version = "0.3.23", features = ["env-filter", "fmt", "chrono"] }
tracing-appender = "0.2.5"
file-rotate = "0.8.0"


[target.'cfg(target_os = "windows")'.dependencies]
Expand Down
30 changes: 6 additions & 24 deletions RustApp/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]

use android_mic::{localize, single_instance};
use chrono::Local;
use std::io::Write;
use std::{fs::File, path::Path};

use android_mic::config::{Args, Config};
use android_mic::ui::app::run_ui;
use android_mic::utils::{self, APP, ORG, QUALIFIER};
use android_mic::utils::{self, APP, ORG, QUALIFIER, initialize_logfile};
use clap::Parser;
use directories::ProjectDirs;
use fslock::LockFile;
Expand Down Expand Up @@ -43,32 +42,15 @@ fn main() {
let project_dirs = ProjectDirs::from(QUALIFIER, ORG, APP).unwrap();

let log_path = if cfg!(debug_assertions) {
Path::new("log")
Path::new("log").to_path_buf()
} else {
project_dirs.cache_dir()
project_dirs.cache_dir().to_path_buf()
};
std::fs::create_dir_all(log_path).expect("Failed to create log directory");
std::fs::create_dir_all(&log_path).expect("Failed to create log directory");
let log_file_path = log_path.join(format!("{}.log", APP));

// setup log file
let target = Box::new(File::create(log_file_path.clone()).expect("Can't create log file"));
env_logger::Builder::new()
.format(|buf, record| {
writeln!(
buf,
"[{} {} {}] {}",
Local::now().format("%Y-%m-%dT%H:%M:%S"),
record.level(),
record.target(),
record.args()
)
})
.target(env_logger::Target::Pipe(Box::new(DualWriter {
file: target,
})))
.filter_level(log::LevelFilter::Warn)
.parse_default_env()
.init();
// NOTE: log guard should be alive for entire main lifespan
let _log_guard = initialize_logfile(&log_file_path);

// ensure single instance
let instance_lock_path = if cfg!(debug_assertions) {
Expand Down
34 changes: 34 additions & 0 deletions RustApp/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use std::path::PathBuf;

use cargo_packager_resource_resolver as resource_resolver;
use file_rotate::{ContentLimit, FileRotate, compression::Compression, suffix::AppendCount};
use log::error;
use tracing_appender::non_blocking::WorkerGuard;
use tracing_subscriber::{EnvFilter, fmt, layer::SubscriberExt, util::SubscriberInitExt};

use cached::cached;
use constcat::concat;
Expand Down Expand Up @@ -56,3 +59,34 @@ pub fn setup_wgpu() {
pub fn setup_wgpu() {
debug!("WGPU: Using default graphics backend.");
}

pub fn initialize_logfile(log_file_path: &PathBuf) -> WorkerGuard {
// configure size limit and 2 backup for rotation
let rotator = FileRotate::new(
log_file_path,
AppendCount::new(2), // 2 backups
ContentLimit::Bytes(1024 * 1024), // 1 MB size limit
Compression::None,
None,
);

// run on background threads
let (non_blocking_file, guard) = tracing_appender::non_blocking(rotator);

// set up log filter that can be overwrite by RUST_LOG
let filter = EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("warn"));

// set up subscriber with dual outputs
tracing_subscriber::registry()
.with(filter)
.with(fmt::layer().with_writer(std::io::stdout).with_target(true))
.with(
fmt::layer()
.with_ansi(false)
.with_writer(non_blocking_file)
.with_target(true),
)
.init();

guard
}
Loading