From 631f0cfd6b421f5be68d60375db2ce91176d923d Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Mon, 4 Dec 2017 12:26:15 +0100 Subject: [PATCH] standardized logging --- src/blobs.cpp | 2 +- src/creature/creature.cpp | 22 ++++++++++------------ src/creature/goal.cpp | 3 +-- src/world/Body.hpp | 1 + src/world/Simulation.hpp | 3 +++ src/world/sim.cpp | 27 +++++++++++++++++++++++++++ 6 files changed, 43 insertions(+), 15 deletions(-) diff --git a/src/blobs.cpp b/src/blobs.cpp index d6a6fed..01bfb2a 100644 --- a/src/blobs.cpp +++ b/src/blobs.cpp @@ -24,7 +24,7 @@ struct SwitchPanel { void operator ()(creature::Creature &c) { if (planet.Creatures().empty()) { - std::cout << "no more creatures, game over" << std::endl; + planet.GetSimulation().Log() << "no more creatures, game over" << std::endl; state.GetCreaturePanel().Hide(); while (app.HasState()) { app.PopState(); diff --git a/src/creature/creature.cpp b/src/creature/creature.cpp index 1a88dba..376ad3f 100644 --- a/src/creature/creature.cpp +++ b/src/creature/creature.cpp @@ -209,21 +209,21 @@ void Creature::DoWork(double amount) noexcept { void Creature::Hurt(double amount) noexcept { stats.Damage().Add(amount); if (stats.Damage().Full()) { - std::cout << "[" << ui::TimeString(sim.Time()) << "] " << name << " "; + std::ostream &log = sim.Log() << name << " "; if (stats.Exhaustion().Full()) { - std::cout << "died of exhaustion"; + log << "died of exhaustion"; } else if (stats.Breath().Full()) { - std::cout << "suffocated"; + log << "suffocated"; } else if (stats.Thirst().Full()) { - std::cout << "died of thirst"; + log << "died of thirst"; } else if (stats.Hunger().Full()) { - std::cout << "starved to death"; + log << "starved to death"; } else { - std::cout << "succumed to wounds"; + log << "succumed to wounds"; } - std::cout << " at an age of " << ui::TimeString(Age()) + log << " at an age of " << ui::TimeString(Age()) << " (" << ui::PercentageString(Age() / properties.Lifetime()) - << "% of life expectancy of " << ui::TimeString(properties.Lifetime()) + << " of life expectancy of " << ui::TimeString(properties.Lifetime()) << ")" << std::endl; Die(); } @@ -697,8 +697,7 @@ void Split(Creature &c) { s.GetPlanet(), s.Surface(), s.Position() + glm::dvec3(0.0, 0.55 * a->Size(), 0.0)); a->BuildVAO(); - std::cout << "[" << ui::TimeString(c.GetSimulation().Time()) << "] " - << a->Name() << " was born" << std::endl; + c.GetSimulation().Log() << a->Name() << " was born" << std::endl; Creature *b = new Creature(c.GetSimulation()); b->AddParent(c); @@ -712,8 +711,7 @@ void Split(Creature &c) { s.GetPlanet(), s.Surface(), s.Position() - glm::dvec3(0.0, 0.55 * b->Size(), 0.0)); b->BuildVAO(); - std::cout << "[" << ui::TimeString(c.GetSimulation().Time()) << "] " - << b->Name() << " was born" << std::endl; + c.GetSimulation().Log() << b->Name() << " was born" << std::endl; c.Die(); } diff --git a/src/creature/goal.cpp b/src/creature/goal.cpp index a4f3330..f20ecc5 100644 --- a/src/creature/goal.cpp +++ b/src/creature/goal.cpp @@ -100,8 +100,7 @@ void BlobBackgroundTask::CheckStats() { void BlobBackgroundTask::CheckSplit() { if (GetCreature().Mass() > GetCreature().OffspringMass() * 2.0 && GetCreature().OffspringChance() > Assets().random.UNorm()) { - std::cout << "[" << ui::TimeString(GetCreature().GetSimulation().Time()) - << "] " << GetCreature().Name() << " split" << std::endl; + GetCreature().GetSimulation().Log() << GetCreature().Name() << " split" << std::endl; Split(GetCreature()); return; } diff --git a/src/world/Body.hpp b/src/world/Body.hpp index 5ac4fa1..165d848 100644 --- a/src/world/Body.hpp +++ b/src/world/Body.hpp @@ -35,6 +35,7 @@ public: public: bool HasSimulation() const noexcept { return sim; } + Simulation &GetSimulation() noexcept { return *sim; } const Simulation &GetSimulation() const noexcept { return *sim; } void SetSimulation(Simulation &) noexcept; diff --git a/src/world/Simulation.hpp b/src/world/Simulation.hpp index 5df595f..1902340 100644 --- a/src/world/Simulation.hpp +++ b/src/world/Simulation.hpp @@ -5,6 +5,7 @@ #include "Set.hpp" #include "../app/Assets.hpp" +#include #include #include @@ -65,6 +66,8 @@ public: const std::vector &Records() const noexcept { return records; } void CheckRecords(creature::Creature &) noexcept; + std::ostream &Log(); + private: Body &root; app::Assets &assets; diff --git a/src/world/sim.cpp b/src/world/sim.cpp index edf7bfe..1d9706e 100644 --- a/src/world/sim.cpp +++ b/src/world/sim.cpp @@ -4,8 +4,10 @@ #include "Planet.hpp" #include "Sun.hpp" #include "../creature/Creature.hpp" +#include "../ui/string.hpp" #include +#include namespace blobs { @@ -83,41 +85,66 @@ void Simulation::SetDead(creature::Creature *c) { void Simulation::CheckRecords(creature::Creature &c) noexcept { if (c.Age() > records[0].value) { + if (records[0].holder && records[0].holder != &c) { + Log() << "new age record by " << c.Name() << std::endl; + } records[0].value = c.Age(); records[0].time = Time(); records[0].holder = &c; } if (c.Mass() > records[1].value) { + if (records[1].holder && records[1].holder != &c) { + Log() << "new mass record by " << c.Name() << std::endl; + } records[1].value = c.Mass(); records[1].time = Time(); records[1].holder = &c; } if (c.Size() > records[2].value) { + if (records[2].holder && records[2].holder != &c) { + Log() << "new size record by " << c.Name() << std::endl; + } records[2].value = c.Size(); records[2].time = Time(); records[2].holder = &c; } if (c.Strength() > records[3].value) { + if (records[3].holder && records[3].holder != &c) { + Log() << "new strength record by " << c.Name() << std::endl; + } records[3].value = c.Strength(); records[3].time = Time(); records[3].holder = &c; } if (c.Stamina() > records[4].value) { + if (records[4].holder && records[4].holder != &c) { + Log() << "new stamina record by " << c.Name() << std::endl; + } records[4].value = c.Stamina(); records[4].time = Time(); records[4].holder = &c; } if (c.Dexerty() > records[5].value) { + if (records[5].holder && records[5].holder != &c) { + Log() << "new dexerty record by " << c.Name() << std::endl; + } records[5].value = c.Dexerty(); records[5].time = Time(); records[5].holder = &c; } if (c.Intelligence() > records[6].value) { + if (records[6].holder && records[6].holder != &c) { + Log() << "new intelligence record by " << c.Name() << std::endl; + } records[6].value = c.Intelligence(); records[6].time = Time(); records[6].holder = &c; } } +std::ostream &Simulation::Log() { + return std::cout << '[' << ui::TimeString(Time()) << "] "; +} + } } -- 2.39.2