1
0
Fork 0

Spectre/System/Log: Support Writer interface.

This commit is contained in:
Henrik Hautakoski 2023-02-18 13:54:57 +01:00
parent 8db01a0957
commit faed79c653
2 changed files with 20 additions and 4 deletions

View file

@ -7,6 +7,8 @@
namespace sp { namespace sp {
namespace log { class Writer; }
class Log class Log
{ {
public : public :
@ -18,6 +20,8 @@ public :
T_DEBUG = 1 << 3 T_DEBUG = 1 << 3
}; };
static void setWriter(log::Writer* writer);
static void info(const char *format, ...); static void info(const char *format, ...);
static void warn(const char *format, ...); static void warn(const char *format, ...);
@ -29,6 +33,8 @@ public :
private : private :
static void writeln(Type type, const char *fmt, va_list args); static void writeln(Type type, const char *fmt, va_list args);
static log::Writer* _writer;
}; };
} // namespace sp } // namespace sp

View file

@ -2,10 +2,18 @@
#include <string> #include <string>
#include <stdarg.h> #include <stdarg.h>
#include <Spectre/System/Log.h> #include <Spectre/System/Log.h>
#include <Spectre/System/Log/Writer.h>
namespace sp namespace sp
{ {
log::Writer* Log::_writer;
void Log::setWriter(log::Writer* writer)
{
_writer = writer;
}
void Log::info(const char *format, ...) { void Log::info(const char *format, ...) {
va_list vl; va_list vl;
@ -48,13 +56,13 @@ void Log::debug(const char *format, ...)
void Log::writeln(Type type, const char *format, va_list args) void Log::writeln(Type type, const char *format, va_list args)
{ {
FILE *fd = stderr; int len;
static char msg[4096];
static char buf[4096]; static char buf[4096];
const char *prefix; const char *prefix;
switch(type) { switch(type) {
case T_INFO : case T_INFO :
fd = stdout;
prefix = "INFO"; prefix = "INFO";
break; break;
case T_WARNING : case T_WARNING :
@ -73,8 +81,10 @@ void Log::writeln(Type type, const char *format, va_list args)
break; break;
} }
vsnprintf(buf, sizeof(buf), format, args); vsnprintf(msg, sizeof(msg), format, args);
fprintf(fd, "%s: %s\n", prefix, buf); len = sprintf(buf, "%s: %s\n", prefix, msg);
_writer->write(buf, len);
} }
} // namespace sp } // namespace sp