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
|
|
@ -2,9 +2,31 @@
|
|||
#ifndef SYSTEM_LOG_H
|
||||
#define SYSTEM_LOG_H
|
||||
|
||||
#include <string>
|
||||
#include <stdarg.h>
|
||||
|
||||
namespace sp {
|
||||
|
||||
void log(const char *fmt, ...);
|
||||
class Log
|
||||
{
|
||||
public :
|
||||
enum Type {
|
||||
T_INFO = 0,
|
||||
T_WARNING = 1 << 0,
|
||||
T_CRITICAL = 1 << 1,
|
||||
T_ERROR = 1 << 2,
|
||||
};
|
||||
|
||||
static void info(const char *message, ...);
|
||||
|
||||
static void warn(const char *message, ...);
|
||||
|
||||
static int error(const char *message, ...);
|
||||
|
||||
private :
|
||||
|
||||
static void writeln(Type type, const char *fmt, va_list args);
|
||||
};
|
||||
|
||||
} // namespace sp
|
||||
|
||||
|
|
|
|||
|
|
@ -108,11 +108,11 @@ void BatchRenderer2D::setBatchSize(unsigned short size)
|
|||
//glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
|
||||
// Log
|
||||
log("BatchRenderer - BatchSize\n");
|
||||
log(" num sprites: %i\n", m_size);
|
||||
log(" num indices: %i\n", m_size * 6);
|
||||
log(" num vertices: %i\n", m_size);
|
||||
log("------\n");
|
||||
Log::info("BatchRenderer - BatchSize");
|
||||
Log::info(" num sprites: %i", m_size);
|
||||
Log::info(" num indices: %i", m_size * 6);
|
||||
Log::info(" num vertices: %i", m_size);
|
||||
Log::info("------");
|
||||
}
|
||||
|
||||
void BatchRenderer2D::begin()
|
||||
|
|
|
|||
|
|
@ -38,12 +38,11 @@ LibWrapper::LibWrapper()
|
|||
{
|
||||
FT_Error error = FT_Init_FreeType(&handle);
|
||||
if (error) {
|
||||
log("Could not initialize FreeType\n");
|
||||
Log::error("Could not initialize FreeType");
|
||||
return;
|
||||
} else {
|
||||
log("FreeType font driver was initialized.\n");
|
||||
}
|
||||
|
||||
Log::info("FreeType font driver was initialized.");
|
||||
error = FT_Stroker_New(handle, &stroker);
|
||||
}
|
||||
|
||||
|
|
@ -55,7 +54,7 @@ LibWrapper::~LibWrapper()
|
|||
|
||||
error = FT_Done_FreeType(handle);
|
||||
if (error) {
|
||||
log("Could not close FreeType\n");
|
||||
Log::error("Could not close FreeType");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -75,7 +74,7 @@ bool FreeTypeDriver::setCharacterSize(unsigned int size)
|
|||
{
|
||||
FT_Error error = FT_Set_Pixel_Sizes(m_face, 0, size);
|
||||
if (error) {
|
||||
log("FreeType: failed to set character size\n");
|
||||
Log::warn("FreeType: failed to set character size");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
|
|
@ -88,14 +87,14 @@ bool FreeTypeDriver::loadFromFile(const std::string& filename)
|
|||
|
||||
error = FT_New_Face(LibWrapper::getInstance().handle, filename.c_str(), 0, &face);
|
||||
if (error) {
|
||||
log("FreeType: could not load file (%s): %s\n",
|
||||
Log::warn("FreeType: could not load file (%s): %s",
|
||||
filename.c_str(), FT_GetErrorString(error));
|
||||
return false;
|
||||
}
|
||||
|
||||
error = FT_Select_Charmap(face, FT_ENCODING_UNICODE);
|
||||
if (error) {
|
||||
log("FreeType: (%s) failed to set unicode charmap\n", filename.c_str());
|
||||
Log::warn("FreeType: (%s) failed to set unicode charmap", filename.c_str());
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
@ -121,7 +120,7 @@ Font::Glyph FreeTypeDriver::loadGlyph(unsigned int codepoint, Image& img, unsign
|
|||
}
|
||||
|
||||
if (FT_Load_Char(m_face, codepoint, flags) != 0) {
|
||||
log("FreeType: failed to load glyph for character code '%c'\n", codepoint);
|
||||
Log::warn("FreeType: failed to load glyph for character code '%c'", codepoint);
|
||||
}
|
||||
|
||||
FT_Error error = FT_Get_Glyph(m_face->glyph, &glyph_info);
|
||||
|
|
@ -158,7 +157,7 @@ Font::Glyph FreeTypeDriver::loadGlyph(unsigned int codepoint, Image& img, unsign
|
|||
|
||||
glyph.advance = static_cast<unsigned char>(metrics.horiAdvance >> 6) + (outlineSize * 2);
|
||||
} else {
|
||||
log("FreeType: failed to load glyph for character code '%c'\n", codepoint);
|
||||
Log::warn("FreeType: failed to load glyph for character code '%c'", codepoint);
|
||||
}
|
||||
|
||||
return glyph;
|
||||
|
|
|
|||
|
|
@ -37,14 +37,14 @@ bool ImageLoader::loadFromFile(const char *filename, Image& img)
|
|||
|
||||
// loaded into memory. now decode.
|
||||
if (decode((const char*)&buf[0], buf.size(), img) == false) {
|
||||
log("ImageLoader: could not load file '%s'. Reason: %s",
|
||||
Log::warn("ImageLoader: could not load file '%s'. Reason: %s",
|
||||
filename, m_error);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
log("ImageLoader: could not open file '%s'. Reason: %s",
|
||||
Log::warn("ImageLoader: could not open file '%s'. Reason: %s",
|
||||
filename, strerror(errno));
|
||||
return false;
|
||||
}
|
||||
|
|
@ -67,12 +67,12 @@ bool ImageLoader::saveToFile(const Image& img, const char *filename)
|
|||
if (ext == "png") {
|
||||
|
||||
if (!encodePNG(img, encoded_data)) {
|
||||
log("ImageLoader: failed to save file '%s'. Reason: \n",
|
||||
Log::warn("ImageLoader: failed to save file '%s'. Reason: %s",
|
||||
filename, m_error);
|
||||
}
|
||||
|
||||
} else {
|
||||
log("ImageLoader: Invalid file format\n");
|
||||
Log::warn("ImageLoader: Invalid file format");
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -53,7 +53,7 @@ bool Win32Display::create(DisplayDescription description)
|
|||
NULL, NULL, GetModuleHandle(NULL), (LPVOID) this);
|
||||
|
||||
if (!m_handle) {
|
||||
log("Win32 - Could not create window: %s", Win32GetMessage(GetLastError()));
|
||||
Log::error("Win32 - Could not create window: %s", Win32GetMessage(GetLastError()));
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -18,11 +18,11 @@ static void ensureExtensionsLoaded(HDC dc)
|
|||
init = true;
|
||||
|
||||
if (!gladLoadWGL(dc)) {
|
||||
log("Win32: Could not load WGL extensions\n");
|
||||
Log::error("Win32: Could not load WGL extensions");
|
||||
}
|
||||
|
||||
if (!gladLoadGL()) {
|
||||
log("Win32: Could not load OpenGL extensions\n");
|
||||
Log::error("Win32: Could not load OpenGL extensions\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -48,7 +48,7 @@ bool Win32GLContext::create(const PlatformDisplay* display)
|
|||
|
||||
// Should have a valid handle here. trigger error.
|
||||
if (!m_wnd) {
|
||||
log("Win32 - Could not create GL context: Invalid display handle\n");
|
||||
Log::warn("Win32 - Could not create GL context: Invalid display handle\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
@ -61,7 +61,7 @@ bool Win32GLContext::create(const PlatformDisplay* display)
|
|||
// Make sure we release all handles.
|
||||
destroy();
|
||||
|
||||
log("Win32 - Could not create GL context: %s", Win32GetMessage(GetLastError()));
|
||||
Log::warn("Win32 - Could not create GL context: %s", Win32GetMessage(GetLastError()));
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@ bool Win32MsgBuffer::postMessage(MSG msg)
|
|||
messages[index++] = msg;
|
||||
return true;
|
||||
}
|
||||
log("Win32MsgBuffer: Queue overflow\n");
|
||||
Log::warn("Win32MsgBuffer: Queue overflow\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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