2016-06-17 18:43:59 -06:00
|
|
|
// Copyright 2016 Dolphin Emulator Project
|
|
|
|
// Licensed under GPLv2+
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2017-06-18 03:42:41 -06:00
|
|
|
#include <chrono>
|
2016-06-17 18:43:59 -06:00
|
|
|
#include <memory>
|
2020-01-23 19:42:52 -07:00
|
|
|
#include <shared_mutex>
|
2016-06-17 18:43:59 -06:00
|
|
|
#include <string>
|
2019-06-03 16:35:26 -06:00
|
|
|
#include <string_view>
|
2016-06-17 18:43:59 -06:00
|
|
|
#include <thread>
|
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
2016-08-02 14:00:41 -06:00
|
|
|
|
2016-06-17 18:43:59 -06:00
|
|
|
#include "Common/CommonTypes.h"
|
|
|
|
#include "Common/Event.h"
|
|
|
|
#include "Common/Flag.h"
|
2017-06-12 09:17:05 -06:00
|
|
|
#include "Common/HttpRequest.h"
|
2017-08-23 17:45:42 -06:00
|
|
|
#include "Common/SPSCQueue.h"
|
2016-06-17 18:43:59 -06:00
|
|
|
|
|
|
|
// Utilities for analytics reporting in Dolphin. This reporting is designed to
|
|
|
|
// provide anonymous data about how well Dolphin performs in the wild. It also
|
|
|
|
// allows developers to declare trace points in Dolphin's source code and get
|
|
|
|
// information about what games trigger these conditions.
|
|
|
|
//
|
|
|
|
// This unfortunately implements Yet Another Serialization Framework within
|
|
|
|
// Dolphin. We cannot really use ChunkFile because there is precedents for
|
|
|
|
// backwards incompatible changes in the ChunkFile format. We could use
|
|
|
|
// something like protobuf but setting up external dependencies is Hard™.
|
|
|
|
//
|
|
|
|
// Example usage:
|
|
|
|
//
|
|
|
|
// static auto s_reporter = std::make_unique<AnalyticsReporter>();
|
|
|
|
// if (user_gave_consent)
|
|
|
|
// {
|
|
|
|
// s_reporter->SetBackend(std::make_unique<MyReportingBackend>());
|
|
|
|
// }
|
|
|
|
// s_reporter->Send(s_reporter->Builder()
|
|
|
|
// .AddData("my_key", 42)
|
|
|
|
// .AddData("other_key", false));
|
|
|
|
|
|
|
|
namespace Common
|
|
|
|
{
|
|
|
|
// Generic interface for an analytics reporting backends. The main
|
2020-09-15 04:13:10 -06:00
|
|
|
// implementation used in Dolphin can be found in Core/DolphinAnalytics.h.
|
2016-06-17 18:43:59 -06:00
|
|
|
class AnalyticsReportingBackend
|
|
|
|
{
|
|
|
|
public:
|
2019-06-03 16:27:43 -06:00
|
|
|
virtual ~AnalyticsReportingBackend() = default;
|
2016-06-17 18:43:59 -06:00
|
|
|
// Called from the AnalyticsReporter backend thread.
|
|
|
|
virtual void Send(std::string report) = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Builder object for an analytics report.
|
|
|
|
class AnalyticsReportBuilder
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
AnalyticsReportBuilder();
|
|
|
|
~AnalyticsReportBuilder() = default;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2020-01-23 19:42:52 -07:00
|
|
|
AnalyticsReportBuilder(const AnalyticsReportBuilder& other) : m_report{other.Get()} {}
|
|
|
|
AnalyticsReportBuilder(AnalyticsReportBuilder&& other) : m_report{other.Consume()} {}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2016-06-17 18:43:59 -06:00
|
|
|
const AnalyticsReportBuilder& operator=(const AnalyticsReportBuilder& other)
|
|
|
|
{
|
|
|
|
if (this != &other)
|
|
|
|
{
|
2020-01-23 19:42:52 -07:00
|
|
|
std::string other_report = other.Get();
|
|
|
|
std::lock_guard lk{m_lock};
|
|
|
|
m_report = std::move(other_report);
|
2016-06-17 18:43:59 -06:00
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2016-06-17 18:43:59 -06:00
|
|
|
// Append another builder to this one.
|
|
|
|
AnalyticsReportBuilder& AddBuilder(const AnalyticsReportBuilder& other)
|
|
|
|
{
|
|
|
|
// Get before locking the object to avoid deadlocks with this += this.
|
|
|
|
std::string other_report = other.Get();
|
2019-06-03 16:30:52 -06:00
|
|
|
std::lock_guard lk{m_lock};
|
2016-06-17 18:43:59 -06:00
|
|
|
m_report += other_report;
|
|
|
|
return *this;
|
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2016-06-17 18:43:59 -06:00
|
|
|
template <typename T>
|
2019-06-03 16:35:26 -06:00
|
|
|
AnalyticsReportBuilder& AddData(std::string_view key, const T& value)
|
2016-06-17 18:43:59 -06:00
|
|
|
{
|
2019-06-03 16:30:52 -06:00
|
|
|
std::lock_guard lk{m_lock};
|
2016-06-17 18:43:59 -06:00
|
|
|
AppendSerializedValue(&m_report, key);
|
|
|
|
AppendSerializedValue(&m_report, value);
|
|
|
|
return *this;
|
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2018-10-26 20:58:39 -06:00
|
|
|
template <typename T>
|
2019-06-03 16:35:26 -06:00
|
|
|
AnalyticsReportBuilder& AddData(std::string_view key, const std::vector<T>& value)
|
2018-10-26 20:58:39 -06:00
|
|
|
{
|
2019-06-03 16:30:52 -06:00
|
|
|
std::lock_guard lk{m_lock};
|
2018-10-26 20:58:39 -06:00
|
|
|
AppendSerializedValue(&m_report, key);
|
|
|
|
AppendSerializedValueVector(&m_report, value);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2016-06-17 18:43:59 -06:00
|
|
|
std::string Get() const
|
|
|
|
{
|
2020-01-23 19:42:52 -07:00
|
|
|
std::shared_lock lk{m_lock};
|
2016-06-17 18:43:59 -06:00
|
|
|
return m_report;
|
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2016-06-17 18:43:59 -06:00
|
|
|
// More efficient version of Get().
|
|
|
|
std::string Consume()
|
|
|
|
{
|
2019-06-03 16:30:52 -06:00
|
|
|
std::lock_guard lk{m_lock};
|
2016-06-17 18:43:59 -06:00
|
|
|
return std::move(m_report);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2019-06-03 16:35:26 -06:00
|
|
|
static void AppendSerializedValue(std::string* report, std::string_view v);
|
2016-06-17 18:43:59 -06:00
|
|
|
static void AppendSerializedValue(std::string* report, const char* v);
|
|
|
|
static void AppendSerializedValue(std::string* report, bool v);
|
|
|
|
static void AppendSerializedValue(std::string* report, u64 v);
|
|
|
|
static void AppendSerializedValue(std::string* report, s64 v);
|
|
|
|
static void AppendSerializedValue(std::string* report, u32 v);
|
|
|
|
static void AppendSerializedValue(std::string* report, s32 v);
|
|
|
|
static void AppendSerializedValue(std::string* report, float v);
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2018-10-26 20:58:39 -06:00
|
|
|
static void AppendSerializedValueVector(std::string* report, const std::vector<u32>& v);
|
|
|
|
|
2020-01-23 19:42:52 -07:00
|
|
|
mutable std::shared_mutex m_lock;
|
2016-06-17 18:43:59 -06:00
|
|
|
std::string m_report;
|
|
|
|
};
|
|
|
|
|
|
|
|
class AnalyticsReporter
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
AnalyticsReporter();
|
|
|
|
~AnalyticsReporter();
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2016-06-17 18:43:59 -06:00
|
|
|
// Sets a reporting backend and enables sending reports. Do not set a remote
|
|
|
|
// backend without user consent.
|
|
|
|
void SetBackend(std::unique_ptr<AnalyticsReportingBackend> backend)
|
|
|
|
{
|
|
|
|
m_backend = std::move(backend);
|
|
|
|
m_reporter_event.Set(); // In case reports are waiting queued.
|
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2016-06-17 18:43:59 -06:00
|
|
|
// Gets the base report builder which is closed for each subsequent report
|
|
|
|
// being sent. DO NOT use this builder to send a report. Only use it to add
|
|
|
|
// new fields that should be globally available.
|
|
|
|
AnalyticsReportBuilder& BaseBuilder() { return m_base_builder; }
|
|
|
|
// Gets a cloned builder that can be used to send a report.
|
|
|
|
AnalyticsReportBuilder Builder() const { return m_base_builder; }
|
|
|
|
// Enqueues a report for sending. Consumes the report builder.
|
|
|
|
void Send(AnalyticsReportBuilder&& report);
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2016-06-17 18:43:59 -06:00
|
|
|
// For convenience.
|
|
|
|
void Send(AnalyticsReportBuilder& report) { Send(std::move(report)); }
|
2018-04-12 06:18:04 -06:00
|
|
|
|
2016-06-17 18:43:59 -06:00
|
|
|
protected:
|
|
|
|
void ThreadProc();
|
|
|
|
|
|
|
|
std::shared_ptr<AnalyticsReportingBackend> m_backend;
|
|
|
|
AnalyticsReportBuilder m_base_builder;
|
|
|
|
|
|
|
|
std::thread m_reporter_thread;
|
|
|
|
Common::Event m_reporter_event;
|
|
|
|
Common::Flag m_reporter_stop_request;
|
2017-08-23 17:45:42 -06:00
|
|
|
SPSCQueue<std::string> m_reports_queue;
|
2016-06-17 18:43:59 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
// Analytics backend to be used for debugging purpose, which dumps reports to
|
|
|
|
// stdout.
|
|
|
|
class StdoutAnalyticsBackend : public AnalyticsReportingBackend
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
void Send(std::string report) override;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Analytics backend that POSTs data to a remote HTTP(s) endpoint. WARNING:
|
|
|
|
// remember to get explicit user consent before using.
|
|
|
|
class HttpAnalyticsBackend : public AnalyticsReportingBackend
|
|
|
|
{
|
|
|
|
public:
|
2019-06-03 16:33:02 -06:00
|
|
|
explicit HttpAnalyticsBackend(std::string endpoint);
|
2016-06-17 18:43:59 -06:00
|
|
|
~HttpAnalyticsBackend() override;
|
|
|
|
|
|
|
|
void Send(std::string report) override;
|
|
|
|
|
|
|
|
protected:
|
2017-06-12 09:17:05 -06:00
|
|
|
std::string m_endpoint;
|
2017-06-18 03:42:41 -06:00
|
|
|
HttpRequest m_http{std::chrono::seconds{5}};
|
2016-06-17 18:43:59 -06:00
|
|
|
};
|
|
|
|
} // namespace Common
|