Skip to content

Commit

Permalink
LogV semantically compresses verbose logging.
Browse files Browse the repository at this point in the history
  • Loading branch information
NatWeiss committed Jun 18, 2016
1 parent 1eac486 commit d248fa7
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 29 deletions.
61 changes: 35 additions & 26 deletions EntityFu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ using namespace std;
#define kTrustPointers 0

/// Log macro.
#ifndef Log
#ifndef LogV
#ifndef NDEBUG
#include <stdio.h>
#define Log(...) {printf(__VA_ARGS__); printf("\n");}
#define LogV(verbosity, minVerbosity, ...) do {if ((verbosity) >= (minVerbosity)) {printf(__VA_ARGS__); printf("\n");}} while (0)
#else
#define Log(...) do {} while (0)
#define LogV(...) do {} while (0)
#endif
#endif

Expand All @@ -41,20 +41,17 @@ static vector<Eid>* componentEids = nullptr;

static void log(Cid cid)
{
if (verbosity < 4)
return;
auto n = Entity::count(cid);
auto& eids = Entity::getAll(cid);
if (eids.size() > 0)
{Log(" Cid %u has %d entities ranging from %u to %u", cid, n, eids.front(), eids.back());}
LogV(verbosity, 4, " Cid %u has %d entities ranging from %u to %u", cid, n, eids.front(), eids.back());
}

void Entity::alloc()
{
if (components != nullptr)
return;
if (verbosity >= 1)
{Log("Allocing entities");}
LogV(verbosity, 1, "Allocing entities");

// allocate entities
entities = new bool[kMaxEntities];
Expand All @@ -78,8 +75,7 @@ void Entity::alloc()

void Entity::dealloc()
{
if (verbosity >= 1)
{Log("Deallocing entities");}
LogV(verbosity, 1, "Deallocing entities");

if (components != nullptr)
{
Expand Down Expand Up @@ -119,8 +115,7 @@ Eid Entity::create()
else
{
entities[eid] = true;
if (verbosity >= 1)
{Log("Entity %u created", eid);}
LogV(verbosity, 1, "Entity %u created", eid);
}

return eid;
Expand All @@ -130,8 +125,7 @@ void Entity::destroyNow(Eid eid)
{
if (eid == 0)
return;
if (verbosity >= 1)
{Log("Entity %u being destroyed", eid);}
LogV(verbosity, 1, "Entity %u being destroyed", eid);

for (Cid cid = 0; cid < Component::numCids; cid++)
Entity::removeComponent(cid, eid);
Expand All @@ -152,8 +146,7 @@ void Entity::destroyAll()
}
}
verbosity = oldVerbosity;
if (verbosity >= 1)
{Log("%u entities destroyed", count);}
LogV(verbosity, 1, "%u entities destroyed", count);
}

void Entity::addComponent(Cid cid, Eid eid, Component* c)
Expand All @@ -166,10 +159,9 @@ void Entity::addComponent(Cid cid, Eid eid, Component* c)
return;
}

log(cid);

if (verbosity >= 3)
{Log(" Adding component cid %u eid %u (%x)", cid, eid, (int)(long)c);}
if (verbosity >= 4)
log(cid);
LogV(verbosity, 3, " Adding component cid %u eid %u (%x)", cid, eid, (int)(long)c);

// if component already added, delete old one
if (components[cid][eid] != nullptr)
Expand All @@ -182,7 +174,8 @@ void Entity::addComponent(Cid cid, Eid eid, Component* c)
// store component eids
componentEids[cid].push_back(eid);

log(cid);
if (verbosity >= 4)
log(cid);
}

void Entity::removeComponent(Cid cid, Eid eid)
Expand All @@ -198,10 +191,9 @@ void Entity::removeComponent(Cid cid, Eid eid)
if (ptr == nullptr)
return;

log(cid);

if (verbosity >= 3)
{Log(" Removing component cid %u eid %u (%x)", cid, eid, (int)(long)ptr);}
if (verbosity >= 4)
log(cid);
LogV(verbosity, 3, " Removing component cid %u eid %u (%x)", cid, eid, (int)(long)ptr);

// pointers to components are deleted
delete ptr;
Expand All @@ -215,7 +207,8 @@ void Entity::removeComponent(Cid cid, Eid eid)
if (it != eids.end())
it = eids.erase(it);

log(cid);
if (verbosity >= 4)
log(cid);
}

Entity::Component* Entity::getComponent(Cid cid, Eid eid)
Expand Down Expand Up @@ -266,9 +259,25 @@ bool Entity::exists(Eid eid)
// Entity::Component
//

Entity::Component::~Component()
{
}

bool Entity::Component::full() const
{
return !this->empty();
}

//
// System
//

void System::tick(double fixedDelta)
{
}

void System::animate(double delta, double tickPercent)
{
}


6 changes: 3 additions & 3 deletions EntityFu.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ namespace Entity
///
struct Entity::Component
{
virtual ~Component() {}
virtual ~Component();
virtual bool empty() const = 0;
virtual bool full() const;
static Cid numCids;
Expand Down Expand Up @@ -157,8 +157,8 @@ class System
public:
struct Ent;

static void tick(double fixedDelta) {}
static void animate(double delta, double tickPercent) {}
static void tick(double fixedDelta);
static void animate(double delta, double tickPercent);
};


0 comments on commit d248fa7

Please sign in to comment.