From 1e5624d551af2aedce22626251908daab43fb160 Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Sun, 13 Mar 2011 00:22:45 +0000 Subject: [PATCH] core: create a logger class to reduce printf usage. --- src/Makefile.am | 2 +- src/core.h | 1 + src/dict.cpp | 2 +- src/eglapp.cpp | 14 ++++---- src/entity.cpp | 6 ++-- src/image.cpp | 4 +-- src/log.cpp | 86 +++++++++++++++++++++++++++++++++++++++++++++++++ src/log.h | 41 +++++++++++++++++++++++ src/map.cpp | 4 +-- src/matrix.cpp | 31 +++++------------- src/sample.cpp | 2 +- src/sdlapp.cpp | 4 +-- src/shader.cpp | 4 +-- src/ticker.cpp | 38 +++++++++++----------- src/tiler.cpp | 8 ++--- 15 files changed, 180 insertions(+), 67 deletions(-) create mode 100644 src/log.cpp create mode 100644 src/log.h diff --git a/src/Makefile.am b/src/Makefile.am index 292e3dd3..a485b3c4 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -5,7 +5,7 @@ liblol_a_SOURCES = \ core.h matrix.cpp matrix.h tiler.cpp tiler.h dict.cpp dict.h \ audio.cpp audio.h scene.cpp scene.h font.cpp font.h layer.cpp layer.h \ map.cpp map.h entity.cpp entity.h ticker.cpp ticker.h lolgl.h \ - tileset.cpp tileset.h forge.cpp forge.h video.cpp video.h \ + tileset.cpp tileset.h forge.cpp forge.h video.cpp video.h log.cpp log.h \ timer.cpp timer.h bitfield.h profiler.cpp profiler.h input.h input.cpp \ world.cpp world.h sample.cpp sample.h sampler.cpp sampler.h \ text.cpp text.h emitter.cpp emitter.h numeric.h hash.cpp hash.h \ diff --git a/src/core.h b/src/core.h index 5cb696eb..1e70d391 100644 --- a/src/core.h +++ b/src/core.h @@ -22,6 +22,7 @@ #include "timer.h" // Static classes +#include "log.h" #include "video.h" #include "audio.h" #include "scene.h" diff --git a/src/dict.cpp b/src/dict.cpp index cbc4933d..509d6718 100644 --- a/src/dict.cpp +++ b/src/dict.cpp @@ -46,7 +46,7 @@ public: { #if !LOL_RELEASE if (nentities) - fprintf(stderr, "ERROR: still %i entities in dict\n", nentities); + Log::Error("still %i entities in dict\n", nentities); #endif free(entities); } diff --git a/src/eglapp.cpp b/src/eglapp.cpp index a118cac4..aff60227 100644 --- a/src/eglapp.cpp +++ b/src/eglapp.cpp @@ -57,7 +57,7 @@ EglApp::EglApp(char const *title, vec2i res, float fps) : data->dpy = XOpenDisplay(NULL); if (data->dpy == NULL) { - fprintf(stderr, "Cannot connect to X server\n"); + Log::Error("cannot connect to X server\n"); exit(EXIT_FAILURE); } @@ -86,13 +86,13 @@ EglApp::EglApp(char const *title, vec2i res, float fps) : data->egl_dpy = eglGetDisplay((EGLNativeDisplayType)data->dpy); if (data->egl_dpy == EGL_NO_DISPLAY) { - fprintf(stderr, "Cannot get EGL display\n"); + Log::Error("cannot get EGL display\n"); exit(EXIT_FAILURE); } if (!eglInitialize(data->egl_dpy, NULL, NULL)) { - fprintf(stderr, "Cannot initialize EGL\n"); + Log::Error("cannot initialize EGL\n"); exit(EXIT_FAILURE); } @@ -112,13 +112,13 @@ EglApp::EglApp(char const *title, vec2i res, float fps) : EGLint num_config; if (!eglChooseConfig(data->egl_dpy, attr, &ecfg, 1, &num_config)) { - fprintf(stderr, "Cannot choose EGL config (%i)\n", eglGetError()); + Log::Error("cannot choose EGL config (%i)\n", eglGetError()); exit(EXIT_FAILURE); } if (num_config != 1) { - fprintf(stderr, "Cannot choose between %i EGL configs\n", num_config); + Log::Error("cannot choose between %i EGL configs\n", num_config); exit(EXIT_FAILURE); } @@ -126,7 +126,7 @@ EglApp::EglApp(char const *title, vec2i res, float fps) : data->win, NULL); if (data->egl_surf == EGL_NO_SURFACE) { - fprintf(stderr, "Cannot create EGL surface (%i)\n", eglGetError()); + Log::Error("cannot create EGL surface (%i)\n", eglGetError()); exit(EXIT_FAILURE); } @@ -143,7 +143,7 @@ EglApp::EglApp(char const *title, vec2i res, float fps) : EGL_NO_CONTEXT, ctxattr); if (data->egl_ctx == EGL_NO_CONTEXT) { - fprintf(stderr, "Cannot create EGL context (%i)\n", eglGetError()); + Log::Error("cannot create EGL context (%i)\n", eglGetError()); exit(EXIT_FAILURE); } diff --git a/src/entity.cpp b/src/entity.cpp index 85560149..5902f169 100644 --- a/src/entity.cpp +++ b/src/entity.cpp @@ -42,7 +42,7 @@ Entity::~Entity() { #if !LOL_RELEASE if (!destroy) - fprintf(stderr, "ERROR: entity destructor called directly\n"); + Log::Error("entity destructor called directly\n"); #endif } @@ -55,7 +55,7 @@ void Entity::TickGame(float deltams) { #if !LOL_RELEASE if (state != STATE_PRETICK_GAME) - fprintf(stderr, "ERROR: invalid entity game tick\n"); + Log::Error("invalid entity game tick\n"); state = STATE_POSTTICK_GAME; #endif } @@ -64,7 +64,7 @@ void Entity::TickDraw(float deltams) { #if !LOL_RELEASE if (state != STATE_PRETICK_DRAW) - fprintf(stderr, "ERROR: invalid entity draw tick\n"); + Log::Error("invalid entity draw tick\n"); state = STATE_POSTTICK_DRAW; #endif } diff --git a/src/image.cpp b/src/image.cpp index a77cb065..ba6e87e5 100644 --- a/src/image.cpp +++ b/src/image.cpp @@ -79,7 +79,7 @@ Image::Image(char const *path) if (!image) { #if !LOL_RELEASE - fprintf(stderr, "ERROR: could not load %s\n", path); + Log::Error("could not load %s\n", path); #endif exit(1); } @@ -109,7 +109,7 @@ Image::Image(char const *path) if (!data->img) { #if !LOL_RELEASE - fprintf(stderr, "ERROR: could not load %s\n", path); + Log::Error("could not load %s\n", path); #endif SDL_Quit(); exit(1); diff --git a/src/log.cpp b/src/log.cpp new file mode 100644 index 00000000..ff749cf7 --- /dev/null +++ b/src/log.cpp @@ -0,0 +1,86 @@ +// +// Lol Engine +// +// Copyright: (c) 2010-2011 Sam Hocevar +// This program is free software; you can redistribute it and/or +// modify it under the terms of the Do What The Fuck You Want To +// Public License, Version 2, as published by Sam Hocevar. See +// http://sam.zoy.org/projects/COPYING.WTFPL for more details. +// + +#if defined HAVE_CONFIG_H +# include "config.h" +#endif + +#include +#include + +#if defined ANDROID_NDK +# include +#else +# include +#endif + +#include "core.h" + +namespace lol +{ + +/* + * Public Log class + */ + +void Log::Debug(char const *fmt, ...) +{ + va_list ap; + va_start(ap, fmt); +#if defined ANDROID_NDK + __android_log_vprint(ANDROID_LOG_DEBUG, "LOL", fmt, ap); +#else + fprintf(stderr, "DEBUG: "); + vfprintf(stderr, fmt, ap); +#endif + va_end(ap); +} + +void Log::Info(char const *fmt, ...) +{ + va_list ap; + va_start(ap, fmt); +#if defined ANDROID_NDK + __android_log_vprint(ANDROID_LOG_INFO, "LOL", fmt, ap); +#else + fprintf(stderr, "INFO: "); + vfprintf(stderr, fmt, ap); +#endif + va_end(ap); +} + +void Log::Warn(char const *fmt, ...) +{ + va_list ap; + va_start(ap, fmt); +#if defined ANDROID_NDK + __android_log_vprint(ANDROID_LOG_WARN, "LOL", fmt, ap); +#else + fprintf(stderr, "WARN: "); + vfprintf(stderr, fmt, ap); +#endif + va_end(ap); +} + +void Log::Error(char const *fmt, ...) +{ + va_list ap; + va_start(ap, fmt); +#if defined ANDROID_NDK + __android_log_vprint(ANDROID_LOG_ERROR, "LOL", fmt, ap); +#else + fprintf(stderr, "ERROR: "); + vfprintf(stderr, fmt, ap); +#endif + va_end(ap); +} + +} /* namespace lol */ + diff --git a/src/log.h b/src/log.h new file mode 100644 index 00000000..cffa93d8 --- /dev/null +++ b/src/log.h @@ -0,0 +1,41 @@ +// +// Lol Engine +// +// Copyright: (c) 2010-2011 Sam Hocevar +// This program is free software; you can redistribute it and/or +// modify it under the terms of the Do What The Fuck You Want To +// Public License, Version 2, as published by Sam Hocevar. See +// http://sam.zoy.org/projects/COPYING.WTFPL for more details. +// + +// +// The Log interface +// ----------------- +// The central logging system. +// + +#if !defined __DH_LOG_H__ +#define __DH_LOG_H__ + +#include + +namespace lol +{ + +class Log +{ +public: +#ifdef __GNUC__ +# define LOL_FMT_ATTR __attribute__((format(printf, 1, 2))) +#endif + static void Debug(char const *format, ...) LOL_FMT_ATTR; + static void Info(char const *format, ...) LOL_FMT_ATTR; + static void Warn(char const *format, ...) LOL_FMT_ATTR; + static void Error(char const *format, ...) LOL_FMT_ATTR; +#undef LOL_FMT_ATTR +}; + +} /* namespace lol */ + +#endif // __DH_LOG_H__ + diff --git a/src/map.cpp b/src/map.cpp index 19c38a0b..d89c463b 100644 --- a/src/map.cpp +++ b/src/map.cpp @@ -119,7 +119,7 @@ Map::Map(char const *path) data->layers[data->nlayers] = l; data->nlayers++; tiles = NULL; - //fprintf(stderr, "new layer %ix%i\n", data->width, data->height); + //Log::Debug("new layer %ix%i\n", data->width, data->height); } } else if (sscanf(tmp, " tilers[data->ntilers] = Tiler::Register(str, 32, 0, sqrtf(2)); data->ntilers++; - //fprintf(stderr, "new tiler %s\n", str); + //Log::Debug("new tiler %s\n", str); } else if (sscanf(tmp, " -#endif - #include #include /* free() */ #include /* strdup() */ @@ -73,25 +69,14 @@ template<> void mat4::printf() const { mat4 const &p = *this; -#ifdef ANDROID_NDK - __android_log_print(ANDROID_LOG_INFO, "LOL", - "[ %6.6f %6.6f %6.6f %6.6f", p[0][0], p[1][0], p[2][0], p[3][0]); - __android_log_print(ANDROID_LOG_INFO, "LOL", - " %6.6f %6.6f %6.6f %6.6f", p[0][1], p[1][1], p[2][1], p[3][1]); - __android_log_print(ANDROID_LOG_INFO, "LOL", - " %6.6f %6.6f %6.6f %6.6f", p[0][2], p[1][2], p[2][2], p[3][2]); - __android_log_print(ANDROID_LOG_INFO, "LOL", - " %6.6f %6.6f %6.6f %6.6f ]", p[0][3], p[1][3], p[2][3], p[3][3]); -#else - fprintf(stderr, "[ %6.6f %6.6f %6.6f %6.6f\n", - p[0][0], p[1][0], p[2][0], p[3][0]); - fprintf(stderr, " %6.6f %6.6f %6.6f %6.6f\n", - p[0][1], p[1][1], p[2][1], p[3][1]); - fprintf(stderr, " %6.6f %6.6f %6.6f %6.6f\n", - p[0][2], p[1][2], p[2][2], p[3][2]); - fprintf(stderr, " %6.6f %6.6f %6.6f %6.6f ]\n", - p[0][3], p[1][3], p[2][3], p[3][3]); -#endif + Log::Debug("[ %6.6f %6.6f %6.6f %6.6f\n", + p[0][0], p[1][0], p[2][0], p[3][0]); + Log::Debug(" %6.6f %6.6f %6.6f %6.6f\n", + p[0][1], p[1][1], p[2][1], p[3][1]); + Log::Debug(" %6.6f %6.6f %6.6f %6.6f\n", + p[0][2], p[1][2], p[2][2], p[3][2]); + Log::Debug(" %6.6f %6.6f %6.6f %6.6f ]\n", + p[0][3], p[1][3], p[2][3], p[3][3]); } template<> mat4 mat4::ortho(float left, float right, float bottom, diff --git a/src/sample.cpp b/src/sample.cpp index dd0dd668..22c6eff0 100644 --- a/src/sample.cpp +++ b/src/sample.cpp @@ -58,7 +58,7 @@ Sample::Sample(char const *path) if (!data->chunk) { #if !LOL_RELEASE - fprintf(stderr, "ERROR: could not load %s\n", path); + Log::Error("could not load %s\n", path); #endif SDL_Quit(); exit(1); diff --git a/src/sdlapp.cpp b/src/sdlapp.cpp index 10ff6b06..352f19e4 100644 --- a/src/sdlapp.cpp +++ b/src/sdlapp.cpp @@ -45,7 +45,7 @@ SdlApp::SdlApp(char const *title, vec2i res, float fps) : /* Initialise SDL */ if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0) { - fprintf(stderr, "Cannot initialise SDL: %s\n", SDL_GetError()); + Log::Error("cannot initialise SDL: %s\n", SDL_GetError()); exit(EXIT_FAILURE); } @@ -54,7 +54,7 @@ SdlApp::SdlApp(char const *title, vec2i res, float fps) : SDL_Surface *video = SDL_SetVideoMode(res.x, res.y, 0, SDL_OPENGL); if (!video) { - fprintf(stderr, "Cannot create OpenGL screen: %s\n", SDL_GetError()); + Log::Error("cannot create OpenGL screen: %s\n", SDL_GetError()); SDL_Quit(); exit(EXIT_FAILURE); } diff --git a/src/shader.cpp b/src/shader.cpp index e8f8f402..f44f5749 100644 --- a/src/shader.cpp +++ b/src/shader.cpp @@ -92,7 +92,7 @@ Shader::Shader(char const *vert, char const *frag) glGetShaderInfoLog(data->vert_id, sizeof(buf), &len, buf); if (len > 0) - fprintf(stderr, "ERROR: failed to compile vertex shader: %s", buf); + Log::Error("failed to compile vertex shader: %s", buf); #endif data->frag_crc = Hash::Crc32(frag); @@ -103,7 +103,7 @@ Shader::Shader(char const *vert, char const *frag) glGetShaderInfoLog(data->frag_id, sizeof(buf), &len, buf); if (len > 0) - fprintf(stderr, "ERROR: failed to compile fragment shader: %s", buf); + Log::Error("failed to compile fragment shader: %s", buf); data->prog_id = glCreateProgram(); glAttachShader(data->prog_id, data->vert_id); diff --git a/src/ticker.cpp b/src/ticker.cpp index 2929f865..5b7acbcf 100644 --- a/src/ticker.cpp +++ b/src/ticker.cpp @@ -44,15 +44,15 @@ public: { #if !LOL_RELEASE if (nentities) - fprintf(stderr, "ERROR: still %i entities in ticker\n", nentities); + Log::Error("still %i entities in ticker\n", nentities); if (autolist) { int count = 0; for (Entity *e = autolist; e; e = e->autonext, count++) ; - fprintf(stderr, "ERROR: still %i autoreleased entities\n", count); + Log::Error("still %i autoreleased entities\n", count); } - fprintf(stderr, "INFO: %i frames required to quit\n", + Log::Debug("%i frames required to quit\n", frame - quitframe); #endif } @@ -100,11 +100,11 @@ void Ticker::Ref(Entity *entity) #if !LOL_RELEASE if (!entity) { - fprintf(stderr, "ERROR: refing NULL entity\n"); + Log::Error("referencing NULL entity\n"); return; } if (entity->destroy) - fprintf(stderr, "ERROR: refing entity scheduled for destruction\n"); + Log::Error("referencing entity scheduled for destruction\n"); #endif if (entity->autorelease) { @@ -131,13 +131,13 @@ int Ticker::Unref(Entity *entity) #if !LOL_RELEASE if (!entity) { - fprintf(stderr, "ERROR: dereferencing NULL entity\n"); + Log::Error("dereferencing NULL entity\n"); return 0; } if (entity->ref <= 0) - fprintf(stderr, "ERROR: dereferencing unreferenced entity\n"); + Log::Error("dereferencing unreferenced entity\n"); if (entity->autorelease) - fprintf(stderr, "ERROR: dereferencing autoreleased entity\n"); + Log::Error("dereferencing autoreleased entity\n"); #endif return --entity->ref; } @@ -155,15 +155,15 @@ void Ticker::TickGame() Profiler::Start(Profiler::STAT_TICK_GAME); #if 0 - fprintf(stderr, "-------------------------------------\n"); + Log::Debug("-------------------------------------\n"); for (int i = 0; i < Entity::ALLGROUP_END; i++) { - fprintf(stderr, "%s Group %i\n", - (i < Entity::GAMEGROUP_END) ? "Game" : "Draw", i); + Log::Debug("%s Group %i\n", + (i < Entity::GAMEGROUP_END) ? "Game" : "Draw", i); for (Entity *e = data->list[i]; e; ) { - fprintf(stderr, " \\-- %s (ref %i, destroy %i)\n", e->GetName(), e->ref, e->destroy); + Log::Debug(" \\-- %s (ref %i, destroy %i)\n", e->GetName(), e->ref, e->destroy); e = (i < Entity::GAMEGROUP_END) ? e->gamenext : e->drawnext; } } @@ -196,7 +196,7 @@ void Ticker::TickGame() if (e->ref) { #if !LOL_RELEASE - fprintf(stderr, "ERROR: poking %s\n", e->GetName()); + Log::Error("poking %s\n", e->GetName()); #endif e->ref--; n++; @@ -204,8 +204,8 @@ void Ticker::TickGame() #if !LOL_RELEASE if (n) - fprintf(stderr, "ERROR: %i entities stuck after %i frames, " - "poked %i\n", data->nentities, data->quitdelay, n); + Log::Error("%i entities stuck after %i frames, poked %i\n", + data->nentities, data->quitdelay, n); #endif data->quitdelay = data->quitdelay > 1 ? data->quitdelay / 2 : 1; @@ -265,13 +265,13 @@ void Ticker::TickGame() { #if !LOL_RELEASE if (e->state != Entity::STATE_IDLE) - fprintf(stderr, "ERROR: entity not idle for game tick\n"); + Log::Error("entity not idle for game tick\n"); e->state = Entity::STATE_PRETICK_GAME; #endif e->TickGame(data->deltams); #if !LOL_RELEASE if (e->state != Entity::STATE_POSTTICK_GAME) - fprintf(stderr, "ERROR: entity missed super game tick\n"); + Log::Error("entity missed super game tick\n"); e->state = Entity::STATE_IDLE; #endif } @@ -306,13 +306,13 @@ void Ticker::TickDraw() { #if !LOL_RELEASE if (e->state != Entity::STATE_IDLE) - fprintf(stderr, "ERROR: entity not idle for draw tick\n"); + Log::Error("entity not idle for draw tick\n"); e->state = Entity::STATE_PRETICK_DRAW; #endif e->TickDraw(data->deltams); #if !LOL_RELEASE if (e->state != Entity::STATE_POSTTICK_DRAW) - fprintf(stderr, "ERROR: entity missed super draw tick\n"); + Log::Error("entity missed super draw tick\n"); e->state = Entity::STATE_IDLE; #endif } diff --git a/src/tiler.cpp b/src/tiler.cpp index 8b1f8faa..95c837d0 100644 --- a/src/tiler.cpp +++ b/src/tiler.cpp @@ -76,7 +76,7 @@ vec2i Tiler::GetSize(int id) #if !LOL_RELEASE if (!tileset) { - fprintf(stderr, "ERROR: getting size for null tiler #%i\n", id); + Log::Error("getting size for null tiler #%i\n", id); return 0; } #endif @@ -89,7 +89,7 @@ vec2i Tiler::GetCount(int id) #if !LOL_RELEASE if (!tileset) { - fprintf(stderr, "ERROR: getting count for null tiler #%i\n", id); + Log::Error("getting count for null tiler #%i\n", id); return 0; } #endif @@ -105,7 +105,7 @@ void Tiler::Bind(uint32_t code) if (!tileset) { if (id != data->lasterror) - fprintf(stderr, "ERROR: binding null tiler #%i\n", id); + Log::Error("binding null tiler #%i\n", id); data->lasterror = id; return; } @@ -123,7 +123,7 @@ void Tiler::BlitTile(uint32_t code, int x, int y, int z, int o, if (!tileset) { if (id != data->lasterror) - fprintf(stderr, "ERROR: blitting to null tiler #%i\n", id); + Log::Error("blitting to null tiler #%i\n", id); data->lasterror = id; return; }