Spectre/System/Log: Support Writer interface.
This commit is contained in:
parent
8db01a0957
commit
faed79c653
2 changed files with 20 additions and 4 deletions
|
|
@ -7,6 +7,8 @@
|
|||
|
||||
namespace sp {
|
||||
|
||||
namespace log { class Writer; }
|
||||
|
||||
class Log
|
||||
{
|
||||
public :
|
||||
|
|
@ -18,6 +20,8 @@ public :
|
|||
T_DEBUG = 1 << 3
|
||||
};
|
||||
|
||||
static void setWriter(log::Writer* writer);
|
||||
|
||||
static void info(const char *format, ...);
|
||||
|
||||
static void warn(const char *format, ...);
|
||||
|
|
@ -29,6 +33,8 @@ public :
|
|||
private :
|
||||
|
||||
static void writeln(Type type, const char *fmt, va_list args);
|
||||
|
||||
static log::Writer* _writer;
|
||||
};
|
||||
|
||||
} // namespace sp
|
||||
|
|
|
|||
|
|
@ -2,10 +2,18 @@
|
|||
#include <string>
|
||||
#include <stdarg.h>
|
||||
#include <Spectre/System/Log.h>
|
||||
#include <Spectre/System/Log/Writer.h>
|
||||
|
||||
namespace sp
|
||||
{
|
||||
|
||||
log::Writer* Log::_writer;
|
||||
|
||||
void Log::setWriter(log::Writer* writer)
|
||||
{
|
||||
_writer = writer;
|
||||
}
|
||||
|
||||
void Log::info(const char *format, ...) {
|
||||
|
||||
va_list vl;
|
||||
|
|
@ -48,13 +56,13 @@ void Log::debug(const char *format, ...)
|
|||
|
||||
void Log::writeln(Type type, const char *format, va_list args)
|
||||
{
|
||||
FILE *fd = stderr;
|
||||
int len;
|
||||
static char msg[4096];
|
||||
static char buf[4096];
|
||||
const char *prefix;
|
||||
|
||||
switch(type) {
|
||||
case T_INFO :
|
||||
fd = stdout;
|
||||
prefix = "INFO";
|
||||
break;
|
||||
case T_WARNING :
|
||||
|
|
@ -73,8 +81,10 @@ void Log::writeln(Type type, const char *format, va_list args)
|
|||
break;
|
||||
}
|
||||
|
||||
vsnprintf(buf, sizeof(buf), format, args);
|
||||
fprintf(fd, "%s: %s\n", prefix, buf);
|
||||
vsnprintf(msg, sizeof(msg), format, args);
|
||||
len = sprintf(buf, "%s: %s\n", prefix, msg);
|
||||
|
||||
_writer->write(buf, len);
|
||||
}
|
||||
|
||||
} // namespace sp
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue