• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // 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 #pragma once
15 
16 #include "pw_containers/intrusive_list.h"
17 #include "pw_metric/metric.h"
18 #include "pw_tokenizer/tokenize.h"
19 
20 namespace pw::metric {
21 
22 // TODO(keir): Add protection to IntrusiveList to detect the unitialized case,
23 // which can happen with the global static constructors in the -O0 case.
24 extern IntrusiveList<Group> global_groups;
25 extern IntrusiveList<Metric> global_metrics;
26 
27 // Define a metric that is registered in pw::metric::global_metrics.
28 //
29 // This is useful for cases where uncoordinated instrumentation with metrics is
30 // needed; for example, when instrumenting legacy code where plumbing a metric
31 // group around by dependency injection is infeasible.
32 #define PW_METRIC_GLOBAL(variable_name, metric_name, init)                    \
33   static constexpr uint32_t variable_name##_token =                           \
34       PW_TOKENIZE_STRING_DOMAIN("metrics", #metric_name);                     \
35   ::pw::metric::TypedMetric<_PW_METRIC_FLOAT_OR_UINT32(init)> variable_name = \
36       {variable_name##_token, init, ::pw::metric::global_metrics}
37 
38 // Define a group that is registered in pw::metric::global_groups.
39 #define PW_METRIC_GROUP_GLOBAL(variable_name, group_name)     \
40   static constexpr uint32_t variable_name##_token =           \
41       PW_TOKENIZE_STRING_DOMAIN("metrics", #group_name);      \
42   ::pw::metric::Group variable_name = {variable_name##_token, \
43                                        ::pw::metric::global_groups}
44 
45 }  // namespace pw::metric
46