Logging to file in plugin


#1

Hi,

I’m new to Juce, and haven’t done any C++ coding in over 10 years. Was doing JavaScript and Swift recently. Anyway, I’m trying to figure out how to use Logger to log to a file (I’m creating a plugin), and not getting anywhere. I looked through the example code, the tutorials and GitHub, as well as this forum.

Not being an expert C++ developer and finding the documentation on JUCE lacking useful examples of how to use the API is driving me bonkers. Thinking I might be better off trying the VST SDK first before getting too deep into JUCE.

Questions:

  1. To log to a file should I be using Logger or FileLogger? I have basic dev console logging working OK. I tried the following code and got these compile time errors:
    unknown type name: fileLog
    then complains about arrow -> in 2nd line. I tried with ‘.’ and won’t work either.

FileLogger fileLog();
fileLog->createDateStampedLogger(“TailGunnerSr”, “TailGunnerSr”, “.log”, “Tail Gunner Sr - Log File”);

  1. I have the above code in a separate header file: myLogger.h does that make sense?

  2. I also tried the following, however the plugin crashes since the file “tail-gun-test.log” doesn’t exist. I thought the FileLogger constructor would create a new file if one doesn’t exist as per the docs:

fileToWriteTo the file that to use - new messages will be appended to the file. If the file doesn’t exist, it will be created, along with any parent directories that are needed.
https://docs.juce.com/master/classFileLogger.html#a708694692a4ab44a1436c8a55e6549f2

The code below crashes. If I change the File constructor to just:

File logfile;

Then it logs to the dev console OK, but doesn’t create a new file.

This is the code that crashes:

// in myLogger.h
File logfile(“tail-gun-test.log”);
FileLogger fileLog(logfile, “Welcome to Application”, 1000);

// in PluginEditor.cpp
fileLog.logMessage(“Testing 1 2 3!!!\n”);

Thanks for reading this far :slight_smile: Hope you can shine a light.


#2

As a member of some suitable class (probably your subclass of AudioProcessor) :

std::unique_ptr<FileLogger> m_flogger;

In the constructor of your class :

m_flogger = std::unique_ptr<FileLogger>(FileLogger::createDateStampedLogger("foo", "mylog", ".txt", "Welcome to plugin"));

To log :

double MyPluginProcessor::getTailLengthSeconds() const
{
	if (m_flogger)
		m_flogger->logMessage("getTailLengthSeconds called");
	return 0.0;
}

Note that the null pointer check (if (m_flogger)) is needed because creating the FileLogger may have failed in the constructor.

I just quickly tested that this way of using FileLogger works, there may also be alternative ways of using it.


#3

Since you are new to audio, just a word of caution: writing to log files is a system call and blocking, so be 100% sure to not call any logging from the audio thread!

Another warning, there is the static Logger::setCurrentLogger(), which is very handy. But in plugins this would create the problem, that you have many instances setting and resetting the logger, so that would create a mess.


#4

Very good point, thanks. I use the Juce DBG() macro for stuff I only want to see in a Debug build.