A Simple Debugging Formatted Message File Logger- Glitches audio

Edit: It turned out that debug_trace() would reliably glitch audio on a VCO with no input CV if the function was called in a loop within process(), whereas DEBUG() does not. I added a mutex but that did not help. I eventually determined that opening for appending and closing the file each call was the source of the glitch. After changing to leave file open for writing after the first call, the glitch went away.

As @Vortico pointed out to me the DEBUG() macro, I recommend that no one use my code. I’ve leaving the code here for a while in case anyone wants to explore how fileio can cause audio clicks and pops and maybe give some ideas of how those happen.

Ken

bool DebugFileEnable=true; // enable debug_trace()
bool debug_busy=false;  // simple reentrancy block

const char debugfile_path[]="D:/VCV Rack Plugin Development/Meander/src/debug.txt";

void debug_trace(const char *szFormat, ...)
  {
    if (!DebugFileEnable)
      return;  // don't do anything

    FILE *output;

    if (strcmp(szFormat,"")==0)  // if argument string is empty, clear the debug file
    {
	if ((output=fopen(debugfile_path,"w")))
	{
      	     fclose(output);
	     return;
        }
    }

    if (debug_busy) return;
    debug_busy=true;
    char szBuffer[1024];

    va_list pArgs;
    va_start(pArgs, szFormat);
    vsprintf(szBuffer, szFormat, pArgs);
    va_end(pArgs);
    
    if ((output=fopen(debugfile_path,"a")))  // append the message to the end of the file
    {
      fprintf(output,"%s\n",szBuffer);
      fflush(output);
      fclose(output);
    }
    debug_busy=false;
  }

// example usage calls to clear the debug file and to append a couple of formatted messages to the file
debug_trace("");  // clear debug log file
debug_trace("Meander()");
debug_trace("Meander()-%s %d","Ken", 2019);

See DEBUG, INFO, WARN, etc in https://github.com/VCVRack/Rack/blob/v1/include/logger.hpp#L12-L15.
Usage:

DEBUG("%s %d", str.c_str(), 42);