summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/core/telemetry_session.cpp36
1 files changed, 36 insertions, 0 deletions
diff --git a/src/core/telemetry_session.cpp b/src/core/telemetry_session.cpp
index 94483f385..61ba78457 100644
--- a/src/core/telemetry_session.cpp
+++ b/src/core/telemetry_session.cpp
@@ -3,8 +3,10 @@
3// Refer to the license.txt file included. 3// Refer to the license.txt file included.
4 4
5#include <cstring> 5#include <cstring>
6#include <cryptopp/osrng.h>
6 7
7#include "common/assert.h" 8#include "common/assert.h"
9#include "common/file_util.h"
8#include "common/scm_rev.h" 10#include "common/scm_rev.h"
9#include "common/x64/cpu_detect.h" 11#include "common/x64/cpu_detect.h"
10#include "core/core.h" 12#include "core/core.h"
@@ -29,12 +31,46 @@ static const char* CpuVendorToStr(Common::CPUVendor vendor) {
29 UNREACHABLE(); 31 UNREACHABLE();
30} 32}
31 33
34static u64 GenerateTelemetryId() {
35 u64 telemetry_id{};
36 CryptoPP::AutoSeededRandomPool rng;
37 rng.GenerateBlock(reinterpret_cast<CryptoPP::byte*>(&telemetry_id), sizeof(u64));
38 return telemetry_id;
39}
40
41static u64 GetTelemetryId() {
42 u64 telemetry_id{};
43 static const std::string& filename{FileUtil::GetUserPath(D_CONFIG_IDX) + "telemetry_id"};
44
45 if (FileUtil::Exists(filename)) {
46 FileUtil::IOFile file(filename, "rb");
47 if (!file.IsOpen()) {
48 LOG_ERROR(WebService, "failed to open telemetry_id: %s", filename.c_str());
49 return {};
50 }
51 file.ReadBytes(&telemetry_id, sizeof(u64));
52 } else {
53 FileUtil::IOFile file(filename, "wb");
54 if (!file.IsOpen()) {
55 LOG_ERROR(WebService, "failed to open telemetry_id: %s", filename.c_str());
56 return {};
57 }
58 telemetry_id = GenerateTelemetryId();
59 file.WriteBytes(&telemetry_id, sizeof(u64));
60 }
61
62 return telemetry_id;
63}
64
32TelemetrySession::TelemetrySession() { 65TelemetrySession::TelemetrySession() {
33#ifdef ENABLE_WEB_SERVICE 66#ifdef ENABLE_WEB_SERVICE
34 backend = std::make_unique<WebService::TelemetryJson>(); 67 backend = std::make_unique<WebService::TelemetryJson>();
35#else 68#else
36 backend = std::make_unique<Telemetry::NullVisitor>(); 69 backend = std::make_unique<Telemetry::NullVisitor>();
37#endif 70#endif
71 // Log one-time top-level information
72 AddField(Telemetry::FieldType::None, "TelemetryId", GetTelemetryId());
73
38 // Log one-time session start information 74 // Log one-time session start information
39 const s64 init_time{std::chrono::duration_cast<std::chrono::milliseconds>( 75 const s64 init_time{std::chrono::duration_cast<std::chrono::milliseconds>(
40 std::chrono::system_clock::now().time_since_epoch()) 76 std::chrono::system_clock::now().time_since_epoch())