Skip to content

Commit cde32e7

Browse files
committed
Common: ConfKey align prov + print hash
Signed-off-by: Felix Schlepper <felix.schlepper@cern.ch>
1 parent 1f6767c commit cde32e7

File tree

3 files changed

+46
-19
lines changed

3 files changed

+46
-19
lines changed

Common/Utils/include/CommonUtils/ConfigurableParam.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ class ConfigurableParam
162162
virtual std::string getName() const = 0;
163163

164164
// print the current keys and values to screen (optionally with provenance information)
165-
virtual void printKeyValues(bool showprov = true, bool useLogger = false) const = 0;
165+
virtual void printKeyValues(bool showprov = true, bool useLogger = false, bool withPadding = false, bool showHash = false) const = 0;
166166

167167
// get a single size_t hash_value of this parameter (can be used as a checksum to see
168168
// if object changed or different)

Common/Utils/include/CommonUtils/ConfigurableParamHelper.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ struct ParamDataMember {
3434
std::string value;
3535
std::string provenance;
3636

37-
std::string toString(std::string const& prefix, bool showProv) const;
37+
std::string toString(std::string const& prefix, bool showProv, size_t padding = 0) const;
3838
};
3939

4040
// ----------------------------------------------------------------
@@ -58,8 +58,8 @@ class _ParamHelper
5858
static void syncCCDBandRegistry(std::string const& mainkey, TClass* cl, void* to, void* from,
5959
std::map<std::string, ConfigurableParam::EParamProvenance>* provmap, size_t offset);
6060

61-
static void outputMembersImpl(std::ostream& out, std::string const& mainkey, std::vector<ParamDataMember> const* members, bool showProv, bool useLogger);
62-
static void printMembersImpl(std::string const& mainkey, std::vector<ParamDataMember> const* members, bool showProv, bool useLogger);
61+
static void outputMembersImpl(std::ostream& out, std::string const& mainkey, std::vector<ParamDataMember> const* members, bool showProv, bool useLogger, bool withPadding = false, bool showHash = false);
62+
static void printMembersImpl(std::string const& mainkey, std::vector<ParamDataMember> const* members, bool showProv, bool useLogger, bool withPadding, bool showHash);
6363

6464
static size_t getHashImpl(std::string const& mainkey, std::vector<ParamDataMember> const* members);
6565

@@ -100,13 +100,13 @@ class ConfigurableParamHelper : virtual public ConfigurableParam
100100
// ----------------------------------------------------------------
101101

102102
// one of the key methods, using introspection to print itself
103-
void printKeyValues(bool showProv = true, bool useLogger = false) const final
103+
void printKeyValues(bool showProv = true, bool useLogger = false, bool withPadding = true, bool showHash = true) const final
104104
{
105105
if (!isInitialized()) {
106106
initialize();
107107
}
108108
auto members = getDataMembers();
109-
_ParamHelper::printMembersImpl(getName(), members, showProv, useLogger);
109+
_ParamHelper::printMembersImpl(getName(), members, showProv, useLogger, withPadding, showHash);
110110
}
111111

112112
//
@@ -237,13 +237,13 @@ class ConfigurableParamPromoter : public Base, virtual public ConfigurableParam
237237
// ----------------------------------------------------------------
238238

239239
// one of the key methods, using introspection to print itself
240-
void printKeyValues(bool showProv = true, bool useLogger = false) const final
240+
void printKeyValues(bool showProv = true, bool useLogger = false, bool withPadding = true, bool showHash = true) const final
241241
{
242242
if (!isInitialized()) {
243243
initialize();
244244
}
245245
auto members = getDataMembers();
246-
_ParamHelper::printMembersImpl(getName(), members, showProv, useLogger);
246+
_ParamHelper::printMembersImpl(getName(), members, showProv, useLogger, withPadding, showHash);
247247
}
248248

249249
//

Common/Utils/src/ConfigurableParamHelper.cxx

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <boost/property_tree/ptree.hpp>
2727
#include <boost/functional/hash.hpp>
2828
#include <functional>
29+
#include <format>
2930
#ifdef NDEBUG
3031
#undef NDEBUG
3132
#endif
@@ -35,16 +36,25 @@ using namespace o2::conf;
3536

3637
// ----------------------------------------------------------------------
3738

38-
std::string ParamDataMember::toString(std::string const& prefix, bool showProv) const
39+
std::string ParamDataMember::toString(std::string const& prefix, bool showProv, size_t padding) const
3940
{
40-
std::string nil = "<null>";
41-
41+
const std::string label = prefix + "." + name + " : " + value;
4242
std::ostringstream out;
43-
out << prefix << "." << name << " : " << value;
43+
out << label;
4444

4545
if (showProv) {
46-
std::string prov = (provenance.compare("") == 0 ? nil : provenance);
47-
out << "\t\t[ " + prov + " ]";
46+
std::string prov = (provenance.compare("") == 0 ? "<null>" : provenance);
47+
if (padding) {
48+
size_t len = label.size() - prefix.size() - 5; // 4 four the extra chars + 1 for the maxpad
49+
if (len < padding) {
50+
out << std::string(padding - len, ' ');
51+
} else {
52+
out << ' ';
53+
}
54+
out << "[ " + prov + " ]";
55+
} else {
56+
out << "\t\t[ " + prov + " ]";
57+
}
4858
}
4959
return out.str();
5060
}
@@ -308,23 +318,40 @@ void _ParamHelper::fillKeyValuesImpl(std::string const& mainkey, TClass* cl, voi
308318

309319
// ----------------------------------------------------------------------
310320

311-
void _ParamHelper::printMembersImpl(std::string const& mainkey, std::vector<ParamDataMember> const* members, bool showProv, bool useLogger)
321+
void _ParamHelper::printMembersImpl(std::string const& mainkey, std::vector<ParamDataMember> const* members, bool showProv, bool useLogger, bool withPadding, bool showHash)
312322
{
313323

314-
_ParamHelper::outputMembersImpl(std::cout, mainkey, members, showProv, useLogger);
324+
_ParamHelper::outputMembersImpl(std::cout, mainkey, members, showProv, useLogger, withPadding, showHash);
315325
}
316326

317-
void _ParamHelper::outputMembersImpl(std::ostream& out, std::string const& mainkey, std::vector<ParamDataMember> const* members, bool showProv, bool useLogger)
327+
void _ParamHelper::outputMembersImpl(std::ostream& out, std::string const& mainkey, std::vector<ParamDataMember> const* members, bool showProv, bool useLogger, bool withPadding, bool showHash)
318328
{
319329
if (members == nullptr) {
320330
return;
321331
}
322332

333+
size_t maxpad{0};
334+
if (withPadding) {
335+
for (auto& member : *members) {
336+
maxpad = std::max(maxpad, member.name.size() + member.value.size());
337+
}
338+
}
339+
340+
if (showHash) {
341+
std::string shash = std::format("{:07x}", getHashImpl(mainkey, members));
342+
shash = shash.substr(0, 7);
343+
if (useLogger) {
344+
LOG(info) << mainkey << " [Hash#" << shash << "]";
345+
} else {
346+
out << mainkey << " [Hash#" << shash << "]\n";
347+
}
348+
}
349+
323350
for (auto& member : *members) {
324351
if (useLogger) {
325-
LOG(info) << member.toString(mainkey, showProv);
352+
LOG(info) << member.toString(mainkey, showProv, maxpad);
326353
} else {
327-
out << member.toString(mainkey, showProv) << "\n";
354+
out << member.toString(mainkey, showProv, maxpad) << "\n";
328355
}
329356
}
330357
}

0 commit comments

Comments
 (0)