mirror of
https://github.com/dolphin-emu/dolphin.git
synced 2025-07-23 14:19:46 -06:00
Common/Analytics: add basic support for vector serialization
Only supports u32 for now since that's the only thing we need.
This commit is contained in:
@ -5,6 +5,7 @@
|
|||||||
#include <cmath>
|
#include <cmath>
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <type_traits>
|
||||||
|
|
||||||
#include "Common/Analytics.h"
|
#include "Common/Analytics.h"
|
||||||
#include "Common/CommonTypes.h"
|
#include "Common/CommonTypes.h"
|
||||||
@ -17,7 +18,7 @@ namespace
|
|||||||
{
|
{
|
||||||
// Format version number, used as the first byte of every report sent.
|
// Format version number, used as the first byte of every report sent.
|
||||||
// Increment for any change to the wire format.
|
// Increment for any change to the wire format.
|
||||||
constexpr u8 WIRE_FORMAT_VERSION = 0;
|
constexpr u8 WIRE_FORMAT_VERSION = 1;
|
||||||
|
|
||||||
// Identifiers for the value types supported by the analytics reporting wire
|
// Identifiers for the value types supported by the analytics reporting wire
|
||||||
// format.
|
// format.
|
||||||
@ -28,8 +29,17 @@ enum class TypeId : u8
|
|||||||
UINT = 2,
|
UINT = 2,
|
||||||
SINT = 3,
|
SINT = 3,
|
||||||
FLOAT = 4,
|
FLOAT = 4,
|
||||||
|
|
||||||
|
// Flags which can be combined with other types.
|
||||||
|
ARRAY = 0x80,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
TypeId operator|(TypeId l, TypeId r)
|
||||||
|
{
|
||||||
|
using ut = std::underlying_type_t<TypeId>;
|
||||||
|
return static_cast<TypeId>(static_cast<ut>(l) | static_cast<ut>(r));
|
||||||
|
}
|
||||||
|
|
||||||
void AppendBool(std::string* out, bool v)
|
void AppendBool(std::string* out, bool v)
|
||||||
{
|
{
|
||||||
out->push_back(v ? '\xFF' : '\x00');
|
out->push_back(v ? '\xFF' : '\x00');
|
||||||
@ -112,6 +122,15 @@ void AnalyticsReportBuilder::AppendSerializedValue(std::string* report, float v)
|
|||||||
AppendBytes(report, reinterpret_cast<u8*>(&v), sizeof(v), false);
|
AppendBytes(report, reinterpret_cast<u8*>(&v), sizeof(v), false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void AnalyticsReportBuilder::AppendSerializedValueVector(std::string* report,
|
||||||
|
const std::vector<u32>& v)
|
||||||
|
{
|
||||||
|
AppendType(report, TypeId::UINT | TypeId::ARRAY);
|
||||||
|
AppendVarInt(report, v.size());
|
||||||
|
for (u32 x : v)
|
||||||
|
AppendVarInt(report, x);
|
||||||
|
}
|
||||||
|
|
||||||
AnalyticsReporter::AnalyticsReporter()
|
AnalyticsReporter::AnalyticsReporter()
|
||||||
{
|
{
|
||||||
m_reporter_thread = std::thread(&AnalyticsReporter::ThreadProc, this);
|
m_reporter_thread = std::thread(&AnalyticsReporter::ThreadProc, this);
|
||||||
|
@ -95,6 +95,15 @@ public:
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
AnalyticsReportBuilder& AddData(const std::string& key, const std::vector<T>& value)
|
||||||
|
{
|
||||||
|
std::lock_guard<std::mutex> lk(m_lock);
|
||||||
|
AppendSerializedValue(&m_report, key);
|
||||||
|
AppendSerializedValueVector(&m_report, value);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
std::string Get() const
|
std::string Get() const
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lk(m_lock);
|
std::lock_guard<std::mutex> lk(m_lock);
|
||||||
@ -118,6 +127,8 @@ protected:
|
|||||||
static void AppendSerializedValue(std::string* report, s32 v);
|
static void AppendSerializedValue(std::string* report, s32 v);
|
||||||
static void AppendSerializedValue(std::string* report, float v);
|
static void AppendSerializedValue(std::string* report, float v);
|
||||||
|
|
||||||
|
static void AppendSerializedValueVector(std::string* report, const std::vector<u32>& v);
|
||||||
|
|
||||||
// Should really be a std::shared_mutex, unfortunately that's C++17 only.
|
// Should really be a std::shared_mutex, unfortunately that's C++17 only.
|
||||||
mutable std::mutex m_lock;
|
mutable std::mutex m_lock;
|
||||||
std::string m_report;
|
std::string m_report;
|
||||||
|
Reference in New Issue
Block a user