Debian Codegraph @

packages/seacas/libraries/ioss/src/heartbeat/Iohb_Layout.C

raw
1 // Copyright(C) 1999-2022, 2024 National Technology & Engineering Solutions
2 // of Sandia, LLC (NTESS). Under the terms of Contract DE-NA0003525 with
3 // NTESS, the U.S. Government retains certain rights in this software.
4 //
5 // See packages/seacas/LICENSE for details
6
7 #include "heartbeat/Iohb_Layout.h"
8 #include <fmt/format.h>
9 #include <fmt/ostream.h>
10 #include <fmt/ranges.h>
11 #include <string>
12
13 namespace Iohb {
14 Layout::Layout(bool show_labels, int precision, std::string separator, int field_width)
15 : layout_(), separator_(std::move(separator)), precision_(precision),
16 fieldWidth_(field_width), showLabels(show_labels)
17 {
18 }
19
20 void Layout::add_literal(const std::string &label) { fmt::print(layout_, "{}", label); }
21
22 void Layout::add_legend(const std::string &label)
23 {
24 fmt::print(layout_, "{}{:>{}}", legendStarted ? separator_ : "", label, fieldWidth_);
25 legendStarted = true;
26 }
27
28 void Layout::output_common(const std::string &name)
29 {
30 if (count_++ > 0 && !separator_.empty()) {
31 fmt::print(layout_, "{}", separator_);
32 }
33
34 if (showLabels && !name.empty()) {
35 fmt::print(layout_, "{}=", name);
36 }
37 }
38
39 template void Layout::add(const std::string &name, const std::string &value);
40 template void Layout::add(const std::string &name, const int &value);
41 template void Layout::add(const std::string &name, const int64_t &value);
42 template void Layout::add(const std::string &name, const size_t &value);
43
44 // Ideally, this would be in the include file, but when building in Sierra, we
45 // need to keep all `fmt` includes out of the include file due to some TPLs
46 // having an embedded fmt which is a different version than used by IOSS.
47 template <typename T> void Layout::add(const std::string &name, const T &value)
48 {
49 output_common(name);
50 if (!showLabels && fieldWidth_ > 0) {
51 fmt::print(layout_, "{0:{1}}", value, fieldWidth_);
52 }
53 else {
54 fmt::print(layout_, "{}", value);
55 }
56 }
57
58 template <> void Layout::add(const std::string &name, const double &value)
59 {
60 output_common(name);
61 if (precision_ == -1) {
62 // Use lib::fmt full precision output -- as many digits as needed to fully represent the
63 // double
64 fmt::print(layout_, "{}", value);
65 }
66 else if (!showLabels && fieldWidth_ > 0) {
67 fmt::print(layout_, "{0:{1}.{2}e}", value, fieldWidth_, precision_);
68 }
69 else {
70 fmt::print(layout_, "{0:.{1}e}", value, precision_);
71 }
72 }
73
74 template void Layout::add(const std::string &name, const std::vector<int> &value);
75 template void Layout::add(const std::string &name, const std::vector<int64_t> &value);
76 template void Layout::add(const std::string &name, const std::vector<size_t> &value);
77
78 template <typename T> void Layout::add(const std::string &name, const std::vector<T> &value)
79 {
80 if (value.size() == 1) {
81 add(name, value[0]);
82 }
83 else {
84 output_common(name);
85 if (!showLabels && fieldWidth_ > 0) {
86 fmt::print(layout_, "{0:{1}}", fmt::join(value, separator_), fieldWidth_);
87 }
88 else {
89 fmt::print(layout_, "{}", fmt::join(value, separator_));
90 }
91 }
92 }
93
94 template <> void Layout::add(const std::string &name, const std::vector<double> &value)
95 {
96 if (value.size() == 1) {
97 add(name, value[0]);
98 }
99 else {
100 output_common(name);
101 if (precision_ == -1) {
102 // Use lib::fmt full precision output -- as many digits as needed to fully represent the
103 // double
104 fmt::print(layout_, "{}", fmt::join(value, separator_));
105 }
106 else if (!showLabels && fieldWidth_ > 0) {
107 fmt::print(layout_, "{0:{2}.{1}e}", fmt::join(value, separator_), precision_, fieldWidth_);
108 }
109 else {
110 fmt::print(layout_, "{0:.{1}e}", fmt::join(value, separator_), precision_);
111 }
112 }
113 }
114
115 } // namespace Iohb