[Question] How to add logging to a plugin?
What's the best way to add logging (to a log file) to a plugin? I tried the following, which works with rust vst2 (adapted from https://github.com/DGriffin91/egui_baseview_test_vst2). It creates the log file, but doesn't log anything to it:
use simplelog::{Config};
pub(crate) fn init(name: String, version: i32) {
if let Some(local_dir) = dirs::data_local_dir() {
// check if clockwork-vst folder exists, if not create it
let mut path = local_dir.clone();
path.push("clockwork-vst");
path.push("logs");
if !path.exists() {
std::fs::create_dir_all(&path).ok();
}
let logging_dir = path.clone();
if logging_dir.exists() {
let filename = format!("{name}-{version}-log.txt");
let log_file_path = logging_dir.join(filename);
if let Ok(log_file) = std::fs::OpenOptions::new()
.append(true)
.create(true)
.open(&log_file_path)
{
simplelog::WriteLogger::init(simplelog::LevelFilter::Info, Config::default(), log_file)
.ok();
log_panics::init();
log::info!("Starting VST");
} else {
panic!("Could not open log file");
}
}
}
}
Check the documentation on the nih_log!() macro. By default NIH-plug logs to STDERR unless a Windows debugger is attached ,in which case it will write there. Panics are also logged to the logger. You can override this or redirect the output to a file using the NIH_LOG environment variable as described in the documentation:
https://nih-plug.robbertvanderhelm.nl/nih_plug/macro.nih_log.html
How to set this env var? I tried the following, no logging file gets created anywhere:
config (in .cargo/config)
[env]
NIH_LOG = { value = "/Users/aw/nih-log", force = true }
and (in the code)
let key = "NIH_LOG";
env::set_var(key, "/Users/aw/nih-log");
Just like any other environment variable. On Linux and macOS you can just run your host from the terminal with env NIH_LOG=/Users/aw/nih-log host-name to temporarily override it for that command.
Just like any other environment variable. On Linux and macOS you can just run your host from the terminal with
env NIH_LOG=/Users/aw/nih-log host-nameto temporarily override it for that command.
Thanks. I thought you meant a cargo-specific environment variable.
I'll close this since NIH-plug did already have logging built in.