Skip to content

Commit 06225f0

Browse files
authored
Rework logger (#249)
* Enhance logger * Update version * Example logger on stalingrado * Fix colors
1 parent 8bdb063 commit 06225f0

20 files changed

+247
-280
lines changed

examples/stalingrado.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ int main() {
8282
graph.makeSpireStreet(19);
8383
auto& spire = dynamic_cast<SpireStreet&>(*graph.street(19));
8484

85-
std::cout << "Intersections: " << graph.nNodes() << '\n';
86-
std::cout << "Streets: " << graph.nEdges() << '\n';
85+
dsm::logger.info(std::format("Intersections: {}", graph.nNodes()));
86+
dsm::logger.info(std::format("Streets: {}", graph.nEdges()));
8787

8888
// Create the dynamics
8989
Dynamics dynamics{graph, 69, 0.95};

src/dsm/dsm.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
static constexpr uint8_t DSM_VERSION_MAJOR = 2;
88
static constexpr uint8_t DSM_VERSION_MINOR = 3;
9-
static constexpr uint8_t DSM_VERSION_PATCH = 6;
9+
static constexpr uint8_t DSM_VERSION_PATCH = 7;
1010

1111
static auto const DSM_VERSION =
1212
std::format("{}.{}.{}", DSM_VERSION_MAJOR, DSM_VERSION_MINOR, DSM_VERSION_PATCH);

src/dsm/headers/Agent.hpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ namespace dsm {
159159
requires(is_numeric_v<delay_t>)
160160
void Agent<delay_t>::setSpeed(double speed) {
161161
if (speed < 0) {
162-
throw std::invalid_argument(buildLog("Speed must be positive"));
162+
logger.error(std::format("Speed ({}) of agent {} must be positive", speed, m_id));
163163
}
164164
m_speed = speed;
165165
}
@@ -184,23 +184,26 @@ namespace dsm {
184184
requires(is_numeric_v<delay_t>)
185185
void Agent<delay_t>::incrementDelay() {
186186
if (m_delay == std::numeric_limits<delay_t>::max()) {
187-
throw std::overflow_error(buildLog("delay_t has reached its maximum value"));
187+
throw std::overflow_error(
188+
logger.buildExceptionMessage("delay_t has reached its maximum value"));
188189
}
189190
++m_delay;
190191
}
191192
template <typename delay_t>
192193
requires(is_numeric_v<delay_t>)
193194
void Agent<delay_t>::incrementDelay(delay_t const delay) {
194195
if (m_delay + delay < m_delay) {
195-
throw std::overflow_error(buildLog("delay_t has reached its maximum value"));
196+
throw std::overflow_error(
197+
logger.buildExceptionMessage("delay_t has reached its maximum value"));
196198
}
197199
m_delay += delay;
198200
}
199201
template <typename delay_t>
200202
requires(is_numeric_v<delay_t>)
201203
void Agent<delay_t>::decrementDelay() {
202204
if (m_delay == 0) {
203-
throw std::underflow_error(buildLog("delay_t has reached its minimum value"));
205+
throw std::underflow_error(
206+
logger.buildExceptionMessage("delay_t has reached its minimum value"));
204207
}
205208
--m_delay;
206209
}
@@ -209,7 +212,8 @@ namespace dsm {
209212
requires(is_numeric_v<delay_t>)
210213
void Agent<delay_t>::incrementDistance(double distance) {
211214
if (distance < 0) {
212-
throw std::invalid_argument(buildLog("Distance travelled must be positive"));
215+
logger.error(std::format(
216+
"Distance travelled ({}) by agent {} must be positive", distance, m_id));
213217
}
214218
m_distance += distance;
215219
}
@@ -218,15 +222,17 @@ namespace dsm {
218222
requires(is_numeric_v<delay_t>)
219223
void Agent<delay_t>::incrementTime() {
220224
if (m_time == std::numeric_limits<unsigned int>::max()) {
221-
throw std::overflow_error(buildLog("Time has reached its maximum value"));
225+
throw std::overflow_error(
226+
logger.buildExceptionMessage("Time has reached its maximum value"));
222227
}
223228
++m_time;
224229
}
225230
template <typename delay_t>
226231
requires(is_numeric_v<delay_t>)
227232
void Agent<delay_t>::incrementTime(unsigned int const time) {
228233
if (m_time + time < m_time) {
229-
throw std::overflow_error(buildLog("Time has reached its maximum value"));
234+
throw std::overflow_error(
235+
logger.buildExceptionMessage("Time has reached its maximum value"));
230236
}
231237
m_time += time;
232238
}

src/dsm/headers/Dynamics.hpp

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -112,25 +112,17 @@ namespace dsm {
112112
path.insert(nodeId, nextNodeId, true);
113113
}
114114
} else if ((nextNodeId != destinationID)) {
115-
std::cerr << std::format(
116-
"\033[38;2;130;30;180mWARNING ({}:{}): No "
117-
"path found "
118-
"from node {} "
119-
"to node {}\033[0m",
120-
__FILE__,
121-
__LINE__,
122-
nextNodeId,
123-
destinationID)
124-
<< std::endl;
115+
logger.warning(std::format(
116+
"No path found from node {} to node {}", nextNodeId, destinationID));
125117
}
126118
}
127119
}
128120
if (path.size() == 0) {
129-
throw std::runtime_error(
130-
buildLog(std::format("Path with id {} and destination {} is empty. Please "
131-
"check the adjacency matrix.",
132-
pItinerary->id(),
133-
pItinerary->destination())));
121+
logger.error(
122+
std::format("Path with id {} and destination {} is empty. Please "
123+
"check the adjacency matrix.",
124+
pItinerary->id(),
125+
pItinerary->destination()));
134126
}
135127
pItinerary->setPath(path);
136128
}
@@ -307,8 +299,7 @@ namespace dsm {
307299
bool updatePaths) {
308300
for (const auto& nodeId : destinationNodes) {
309301
if (!m_graph.nodeSet().contains(nodeId)) {
310-
throw std::invalid_argument(
311-
buildLog(std::format("Node with id {} not found", nodeId)));
302+
logger.error(std::format("Node with id {} not found", nodeId));
312303
}
313304
this->addItinerary(Itinerary{nodeId, nodeId});
314305
}
@@ -320,13 +311,13 @@ namespace dsm {
320311
template <typename agent_t>
321312
void Dynamics<agent_t>::addAgent(std::unique_ptr<agent_t> agent) {
322313
if (m_agents.size() + 1 > m_graph.maxCapacity()) {
323-
throw std::overflow_error(buildLog(
314+
throw std::overflow_error(logger.buildExceptionMessage(
324315
std::format("Graph is already holding the max possible number of agents ({})",
325316
m_graph.maxCapacity())));
326317
}
327318
if (m_agents.contains(agent->id())) {
328-
throw std::invalid_argument(
329-
buildLog(std::format("Agent with id {} already exists.", agent->id())));
319+
throw std::invalid_argument(logger.buildExceptionMessage(
320+
std::format("Agent with id {} already exists.", agent->id())));
330321
}
331322
m_agents.emplace(agent->id(), std::move(agent));
332323
}

src/dsm/headers/Edge.cpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,30 +19,28 @@ namespace dsm {
1919
m_transportCapacity{transportCapacity},
2020
m_angle{angle} {
2121
if (capacity < 1) {
22-
throw std::invalid_argument(
23-
buildLog(std::format("Edge capacity ({}) must be greater than 0.", capacity)));
22+
logger.error(std::format("Edge capacity ({}) must be greater than 0.", capacity));
2423
}
2524
if (transportCapacity < 1) {
26-
throw std::invalid_argument(buildLog(std::format(
27-
"Edge transport capacity ({}) must be greater than 0.", transportCapacity)));
25+
logger.error(std::format("Edge transport capacity ({}) must be greater than 0.",
26+
transportCapacity));
2827
}
2928
if (std::abs(angle) > 2 * std::numbers::pi) {
30-
throw std::invalid_argument(buildLog(
31-
std::format("Edge angle ({}) must be in the range [-2pi, 2pi].", angle)));
29+
logger.error(
30+
std::format("Edge angle ({}) must be in the range [-2pi, 2pi].", angle));
3231
}
3332
}
3433

3534
void Edge::setCapacity(int capacity) {
3635
if (capacity < 1) {
37-
throw std::invalid_argument(
38-
buildLog(std::format("Edge capacity ({}) must be greater than 0.", capacity)));
36+
logger.error(std::format("Edge capacity ({}) must be greater than 0.", capacity));
3937
}
4038
m_capacity = capacity;
4139
}
4240
void Edge::setTransportCapacity(int capacity) {
4341
if (capacity < 1) {
44-
throw std::invalid_argument(buildLog(
45-
std::format("Edge transport capacity ({}) must be greater than 0.", capacity)));
42+
logger.error(
43+
std::format("Edge transport capacity ({}) must be greater than 0.", capacity));
4644
}
4745
m_transportCapacity = capacity;
4846
}

src/dsm/headers/FirstOrderDynamics.cpp

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,9 @@ namespace dsm {
44
FirstOrderDynamics::FirstOrderDynamics(Graph& graph,
55
std::optional<unsigned int> seed,
66
double alpha)
7-
: RoadDynamics<Delay>(graph, seed), m_alpha{0.}, m_speedFluctuationSTD{0.} {
7+
: RoadDynamics<Delay>(graph, seed), m_alpha{alpha}, m_speedFluctuationSTD{0.} {
88
if (alpha < 0. || alpha > 1.) {
9-
throw std::invalid_argument(buildLog(std::format(
10-
"The minimum speed rateo must be between 0 and 1, but it is {}", alpha)));
11-
} else {
12-
m_alpha = alpha;
9+
logger.error(std::format("The minimum speed rateo ({}) must be in [0, 1[", alpha));
1310
}
1411
double globMaxTimePenalty{0.};
1512
for (const auto& [streetId, street] : this->m_graph.streetSet()) {
@@ -18,11 +15,11 @@ namespace dsm {
1815
std::ceil(street->length() / ((1. - m_alpha) * street->maxSpeed())));
1916
}
2017
if (globMaxTimePenalty > static_cast<double>(std::numeric_limits<Delay>::max())) {
21-
throw std::overflow_error(
22-
buildLog(std::format("The maximum time penalty ({}) is greater than the "
23-
"maximum value of delay_t ({})",
24-
globMaxTimePenalty,
25-
std::numeric_limits<Delay>::max())));
18+
throw std::overflow_error(logger.buildExceptionMessage(
19+
std::format("The maximum time penalty ({}) is greater than the "
20+
"maximum value of delay_t ({})",
21+
globMaxTimePenalty,
22+
std::numeric_limits<Delay>::max())));
2623
}
2724
}
2825

@@ -40,8 +37,9 @@ namespace dsm {
4037

4138
void FirstOrderDynamics::setSpeedFluctuationSTD(double speedFluctuationSTD) {
4239
if (speedFluctuationSTD < 0.) {
43-
throw std::invalid_argument(
44-
buildLog("The speed fluctuation standard deviation must be positive."));
40+
logger.error(
41+
std::format("The speed fluctuation standard deviation ({}) must be positive",
42+
speedFluctuationSTD));
4543
}
4644
m_speedFluctuationSTD = speedFluctuationSTD;
4745
}

0 commit comments

Comments
 (0)