Browse Source

core: create a logger class to reduce printf usage.

legacy
Sam Hocevar sam 13 years ago
parent
commit
1e5624d551
15 changed files with 180 additions and 67 deletions
  1. +1
    -1
      src/Makefile.am
  2. +1
    -0
      src/core.h
  3. +1
    -1
      src/dict.cpp
  4. +7
    -7
      src/eglapp.cpp
  5. +3
    -3
      src/entity.cpp
  6. +2
    -2
      src/image.cpp
  7. +86
    -0
      src/log.cpp
  8. +41
    -0
      src/log.h
  9. +2
    -2
      src/map.cpp
  10. +8
    -23
      src/matrix.cpp
  11. +1
    -1
      src/sample.cpp
  12. +2
    -2
      src/sdlapp.cpp
  13. +2
    -2
      src/shader.cpp
  14. +19
    -19
      src/ticker.cpp
  15. +4
    -4
      src/tiler.cpp

+ 1
- 1
src/Makefile.am View File

@@ -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 \


+ 1
- 0
src/core.h View File

@@ -22,6 +22,7 @@
#include "timer.h"

// Static classes
#include "log.h"
#include "video.h"
#include "audio.h"
#include "scene.h"


+ 1
- 1
src/dict.cpp View File

@@ -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);
}


+ 7
- 7
src/eglapp.cpp View File

@@ -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);
}



+ 3
- 3
src/entity.cpp View File

@@ -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
}


+ 2
- 2
src/image.cpp View File

@@ -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);


+ 86
- 0
src/log.cpp View File

@@ -0,0 +1,86 @@
//
// Lol Engine
//
// Copyright: (c) 2010-2011 Sam Hocevar <sam@hocevar.net>
// 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 <cmath>
#include <cstdio>

#if defined ANDROID_NDK
# include <android/log.h>
#else
# include <cstdarg>
#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 */


+ 41
- 0
src/log.h View File

@@ -0,0 +1,41 @@
//
// Lol Engine
//
// Copyright: (c) 2010-2011 Sam Hocevar <sam@hocevar.net>
// 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 <stdint.h>

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__


+ 2
- 2
src/map.cpp View File

@@ -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, " <tileset firstgid=\"%i\"", &i) == 1)
@@ -133,7 +133,7 @@ Map::Map(char const *path)
data->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, " <layer name=\"%c%i%c%*[^\"]\" "
"width=\"%i\" height=\"%i\"", &a, &i, &b, &j, &k) == 5)


+ 8
- 23
src/matrix.cpp View File

@@ -12,10 +12,6 @@
# include "config.h"
#endif

#ifdef ANDROID_NDK
# include <android/log.h>
#endif

#include <cstdio>
#include <cstdlib> /* free() */
#include <cstring> /* 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,


+ 1
- 1
src/sample.cpp View File

@@ -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);


+ 2
- 2
src/sdlapp.cpp View File

@@ -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);
}


+ 2
- 2
src/shader.cpp View File

@@ -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);


+ 19
- 19
src/ticker.cpp View File

@@ -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
}


+ 4
- 4
src/tiler.cpp View File

@@ -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;
}


Loading…
Cancel
Save