1
0
Fork 0
spectre/source/System/Log.cpp

80 lines
1.2 KiB
C++

#include <string>
#include <stdarg.h>
#include <Spectre/System/Log.h>
namespace sp
{
void Log::info(const char *message, ...) {
va_list vl;
va_start(vl, message);
writeln(T_INFO, message, vl);
va_end(vl);
}
void Log::warn(const char *message, ...)
{
va_list vl;
va_start(vl, message);
writeln(T_WARNING, message, vl);
va_end(vl);
}
int Log::error(const char *message, ...)
{
va_list vl;
va_start(vl, message);
writeln(T_ERROR, message, vl);
va_end(vl);
return 1;
}
void Log::debug(const char *message, ...)
{
#ifdef SPECTRE_DEBUG
va_list vl;
va_start(vl, message);
writeln(T_DEBUG, message, vl);
va_end(vl);
#endif /* SPECTRE_DEBUG */
}
void Log::writeln(Type type, const char *message, va_list args)
{
FILE *fd = stderr;
static char buf[4096];
const char *prefix;
switch(type) {
case T_INFO :
fd = stdout;
prefix = "INFO";
break;
case T_WARNING :
prefix = "WARN";
break;
case T_CRITICAL:
prefix = "CRIT";
break;
#ifdef SPECTRE_DEBUG
case T_DEBUG:
prefix = "DEBUG";
break;
#endif /* SPECTRE_DEBUG */
case T_ERROR : default :
prefix = "ERROR";
break;
}
vsnprintf(buf, sizeof(buf), message, args);
fprintf(fd, "%s: %s\n", prefix, buf);
}
} // namespace sp