Skip to content

Commit 02e3a1d

Browse files
committed
feat: add logging functionality to Container and System classes
1 parent e60c99c commit 02e3a1d

4 files changed

Lines changed: 47 additions & 12 deletions

File tree

include/libecs-cpp/Container.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <thread>
88
#include <memory>
99
#include <iostream>
10+
#include <functional>
1011
#include <libecs-cpp/json.hpp>
1112
#include <libecs-cpp/Resource.hpp>
1213
#include <libecs-cpp/Component.hpp>
@@ -45,8 +46,11 @@ namespace ecs
4546
ecs::TypeEntityComponentList Components;
4647
ecs::Uuid UuidGet();
4748
std::unordered_map<std::string, std::unique_ptr<ecs::System>> Systems;
49+
void Log(const std::string &message, const std::string &level = "info");
50+
void LoggerSet(std::function<void(const std::string &, const std::string &)> fn);
4851

4952
private:
53+
std::function<void(const std::string &, const std::string &)> logger;
5054
std::vector<std::string> system_order_;
5155
/*! Number of microseconds to sleep between Update() calls */
5256
uint32_t sleepInterval = 1000000 / 30;

include/libecs-cpp/System.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ namespace ecs
6060
uint32_t DeltaTimeGet();
6161
void TimerClear(const std::string &name);
6262
void TimerAdd(Timer timer);
63+
void Log(const std::string &message, const std::string &level);
6364
protected:
6465
std::queue<nlohmann::json> messages;
6566
std::chrono::steady_clock::time_point lastTime = std::chrono::steady_clock::now();

src/Container.cpp

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,36 @@
55
#include <chrono>
66
#include <iostream>
77

8+
auto loggerFunction = [](const std::string &message, const std::string &level) {
9+
if(level == "error")
10+
{
11+
std::cerr << "\033[91m[" << level << "]\033[0m " << message << std::endl;
12+
return;
13+
}
14+
if(level == "warning")
15+
{
16+
std::cerr << "\033[93m[" << level << "]\033[0m " << message << std::endl;
17+
return;
18+
}
19+
if(level == "debug")
20+
{
21+
std::cout << "\033[97m[" << level << "]\033[0m " << message << std::endl;
22+
return;
23+
}
24+
std::cout << "\033[92m[" << level << "]\033[0m " << message << std::endl;
25+
};
826
namespace ecs
927
{
1028
Container::Container(ecs::Manager *manager):
1129
Manager(manager), Handle(ecs::Uuid().Get())
1230
{
31+
this->logger = loggerFunction;
1332
}
1433

1534
Container::Container(ecs::Manager *manager, const std::string &handle):
1635
Manager(manager), Handle(handle)
1736
{
37+
this->logger = loggerFunction;
1838
}
1939

2040
Container::~Container()
@@ -105,16 +125,12 @@ namespace ecs
105125
}
106126
catch(const std::exception &e)
107127
{
108-
std::cerr << "ecs::Container(\"" << this->Handle
109-
<< "\")::SystemsInitialize(): System \"" << handle
110-
<< "\" threw during Initialize(): " << e.what() << std::endl;
128+
this->Log("[" + handle + "] threw during Initialize(): " + e.what(), "error");
111129
this->disabledSystems.insert(handle);
112130
}
113131
catch(...)
114132
{
115-
std::cerr << "ecs::Container(\"" << this->Handle
116-
<< "\")::SystemsInitialize(): System \"" << handle
117-
<< "\" threw unknown exception during Initialize()" << std::endl;
133+
this->Log("[" + handle + "] threw unknown exception during Initialize()", "error");
118134
this->disabledSystems.insert(handle);
119135
}
120136
}
@@ -145,16 +161,12 @@ namespace ecs
145161
}
146162
catch(const std::exception &e)
147163
{
148-
std::cerr << "ecs::Container(\"" << this->Handle
149-
<< "\")::Update(): System \"" << handle
150-
<< "\" threw during Update(): " << e.what() << std::endl;
164+
this->Log("[" + handle + "] threw during Update(): " + e.what(), "error");
151165
this->disabledSystems.insert(handle);
152166
}
153167
catch(...)
154168
{
155-
std::cerr << "ecs::Container(\"" << this->Handle
156-
<< "\")::Update(): System \"" << handle
157-
<< "\" threw unknown exception during Update()" << std::endl;
169+
this->Log("[" + handle + "] threw unknown exception during Update()", "error");
158170
this->disabledSystems.insert(handle);
159171
}
160172
}
@@ -218,4 +230,17 @@ namespace ecs
218230
{
219231
return ecs::Uuid();
220232
}
233+
234+
void Container::Log(const std::string &message, const std::string &level)
235+
{
236+
if(this->logger)
237+
{
238+
this->logger(message, level);
239+
}
240+
}
241+
242+
void Container::LoggerSet(std::function<void(const std::string &, const std::string &)> fn)
243+
{
244+
this->logger = std::move(fn);
245+
}
221246
}

src/System.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,9 @@ namespace ecs
8787
{
8888
this->timers.push_back(timer);
8989
}
90+
91+
void System::Log(const std::string &message, const std::string &level)
92+
{
93+
this->Container->Log("[" + this->Handle + "] " + message, level);
94+
}
9095
}

0 commit comments

Comments
 (0)