include/Spectre/System/Log.h: implement a static "Log" class instead of just a function.
This commit is contained in:
parent
e10daeaaa6
commit
3b27db9435
8 changed files with 101 additions and 31 deletions
|
|
@ -1,16 +1,65 @@
|
|||
|
||||
#include <string>
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <Spectre/System/Log.h>
|
||||
|
||||
namespace sp
|
||||
{
|
||||
void log(const char *fmt, ...) {
|
||||
|
||||
va_list vl;
|
||||
void Log::info(const char *message, ...) {
|
||||
|
||||
va_start(vl, fmt);
|
||||
vfprintf(stderr, fmt, vl);
|
||||
va_end(vl);
|
||||
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::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;
|
||||
default :
|
||||
case T_ERROR :
|
||||
prefix = "ERROR";
|
||||
break;
|
||||
}
|
||||
|
||||
vsnprintf(buf, sizeof(buf), message, args);
|
||||
fprintf(fd, "%s: %s\n", prefix, buf);
|
||||
}
|
||||
|
||||
} // namespace sp
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue