mirror of
https://github.com/microsoft/vcpkg.git
synced 2025-06-07 16:52:53 +08:00
[vcpkg] Enable metrics on linux
This commit is contained in:
parent
222fa36012
commit
0ef0300b8e
@ -13,15 +13,12 @@ endif()
|
|||||||
|
|
||||||
file(GLOB_RECURSE VCPKGLIB_SOURCES src/vcpkg/*.cpp)
|
file(GLOB_RECURSE VCPKGLIB_SOURCES src/vcpkg/*.cpp)
|
||||||
|
|
||||||
add_library(vcpkglib STATIC ${VCPKGLIB_SOURCES})
|
add_executable(vcpkg src/vcpkg.cpp ${VCPKGLIB_SOURCES})
|
||||||
target_compile_definitions(vcpkglib PRIVATE -DDISABLE_METRICS=0)
|
target_compile_definitions(vcpkg PRIVATE -DDISABLE_METRICS=0)
|
||||||
target_include_directories(vcpkglib PUBLIC include)
|
target_include_directories(vcpkg PUBLIC include)
|
||||||
|
|
||||||
if(GCC)
|
if(GCC)
|
||||||
target_link_libraries(vcpkglib PUBLIC stdc++fs)
|
target_link_libraries(vcpkg PUBLIC stdc++fs)
|
||||||
elseif(CLANG)
|
elseif(CLANG)
|
||||||
target_link_libraries(vcpkglib PUBLIC c++experimental)
|
target_link_libraries(vcpkg PUBLIC c++experimental)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
add_executable(vcpkg src/vcpkg.cpp)
|
|
||||||
target_link_libraries(vcpkg PRIVATE vcpkglib)
|
|
||||||
|
@ -38,6 +38,8 @@ namespace vcpkg::Strings
|
|||||||
|
|
||||||
std::string to_utf8(const CWStringView& w);
|
std::string to_utf8(const CWStringView& w);
|
||||||
|
|
||||||
|
std::string escape_string(const CStringView& s, char char_to_escape, char escape_char);
|
||||||
|
|
||||||
std::string::const_iterator case_insensitive_ascii_find(const std::string& s, const std::string& pattern);
|
std::string::const_iterator case_insensitive_ascii_find(const std::string& s, const std::string& pattern);
|
||||||
|
|
||||||
bool case_insensitive_ascii_contains(const std::string& s, const std::string& pattern);
|
bool case_insensitive_ascii_contains(const std::string& s, const std::string& pattern);
|
||||||
|
@ -171,7 +171,6 @@ static void inner(const VcpkgCmdArguments& args)
|
|||||||
|
|
||||||
static void load_config()
|
static void load_config()
|
||||||
{
|
{
|
||||||
#if defined(_WIN32)
|
|
||||||
auto& fs = Files::get_real_filesystem();
|
auto& fs = Files::get_real_filesystem();
|
||||||
|
|
||||||
auto config = UserConfig::try_read_data(fs);
|
auto config = UserConfig::try_read_data(fs);
|
||||||
@ -185,16 +184,20 @@ static void load_config()
|
|||||||
write_config = true;
|
write_config = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if defined(_WIN32)
|
||||||
if (config.user_mac.empty())
|
if (config.user_mac.empty())
|
||||||
{
|
{
|
||||||
config.user_mac = Metrics::get_MAC_user();
|
config.user_mac = Metrics::get_MAC_user();
|
||||||
write_config = true;
|
write_config = true;
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
{
|
{
|
||||||
auto locked_metrics = Metrics::g_metrics.lock();
|
auto locked_metrics = Metrics::g_metrics.lock();
|
||||||
locked_metrics->set_user_information(config.user_id, config.user_time);
|
locked_metrics->set_user_information(config.user_id, config.user_time);
|
||||||
|
#if defined(_WIN32)
|
||||||
locked_metrics->track_property("user_mac", config.user_mac);
|
locked_metrics->track_property("user_mac", config.user_mac);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
if (config.last_completed_survey.empty())
|
if (config.last_completed_survey.empty())
|
||||||
@ -208,7 +211,6 @@ static void load_config()
|
|||||||
{
|
{
|
||||||
config.try_write_data(fs);
|
config.try_write_data(fs);
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static std::string trim_path_from_command_line(const std::string& full_command_line)
|
static std::string trim_path_from_command_line(const std::string& full_command_line)
|
||||||
|
@ -31,7 +31,7 @@ namespace vcpkg::Checks
|
|||||||
#if defined(_WIN32)
|
#if defined(_WIN32)
|
||||||
::TerminateProcess(::GetCurrentProcess(), exit_code);
|
::TerminateProcess(::GetCurrentProcess(), exit_code);
|
||||||
#else
|
#else
|
||||||
::exit(exit_code);
|
std::exit(exit_code);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -166,7 +166,7 @@ namespace vcpkg::Files
|
|||||||
}
|
}
|
||||||
virtual void write_contents(const fs::path& file_path, const std::string& data, std::error_code& ec) override
|
virtual void write_contents(const fs::path& file_path, const std::string& data, std::error_code& ec) override
|
||||||
{
|
{
|
||||||
ec = std::error_code();
|
ec.clear();
|
||||||
|
|
||||||
FILE* f = nullptr;
|
FILE* f = nullptr;
|
||||||
#if defined(_WIN32)
|
#if defined(_WIN32)
|
||||||
|
@ -74,6 +74,16 @@ namespace vcpkg::Strings
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string escape_string(const CStringView& s, char char_to_escape, char escape_char)
|
||||||
|
{
|
||||||
|
std::string ret = s.c_str();
|
||||||
|
// Replace '\' with '\\' or '`' with '``'
|
||||||
|
ret = Strings::replace_all(std::move(ret), {escape_char}, {escape_char, escape_char});
|
||||||
|
// Replace '"' with '\"' or '`"'
|
||||||
|
ret = Strings::replace_all(std::move(ret), {char_to_escape}, {escape_char, char_to_escape});
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
std::string::const_iterator case_insensitive_ascii_find(const std::string& s, const std::string& pattern)
|
std::string::const_iterator case_insensitive_ascii_find(const std::string& s, const std::string& pattern)
|
||||||
{
|
{
|
||||||
const std::string pattern_as_lower_case(ascii_to_lowercase(pattern));
|
const std::string pattern_as_lower_case(ascii_to_lowercase(pattern));
|
||||||
|
@ -30,7 +30,6 @@ namespace vcpkg::Commands::Contact
|
|||||||
|
|
||||||
if (Util::Sets::contains(parsed_args.switches, switches[0].name))
|
if (Util::Sets::contains(parsed_args.switches, switches[0].name))
|
||||||
{
|
{
|
||||||
#if defined(_WIN32)
|
|
||||||
auto maybe_now = Chrono::CTime::get_current_date_time();
|
auto maybe_now = Chrono::CTime::get_current_date_time();
|
||||||
if (auto p_now = maybe_now.get())
|
if (auto p_now = maybe_now.get())
|
||||||
{
|
{
|
||||||
@ -39,10 +38,14 @@ namespace vcpkg::Commands::Contact
|
|||||||
config.last_completed_survey = p_now->to_string();
|
config.last_completed_survey = p_now->to_string();
|
||||||
config.try_write_data(fs);
|
config.try_write_data(fs);
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
|
|
||||||
|
#if defined(_WIN32)
|
||||||
System::cmd_execute("start https://aka.ms/NPS_vcpkg");
|
System::cmd_execute("start https://aka.ms/NPS_vcpkg");
|
||||||
System::println("Default browser launched to https://aka.ms/NPS_vcpkg, thank you for your feedback!");
|
System::println("Default browser launched to https://aka.ms/NPS_vcpkg; thank you for your feedback!");
|
||||||
|
#else
|
||||||
|
System::println(
|
||||||
|
"Please navigate to https://aka.ms/NPS_vcpkg in your preferred browser. Thank you for your feedback!");
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@ -8,7 +8,11 @@
|
|||||||
#define STRINGIFY(...) #__VA_ARGS__
|
#define STRINGIFY(...) #__VA_ARGS__
|
||||||
#define MACRO_TO_STRING(X) STRINGIFY(X)
|
#define MACRO_TO_STRING(X) STRINGIFY(X)
|
||||||
|
|
||||||
|
#if defined(VCPKG_VERSION)
|
||||||
#define VCPKG_VERSION_AS_STRING MACRO_TO_STRING(VCPKG_VERSION)
|
#define VCPKG_VERSION_AS_STRING MACRO_TO_STRING(VCPKG_VERSION)
|
||||||
|
#else
|
||||||
|
#define VCPKG_VERSION_AS_STRING "-unknownhash"
|
||||||
|
#endif
|
||||||
|
|
||||||
namespace vcpkg::Commands::Version
|
namespace vcpkg::Commands::Version
|
||||||
{
|
{
|
||||||
|
@ -196,8 +196,8 @@ namespace vcpkg::Metrics
|
|||||||
"iKey": "b4e88960-4393-4dd9-ab8e-97e8fe6d7603",
|
"iKey": "b4e88960-4393-4dd9-ab8e-97e8fe6d7603",
|
||||||
"flags": 0.000000,
|
"flags": 0.000000,
|
||||||
"tags": {
|
"tags": {
|
||||||
"ai.device.os": "Windows",
|
"ai.device.os": "Other",
|
||||||
"ai.device.osVersion": "%s",
|
"ai.device.osVersion": "%s-%s",
|
||||||
"ai.session.id": "%s",
|
"ai.session.id": "%s",
|
||||||
"ai.user.id": "%s",
|
"ai.user.id": "%s",
|
||||||
"ai.user.accountAcquisitionDate": "%s"
|
"ai.user.accountAcquisitionDate": "%s"
|
||||||
@ -213,6 +213,17 @@ namespace vcpkg::Metrics
|
|||||||
}
|
}
|
||||||
}])",
|
}])",
|
||||||
timestamp,
|
timestamp,
|
||||||
|
#if defined(_WIN32)
|
||||||
|
"Windows",
|
||||||
|
#elif defined(__APPLE__)
|
||||||
|
"OSX",
|
||||||
|
#elif defined(__linux__)
|
||||||
|
"Linux",
|
||||||
|
#elif defined(__unix__)
|
||||||
|
"Unix",
|
||||||
|
#elif defined(__unix__)
|
||||||
|
"Other",
|
||||||
|
#endif
|
||||||
get_os_version_string(),
|
get_os_version_string(),
|
||||||
session_id,
|
session_id,
|
||||||
user_id,
|
user_id,
|
||||||
@ -390,50 +401,62 @@ namespace vcpkg::Metrics
|
|||||||
|
|
||||||
void Metrics::flush()
|
void Metrics::flush()
|
||||||
{
|
{
|
||||||
#if defined(_WIN32)
|
|
||||||
const std::string payload = g_metricmessage.format_event_data_template();
|
const std::string payload = g_metricmessage.format_event_data_template();
|
||||||
if (g_should_print_metrics) std::cerr << payload << "\n";
|
if (g_should_print_metrics) std::cerr << payload << "\n";
|
||||||
if (!g_should_send_metrics) return;
|
if (!g_should_send_metrics) return;
|
||||||
|
|
||||||
// upload(payload);
|
#if defined(_WIN32)
|
||||||
|
|
||||||
wchar_t temp_folder[MAX_PATH];
|
wchar_t temp_folder[MAX_PATH];
|
||||||
GetTempPathW(MAX_PATH, temp_folder);
|
GetTempPathW(MAX_PATH, temp_folder);
|
||||||
|
|
||||||
const fs::path temp_folder_path = fs::path(temp_folder) / "vcpkg";
|
const fs::path temp_folder_path = fs::path(temp_folder) / "vcpkg";
|
||||||
const fs::path temp_folder_path_exe =
|
const fs::path temp_folder_path_exe =
|
||||||
temp_folder_path / Strings::format("vcpkgmetricsuploader-%s.exe", Commands::Version::base_version());
|
temp_folder_path / Strings::format("vcpkgmetricsuploader-%s.exe", Commands::Version::base_version());
|
||||||
|
#endif
|
||||||
|
|
||||||
auto& fs = Files::get_real_filesystem();
|
auto& fs = Files::get_real_filesystem();
|
||||||
|
|
||||||
if (true)
|
#if defined(_WIN32)
|
||||||
{
|
|
||||||
const fs::path exe_path = [&fs]() -> fs::path {
|
|
||||||
auto vcpkgdir = System::get_exe_path_of_current_process().parent_path();
|
|
||||||
auto path = vcpkgdir / "vcpkgmetricsuploader.exe";
|
|
||||||
if (fs.exists(path)) return path;
|
|
||||||
|
|
||||||
path = vcpkgdir / "scripts" / "vcpkgmetricsuploader.exe";
|
const fs::path exe_path = [&fs]() -> fs::path {
|
||||||
if (fs.exists(path)) return path;
|
auto vcpkgdir = System::get_exe_path_of_current_process().parent_path();
|
||||||
|
auto path = vcpkgdir / "vcpkgmetricsuploader.exe";
|
||||||
|
if (fs.exists(path)) return path;
|
||||||
|
|
||||||
return "";
|
path = vcpkgdir / "scripts" / "vcpkgmetricsuploader.exe";
|
||||||
}();
|
if (fs.exists(path)) return path;
|
||||||
|
|
||||||
std::error_code ec;
|
return "";
|
||||||
fs.create_directories(temp_folder_path, ec);
|
}();
|
||||||
if (ec) return;
|
|
||||||
fs.copy_file(exe_path, temp_folder_path_exe, fs::copy_options::skip_existing, ec);
|
|
||||||
if (ec) return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const fs::path vcpkg_metrics_txt_path = temp_folder_path / ("vcpkg" + generate_random_UUID() + ".txt");
|
|
||||||
std::error_code ec;
|
std::error_code ec;
|
||||||
|
fs.create_directories(temp_folder_path, ec);
|
||||||
|
if (ec) return;
|
||||||
|
fs.copy_file(exe_path, temp_folder_path_exe, fs::copy_options::skip_existing, ec);
|
||||||
|
if (ec) return;
|
||||||
|
#else
|
||||||
|
if (!fs.exists("/tmp")) return;
|
||||||
|
const fs::path temp_folder_path = "/tmp/vcpkg";
|
||||||
|
std::error_code ec;
|
||||||
|
fs.create_directory(temp_folder_path, ec);
|
||||||
|
// ignore error
|
||||||
|
ec.clear();
|
||||||
|
#endif
|
||||||
|
const fs::path vcpkg_metrics_txt_path = temp_folder_path / ("vcpkg" + generate_random_UUID() + ".txt");
|
||||||
fs.write_contents(vcpkg_metrics_txt_path, payload, ec);
|
fs.write_contents(vcpkg_metrics_txt_path, payload, ec);
|
||||||
if (ec) return;
|
if (ec) return;
|
||||||
|
|
||||||
|
#if defined(_WIN32)
|
||||||
const std::string cmd_line = Strings::format("start \"vcpkgmetricsuploader.exe\" \"%s\" \"%s\"",
|
const std::string cmd_line = Strings::format("start \"vcpkgmetricsuploader.exe\" \"%s\" \"%s\"",
|
||||||
temp_folder_path_exe.u8string(),
|
temp_folder_path_exe.u8string(),
|
||||||
vcpkg_metrics_txt_path.u8string());
|
vcpkg_metrics_txt_path.u8string());
|
||||||
System::cmd_execute_clean(cmd_line);
|
#else
|
||||||
|
auto escaped_path = Strings::escape_string(vcpkg_metrics_txt_path.u8string(), '\'', '\\');
|
||||||
|
const std::string cmd_line = Strings::format(
|
||||||
|
R"((curl "https://dc.services.visualstudio.com/v2/track" -H "Content-Type: application/json" -X POST --data '@%s' >/dev/null 2>&1; rm '%s') &)",
|
||||||
|
escaped_path,
|
||||||
|
escaped_path);
|
||||||
#endif
|
#endif
|
||||||
|
System::cmd_execute_clean(cmd_line);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -29,13 +29,22 @@ namespace
|
|||||||
|
|
||||||
namespace vcpkg
|
namespace vcpkg
|
||||||
{
|
{
|
||||||
|
static fs::path get_config_path()
|
||||||
|
{
|
||||||
|
#if defined(_WIN32)
|
||||||
|
return get_localappdata() / "vcpkg" / "config";
|
||||||
|
#else
|
||||||
|
auto maybe_home = System::get_environment_variable("HOME");
|
||||||
|
return fs::path(maybe_home.value_or("/var")) / "vcpkg" / "config";
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
UserConfig UserConfig::try_read_data(const Files::Filesystem& fs)
|
UserConfig UserConfig::try_read_data(const Files::Filesystem& fs)
|
||||||
{
|
{
|
||||||
UserConfig ret;
|
UserConfig ret;
|
||||||
#if defined(_WIN32)
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
auto maybe_pghs = Paragraphs::get_paragraphs(fs, get_localappdata() / "vcpkg" / "config");
|
auto maybe_pghs = Paragraphs::get_paragraphs(fs, get_config_path());
|
||||||
if (const auto p_pghs = maybe_pghs.get())
|
if (const auto p_pghs = maybe_pghs.get())
|
||||||
{
|
{
|
||||||
const auto& pghs = *p_pghs;
|
const auto& pghs = *p_pghs;
|
||||||
@ -58,19 +67,19 @@ namespace vcpkg
|
|||||||
catch (...)
|
catch (...)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
void UserConfig::try_write_data(Files::Filesystem& fs) const
|
void UserConfig::try_write_data(Files::Filesystem& fs) const
|
||||||
{
|
{
|
||||||
#if defined(_WIN32)
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
auto config_path = get_config_path();
|
||||||
|
auto config_dir = config_path.parent_path();
|
||||||
std::error_code ec;
|
std::error_code ec;
|
||||||
fs.create_directory(get_localappdata() / "vcpkg", ec);
|
fs.create_directory(config_dir, ec);
|
||||||
fs.write_contents(get_localappdata() / "vcpkg" / "config",
|
fs.write_contents(config_path,
|
||||||
Strings::format("User-Id: %s\n"
|
Strings::format("User-Id: %s\n"
|
||||||
"User-Since: %s\n"
|
"User-Since: %s\n"
|
||||||
"Mac-Hash: %s\n"
|
"Mac-Hash: %s\n"
|
||||||
@ -84,6 +93,5 @@ namespace vcpkg
|
|||||||
catch (...)
|
catch (...)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user