summaryrefslogtreecommitdiff
path: root/chromium/tools/metrics/structured/events_template.py
blob: 4ead3094891fd763d1a6f817f0e5b19c8702362c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# Copyright 2019 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Templates for generating event classes for structured metrics."""

import codegen

########
# HEADER
########

HEADER_FILE_TEMPLATE = """\
// Generated from gen_events.py. DO NOT EDIT!
// source: structured.xml

#ifndef {file.guard_path}
#define {file.guard_path}

#include <cstdint>
#include <string>

#include "components/metrics/structured/event_base.h"

namespace metrics {{
namespace structured {{
namespace events {{

constexpr uint64_t kProjectNameHashes[] = {project_name_hashes};\
{event_code}

}}  // namespace events
}}  // namespace structured
}}  // namespace metrics

#endif  // {file.guard_path}\
"""

HEADER_EVENT_TEMPLATE = """

class {event.name} final : public ::metrics::structured::EventBase {{
 public:
  {event.name}();
  ~{event.name}() override;

  static constexpr uint64_t kEventNameHash = UINT64_C({event.name_hash});
  static constexpr uint64_t kProjectNameHash = UINT64_C({event.project_name_hash});\
{metric_code}
}};\
"""

HEADER_METRIC_TEMPLATE = """

  static constexpr uint64_t k{metric.name}NameHash = UINT64_C({metric.hash});
  {event.name}& Set{metric.name}(const {metric.type} value);\
"""

HEADER = codegen.Template(
  basename="structured_events.h",
  file_template=HEADER_FILE_TEMPLATE,
  event_template=HEADER_EVENT_TEMPLATE,
  metric_template=HEADER_METRIC_TEMPLATE)

######
# IMPL
######

IMPL_FILE_TEMPLATE = """\
// Generated from gen_events.py. DO NOT EDIT!
// source: structured.xml

#include "{file.dir_path}/structured_events.h"

namespace metrics {{
namespace structured {{
namespace events {{\
{event_code}

}}  // namespace events
}}  // namespace structured
}}  // namespace metrics\
"""

IMPL_EVENT_TEMPLATE = """

{event.name}::{event.name}() :
  ::metrics::structured::EventBase(kEventNameHash, kProjectNameHash) {{}}
{event.name}::~{event.name}() = default;\
{metric_code}\
"""

IMPL_METRIC_TEMPLATE = """

{event.name}& {event.name}::Set{metric.name}(const {metric.type} value) {{
  {metric.setter}(k{metric.name}NameHash, value);
  return *this;
}}\
"""

IMPL = codegen.Template(
  basename="structured_events.cc",
  file_template=IMPL_FILE_TEMPLATE,
  event_template=IMPL_EVENT_TEMPLATE,
  metric_template=IMPL_METRIC_TEMPLATE)

def WriteFiles(outdir, relpath, data):
  HEADER.WriteFile(outdir, relpath, data)
  IMPL.WriteFile(outdir, relpath, data)