nih-plug icon indicating copy to clipboard operation
nih-plug copied to clipboard

[Question] How to add logging to a plugin?

Open AlexW00 opened this issue 3 years ago • 4 comments

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");
            }
        }
    }
}

AlexW00 avatar Sep 01 '22 21:09 AlexW00

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

robbert-vdh avatar Sep 01 '22 23:09 robbert-vdh

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");

AlexW00 avatar Sep 23 '22 14:09 AlexW00

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.

robbert-vdh avatar Sep 23 '22 14:09 robbert-vdh

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.

Thanks. I thought you meant a cargo-specific environment variable.

AlexW00 avatar Sep 23 '22 16:09 AlexW00

I'll close this since NIH-plug did already have logging built in.

robbert-vdh avatar Oct 19 '22 10:10 robbert-vdh