Skip to content

Commit 264a28d

Browse files
committed
utils/logger : fix column iteration order
1 parent 15d2b99 commit 264a28d

File tree

2 files changed

+21
-12
lines changed

2 files changed

+21
-12
lines changed

include/aligator/utils/logger.hpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
/// @file
2-
/// @copyright Copyright (C) 2022-2024 LAAS-CNRS, INRIA
2+
/// @copyright Copyright (C) 2022-2024 LAAS-CNRS, 2022-2025 INRIA
33
#pragma once
44

5+
#include "string-hash.hpp"
6+
57
#include <string_view>
68
#include <boost/unordered_map.hpp>
79

@@ -44,10 +46,12 @@ struct Logger {
4446
void addEntry(std::string_view name, size_t val);
4547

4648
protected:
47-
// sizes and formats
48-
boost::unordered_map<std::string_view, std::pair<uint, std::string>>
49-
m_colSpecs;
50-
boost::unordered_map<std::string_view, std::string> m_currentLine;
49+
std::vector<std::string> m_columnNames; // in insertion order
50+
boost::unordered_map<std::string_view, std::pair<uint, std::string>,
51+
ExtendedStringHash>
52+
m_colSpecs; // column sizes and formats
53+
boost::unordered_map<std::string_view, std::string, ExtendedStringHash>
54+
m_currentLine; // iterate using order
5155
};
5256

5357
} // namespace aligator

src/utils/logger.cpp

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/// @copyright Copyright (C) 2022-2024 LAAS-CNRS, 2022-2025 INRIA
12
#include "aligator/utils/logger.hpp"
23

34
#include <fmt/color.h>
@@ -13,25 +14,26 @@ void Logger::printHeadline() {
1314
return;
1415
std::vector<std::string> formattedCols;
1516
formattedCols.reserve(m_colSpecs.size());
16-
for (const auto &[name, spec] : m_colSpecs) {
17+
for (const auto &name : m_columnNames) {
18+
const auto &spec = m_colSpecs[name];
1719
formattedCols.push_back(fmt::format(fstr, name, spec.first + 1));
1820
}
19-
fmt::print(fmt::emphasis::bold, "{}", fmt::join(formattedCols, ""));
20-
fmt::print("\n");
21+
fmt::print(fmt::emphasis::bold, "{}\n", fmt::join(formattedCols, ""));
2122
}
2223

2324
void Logger::log() {
2425
if (!active)
2526
return;
2627
std::vector<std::string_view> cols;
2728
cols.reserve(m_colSpecs.size());
28-
for (const auto &[name, spec] : m_colSpecs) {
29+
for (const auto &name : m_columnNames) {
2930
cols.emplace_back(m_currentLine[name]);
3031
}
3132
fmt::print("{}\n", fmt::join(cols, ""));
3233
}
3334

3435
void Logger::reset() {
36+
m_columnNames.clear();
3537
m_colSpecs.clear();
3638
m_currentLine.clear();
3739
}
@@ -51,17 +53,20 @@ void Logger::finish(bool conv) {
5153

5254
void Logger::addColumn(std::string_view name, uint width,
5355
std::string_view format) {
54-
m_colSpecs[name] = {width, std::string(format)};
56+
m_columnNames.emplace_back(name);
57+
m_colSpecs.emplace(name, std::pair(width, std::string(format)));
5558
}
5659

5760
void Logger::addEntry(std::string_view name, double val) {
5861
const auto spec = m_colSpecs[name];
59-
m_currentLine[name] = fmt::format(fmt::runtime(spec.second), val, spec.first);
62+
m_currentLine.insert_or_assign(
63+
name, fmt::format(fmt::runtime(spec.second), val, spec.first));
6064
}
6165

6266
void Logger::addEntry(std::string_view name, size_t val) {
6367
const auto spec = m_colSpecs[name];
64-
m_currentLine[name] = fmt::format(fmt::runtime(spec.second), val, spec.first);
68+
m_currentLine.insert_or_assign(
69+
name, fmt::format(fmt::runtime(spec.second), val, spec.first));
6570
}
6671

6772
} // namespace aligator

0 commit comments

Comments
 (0)