|
5 | 5 | #include <chrono> |
6 | 6 | #include <iostream> |
7 | 7 |
|
| 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 | +}; |
8 | 26 | namespace ecs |
9 | 27 | { |
10 | 28 | Container::Container(ecs::Manager *manager): |
11 | 29 | Manager(manager), Handle(ecs::Uuid().Get()) |
12 | 30 | { |
| 31 | + this->logger = loggerFunction; |
13 | 32 | } |
14 | 33 |
|
15 | 34 | Container::Container(ecs::Manager *manager, const std::string &handle): |
16 | 35 | Manager(manager), Handle(handle) |
17 | 36 | { |
| 37 | + this->logger = loggerFunction; |
18 | 38 | } |
19 | 39 |
|
20 | 40 | Container::~Container() |
@@ -105,16 +125,12 @@ namespace ecs |
105 | 125 | } |
106 | 126 | catch(const std::exception &e) |
107 | 127 | { |
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"); |
111 | 129 | this->disabledSystems.insert(handle); |
112 | 130 | } |
113 | 131 | catch(...) |
114 | 132 | { |
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"); |
118 | 134 | this->disabledSystems.insert(handle); |
119 | 135 | } |
120 | 136 | } |
@@ -145,16 +161,12 @@ namespace ecs |
145 | 161 | } |
146 | 162 | catch(const std::exception &e) |
147 | 163 | { |
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"); |
151 | 165 | this->disabledSystems.insert(handle); |
152 | 166 | } |
153 | 167 | catch(...) |
154 | 168 | { |
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"); |
158 | 170 | this->disabledSystems.insert(handle); |
159 | 171 | } |
160 | 172 | } |
@@ -218,4 +230,17 @@ namespace ecs |
218 | 230 | { |
219 | 231 | return ecs::Uuid(); |
220 | 232 | } |
| 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 | + } |
221 | 246 | } |
0 commit comments