summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/common/CMakeLists.txt2
-rw-r--r--src/common/telemetry.cpp40
-rw-r--r--src/common/telemetry.h196
3 files changed, 238 insertions, 0 deletions
diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt
index 4b30185f1..6905d2d50 100644
--- a/src/common/CMakeLists.txt
+++ b/src/common/CMakeLists.txt
@@ -38,6 +38,7 @@ set(SRCS
38 param_package.cpp 38 param_package.cpp
39 scm_rev.cpp 39 scm_rev.cpp
40 string_util.cpp 40 string_util.cpp
41 telemetry.cpp
41 thread.cpp 42 thread.cpp
42 timer.cpp 43 timer.cpp
43 ) 44 )
@@ -74,6 +75,7 @@ set(HEADERS
74 string_util.h 75 string_util.h
75 swap.h 76 swap.h
76 synchronized_wrapper.h 77 synchronized_wrapper.h
78 telemetry.h
77 thread.h 79 thread.h
78 thread_queue_list.h 80 thread_queue_list.h
79 timer.h 81 timer.h
diff --git a/src/common/telemetry.cpp b/src/common/telemetry.cpp
new file mode 100644
index 000000000..bf1f54886
--- /dev/null
+++ b/src/common/telemetry.cpp
@@ -0,0 +1,40 @@
1// Copyright 2017 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#include <algorithm>
6#include "common/telemetry.h"
7
8namespace Telemetry {
9
10void FieldCollection::Accept(VisitorInterface& visitor) const {
11 for (const auto& field : fields) {
12 field.second->Accept(visitor);
13 }
14}
15
16void FieldCollection::AddField(std::unique_ptr<FieldInterface> field) {
17 fields[field->GetName()] = std::move(field);
18}
19
20template <class T>
21void Field<T>::Accept(VisitorInterface& visitor) const {
22 visitor.Visit(*this);
23}
24
25template class Field<bool>;
26template class Field<double>;
27template class Field<float>;
28template class Field<u8>;
29template class Field<u16>;
30template class Field<u32>;
31template class Field<u64>;
32template class Field<s8>;
33template class Field<s16>;
34template class Field<s32>;
35template class Field<s64>;
36template class Field<std::string>;
37template class Field<const char*>;
38template class Field<std::chrono::microseconds>;
39
40} // namespace Telemetry
diff --git a/src/common/telemetry.h b/src/common/telemetry.h
new file mode 100644
index 000000000..dd6bbd759
--- /dev/null
+++ b/src/common/telemetry.h
@@ -0,0 +1,196 @@
1// Copyright 2017 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#pragma once
6
7#include <chrono>
8#include <map>
9#include <memory>
10#include <string>
11#include "common/common_types.h"
12
13namespace Telemetry {
14
15/// Field type, used for grouping fields together in the final submitted telemetry log
16enum class FieldType : u8 {
17 None = 0, ///< No specified field group
18 App, ///< Citra application fields (e.g. version, branch, etc.)
19 Session, ///< Emulated session fields (e.g. title ID, log, etc.)
20 Performance, ///< Emulated performance (e.g. fps, emulated CPU speed, etc.)
21 UserFeedback, ///< User submitted feedback (e.g. star rating, user notes, etc.)
22 UserConfig, ///< User configuration fields (e.g. emulated CPU core, renderer, etc.)
23 UserSystem, ///< User system information (e.g. host CPU type, RAM, etc.)
24};
25
26struct VisitorInterface;
27
28/**
29 * Interface class for telemetry data fields.
30 */
31class FieldInterface : NonCopyable {
32public:
33 virtual ~FieldInterface() = default;
34
35 /**
36 * Accept method for the visitor pattern.
37 * @param visitor Reference to the visitor that will visit this field.
38 */
39 virtual void Accept(VisitorInterface& visitor) const = 0;
40
41 /**
42 * Gets the name of this field.
43 * @returns Name of this field as a string.
44 */
45 virtual const std::string& GetName() const = 0;
46};
47
48/**
49 * Represents a telemetry data field, i.e. a unit of data that gets logged and submitted to our
50 * telemetry web service.
51 */
52template <typename T>
53class Field : public FieldInterface {
54public:
55 Field(FieldType type, std::string name, const T& value)
56 : type(type), name(std::move(name)), value(value) {}
57
58 Field(FieldType type, std::string name, T&& value)
59 : type(type), name(std::move(name)), value(std::move(value)) {}
60
61 Field(const Field& other) : Field(other.type, other.name, other.value) {}
62
63 Field& operator=(const Field& other) {
64 type = other.type;
65 name = other.name;
66 value = other.value;
67 return *this;
68 }
69
70 Field& operator=(Field&& other) {
71 type = other.type;
72 name = std::move(other.name);
73 value = std::move(other.value);
74 return *this;
75 }
76
77 void Accept(VisitorInterface& visitor) const override;
78
79 const std::string& GetName() const override {
80 return name;
81 }
82
83 /**
84 * Returns the type of the field.
85 */
86 FieldType GetType() const {
87 return type;
88 }
89
90 /**
91 * Returns the value of the field.
92 */
93 const T& GetValue() const {
94 return value;
95 }
96
97 inline bool operator==(const Field<T>& other) {
98 return (type == other.type) && (name == other.name) && (value == other.value);
99 }
100
101 inline bool operator!=(const Field<T>& other) {
102 return !(*this == other);
103 }
104
105private:
106 std::string name; ///< Field name, must be unique
107 FieldType type{}; ///< Field type, used for grouping fields together
108 T value; ///< Field value
109};
110
111/**
112 * Collection of data fields that have been logged.
113 */
114class FieldCollection final : NonCopyable {
115public:
116 FieldCollection() = default;
117
118 /**
119 * Accept method for the visitor pattern, visits each field in the collection.
120 * @param visitor Reference to the visitor that will visit each field.
121 */
122 void Accept(VisitorInterface& visitor) const;
123
124 /**
125 * Creates a new field and adds it to the field collection.
126 * @param type Type of the field to add.
127 * @param name Name of the field to add.
128 * @param value Value for the field to add.
129 */
130 template <typename T>
131 void AddField(FieldType type, const char* name, T value) {
132 return AddField(std::make_unique<Field<T>>(type, name, std::move(value)));
133 }
134
135 /**
136 * Adds a new field to the field collection.
137 * @param field Field to add to the field collection.
138 */
139 void AddField(std::unique_ptr<FieldInterface> field);
140
141private:
142 std::map<std::string, std::unique_ptr<FieldInterface>> fields;
143};
144
145/**
146 * Telemetry fields visitor interface class. A backend to log to a web service should implement
147 * this interface.
148 */
149struct VisitorInterface : NonCopyable {
150 virtual ~VisitorInterface() = default;
151
152 virtual void Visit(const Field<bool>& field) = 0;
153 virtual void Visit(const Field<double>& field) = 0;
154 virtual void Visit(const Field<float>& field) = 0;
155 virtual void Visit(const Field<u8>& field) = 0;
156 virtual void Visit(const Field<u16>& field) = 0;
157 virtual void Visit(const Field<u32>& field) = 0;
158 virtual void Visit(const Field<u64>& field) = 0;
159 virtual void Visit(const Field<s8>& field) = 0;
160 virtual void Visit(const Field<s16>& field) = 0;
161 virtual void Visit(const Field<s32>& field) = 0;
162 virtual void Visit(const Field<s64>& field) = 0;
163 virtual void Visit(const Field<std::string>& field) = 0;
164 virtual void Visit(const Field<const char*>& field) = 0;
165 virtual void Visit(const Field<std::chrono::microseconds>& field) = 0;
166
167 /// Completion method, called once all fields have been visited
168 virtual void Complete() = 0;
169};
170
171/**
172 * Empty implementation of VisitorInterface that drops all fields. Used when a functional
173 * backend implementation is not available.
174 */
175struct NullVisitor : public VisitorInterface {
176 ~NullVisitor() = default;
177
178 void Visit(const Field<bool>& /*field*/) override {}
179 void Visit(const Field<double>& /*field*/) override {}
180 void Visit(const Field<float>& /*field*/) override {}
181 void Visit(const Field<u8>& /*field*/) override {}
182 void Visit(const Field<u16>& /*field*/) override {}
183 void Visit(const Field<u32>& /*field*/) override {}
184 void Visit(const Field<u64>& /*field*/) override {}
185 void Visit(const Field<s8>& /*field*/) override {}
186 void Visit(const Field<s16>& /*field*/) override {}
187 void Visit(const Field<s32>& /*field*/) override {}
188 void Visit(const Field<s64>& /*field*/) override {}
189 void Visit(const Field<std::string>& /*field*/) override {}
190 void Visit(const Field<const char*>& /*field*/) override {}
191 void Visit(const Field<std::chrono::microseconds>& /*field*/) override {}
192
193 void Complete() override {}
194};
195
196} // namespace Telemetry