1
1
#include " prometheus/text_serializer.h"
2
2
3
- #include < array>
4
3
#include < cmath>
4
+ #include < limits>
5
5
#include < locale>
6
6
#include < ostream>
7
- #include < stdexcept>
8
7
#include < string>
9
8
10
- #if __cpp_lib_to_chars >= 201611L
11
- #include < charconv>
12
- #include < stdexcept>
13
- #include < system_error>
14
- #else
15
- #include < cstdio>
16
- #include < limits>
17
- #endif
18
-
19
9
#include " prometheus/client_metric.h"
20
10
#include " prometheus/metric_family.h"
21
11
#include " prometheus/metric_type.h"
@@ -31,26 +21,7 @@ void WriteValue(std::ostream& out, double value) {
31
21
} else if (std::isinf (value)) {
32
22
out << (value < 0 ? " -Inf" : " +Inf" );
33
23
} else {
34
- std::array<char , 128 > buffer;
35
-
36
- #if __cpp_lib_to_chars >= 201611L
37
- auto [ptr, ec] =
38
- std::to_chars (buffer.data (), buffer.data () + buffer.size (), value);
39
- if (ec != std::errc ()) {
40
- throw std::runtime_error (" Could not convert double to string: " +
41
- std::make_error_code (ec).message ());
42
- }
43
- out.write (buffer.data (), ptr - buffer.data ());
44
- #else
45
- auto wouldHaveWritten =
46
- std::snprintf (buffer.data (), buffer.size (), " %.*g" ,
47
- std::numeric_limits<double >::max_digits10 - 1 , value);
48
- if (wouldHaveWritten <= 0 ||
49
- static_cast <std::size_t >(wouldHaveWritten) >= buffer.size ()) {
50
- throw std::runtime_error (" Could not convert double to string" );
51
- }
52
- out.write (buffer.data (), wouldHaveWritten);
53
- #endif
24
+ out << value;
54
25
}
55
26
}
56
27
@@ -217,11 +188,17 @@ void SerializeFamily(std::ostream& out, const MetricFamily& family) {
217
188
218
189
void TextSerializer::Serialize (std::ostream& out,
219
190
const std::vector<MetricFamily>& metrics) const {
220
- std::locale saved_locale = out.getloc ();
191
+ auto saved_locale = out.getloc ();
192
+ auto saved_precision = out.precision ();
193
+
221
194
out.imbue (std::locale::classic ());
195
+ out.precision (std::numeric_limits<double >::max_digits10 - 1 );
196
+
222
197
for (auto & family : metrics) {
223
198
SerializeFamily (out, family);
224
199
}
200
+
225
201
out.imbue (saved_locale);
202
+ out.precision (saved_precision);
226
203
}
227
204
} // namespace prometheus
0 commit comments