#include <cassert>
#include <cstdio>
#include <cstdarg>
#include <cstdlib>
#include <string>
#include "ofxsLog.h"
namespace OFX {
namespace Log {
static FILE *gLogFP = 0;
#define kLogFileEnvVar "OFX_PLUGIN_LOGFILE"
static std::string gLogFileName(getenv(kLogFileEnvVar) ? getenv(kLogFileEnvVar) : "ofxTestLog.txt");
static int gIndent = 0;
void setFileName(const std::string &value)
{
gLogFileName = value;
}
bool open(void)
{
#ifdef DEBUG
if(!gLogFP) {
gLogFP = fopen(gLogFileName.c_str(), "w");
return gLogFP != 0;
}
#endif
return gLogFP != 0;
}
void close(void)
{
if(gLogFP) {
fclose(gLogFP);
}
gLogFP = 0;
}
void indent(void)
{
++gIndent;
}
void outdent(void)
{
--gIndent;
}
static void doIndent(void)
{
if(open()) {
for(int i = 0; i < gIndent; i++) {
fputs(" ", gLogFP);
}
}
}
void print(const char *format, ...)
{
if(open()) {
doIndent();
va_list args;
va_start(args, format);
vfprintf(gLogFP, format, args);
fputc('\n', gLogFP);
fflush(gLogFP);
va_end(args);
}
}
void warning(bool condition, const char *format, ...)
{
if(condition && open()) {
doIndent();
fputs("WARNING : ", gLogFP);
va_list args;
va_start(args, format);
vfprintf(gLogFP, format, args);
fputc('\n', gLogFP);
va_end(args);
fflush(gLogFP);
}
}
void error(bool condition, const char *format, ...)
{
if(condition && open()) {
doIndent();
fputs("ERROR : ", gLogFP);
va_list args;
va_start(args, format);
vfprintf(gLogFP, format, args);
fputc('\n', gLogFP);
va_end(args);
fflush(gLogFP);
}
}
};
};