blob: 8a899f4f2008ac18d948a7ece7f4977ca481e2fd [file] [log] [blame]
Keir Mierle45fa7852020-08-10 21:09:54 -07001// Copyright 2020 The Pigweed Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may not
4// use this file except in compliance with the License. You may obtain a copy of
5// the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12// License for the specific language governing permissions and limitations under
13// the License.
14
15#include "pw_metric/metric.h"
16
17#include <array>
18#include <span>
19
Wyatt Heplerf298de42021-03-19 15:06:36 -070020#include "pw_assert/check.h"
Keir Mierle45fa7852020-08-10 21:09:54 -070021#include "pw_log/log.h"
22#include "pw_tokenizer/base64.h"
23
24namespace pw::metric {
25namespace {
26
27template <typename T>
28std::span<const std::byte> AsSpan(const T& t) {
29 return std::span<const std::byte>(reinterpret_cast<const std::byte*>(&t),
30 sizeof(t));
31}
32
33// A convenience class to encode a token as base64 while managing the storage.
34// TODO(keir): Consider putting this into upstream pw_tokenizer.
35struct Base64EncodedToken {
36 Base64EncodedToken(Token token) {
37 int encoded_size = tokenizer::PrefixedBase64Encode(AsSpan(token), data);
38 data[encoded_size] = 0;
39 }
40
41 const char* value() { return data.data(); }
42 std::array<char, 16> data;
43};
44
45const char* Indent(int level) {
46 static const char* kWhitespace8 = " ";
47 level = std::min(level, 4);
48 return kWhitespace8 + 8 - 2 * level;
49}
50
51} // namespace
52
53// Enable easier registration when used as a member.
54Metric::Metric(Token name, float value, IntrusiveList<Metric>& metrics)
55 : Metric(name, value) {
56 metrics.push_front(*this);
57}
58Metric::Metric(Token name, uint32_t value, IntrusiveList<Metric>& metrics)
59 : Metric(name, value) {
60 metrics.push_front(*this);
61}
62
Keir Mierle9b51cdf2020-08-19 09:46:19 -070063float Metric::as_float() const {
64 PW_DCHECK(is_float());
65 return float_;
66}
67
68uint32_t Metric::as_int() const {
69 PW_DCHECK(is_int());
70 return uint_;
71}
72
73void Metric::Increment(uint32_t amount) {
74 PW_DCHECK(is_int());
75 uint_ += amount;
76}
77
78void Metric::SetInt(uint32_t value) {
79 PW_DCHECK(is_int());
80 uint_ = value;
81}
82
83void Metric::SetFloat(float value) {
84 PW_DCHECK(is_float());
85 float_ = value;
86}
87
Keir Mierle45fa7852020-08-10 21:09:54 -070088void Metric::Dump(int level) {
89 Base64EncodedToken encoded_name(name());
90 const char* indent = Indent(level);
91 if (is_float()) {
Tri Pho4d33a9d2021-11-05 17:55:07 -070092 // Variadic macros promote float to double. Explicitly cast here to
93 // acknowledge this and allow projects to use -Wdouble-promotion.
94 PW_LOG_INFO("%s \"%s\": %f,",
95 indent,
96 encoded_name.value(),
97 static_cast<double>(as_float()));
Keir Mierle45fa7852020-08-10 21:09:54 -070098 } else {
99 PW_LOG_INFO("%s \"%s\": %u,",
100 indent,
101 encoded_name.value(),
102 static_cast<unsigned int>(as_int()));
103 }
104}
105
106void Metric::Dump(IntrusiveList<Metric>& metrics, int level) {
107 for (auto& m : metrics) {
108 m.Dump(level);
109 }
110}
111
Keir Mierle9b51cdf2020-08-19 09:46:19 -0700112Group::Group(Token name) : name_(name) {}
113
114Group::Group(Token name, IntrusiveList<Group>& groups) : name_(name) {
115 groups.push_front(*this);
116}
117
Keir Mierle45fa7852020-08-10 21:09:54 -0700118void Group::Dump(int level) {
119 Base64EncodedToken encoded_name(name());
120 const char* indent = Indent(level);
121 PW_LOG_INFO("%s \"%s\": {", indent, encoded_name.value());
122 Group::Dump(children(), level + 1);
123 Metric::Dump(metrics(), level + 1);
124 PW_LOG_INFO("%s }", indent);
125}
126
127void Group::Dump(IntrusiveList<Group>& groups, int level) {
128 for (auto& group : groups) {
129 group.Dump(level);
130 }
131}
132
133} // namespace pw::metric