Skip to content

Instantly share code, notes, and snippets.

@shakesoda
Created December 11, 2011 20:23
Show Gist options
  • Save shakesoda/1462548 to your computer and use it in GitHub Desktop.
Save shakesoda/1462548 to your computer and use it in GitHub Desktop.
#define FORMAT(str) \
{ \
va_list va; \
char staticbuf[1024]; \
char *buf = staticbuf; \
va_start(va, in); \
unsigned int need = vsnprintf(buf, sizeof(staticbuf), in, va) + 1; \
if (need > sizeof(staticbuf)) \
{ \
/* staticbuf wasn't large enough, malloc large enough */ \
buf = (char *) malloc(need); \
va_start(va,in); \
vsnprintf(buf, need, in, va); \
} \
va_end(va); \
str = string(buf); \
/* free if we had to malloc more space */ \
if (buf != staticbuf) \
free(buf); \
}
void Logger::Warn(const char *in, ...)
{
string str;
FORMAT(str);
// Time
char sTime[25];
GetTime(sTime);
char buf[strlen(sTime)+str.length()+11];
sprintf(buf, "%s WARNING: %s\n", sTime, str.c_str());
PHYSFS_write(m_File, buf, sizeof(buf), 1);
if (m_UseColors)
printf("\x1b[%d;1m%s:\x1b[0m ", FG_YELLOW, "WARNING");
else
printf("%s: %s", "WARNING", str.c_str());
}
// Problem: works fine when color is disabled, explodes when enabled.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment