2017-01-28 04:49:09 +08:00
|
|
|
#include "pch.h"
|
2017-04-28 09:08:52 +08:00
|
|
|
|
2017-08-24 07:47:54 +08:00
|
|
|
#include "metrics.h"
|
2016-09-19 11:50:08 +08:00
|
|
|
#include "vcpkg_Checks.h"
|
2017-08-24 07:47:54 +08:00
|
|
|
#include "vcpkg_GlobalState.h"
|
2016-09-19 11:50:08 +08:00
|
|
|
#include "vcpkg_System.h"
|
2017-03-14 07:59:21 +08:00
|
|
|
#include "vcpkglib.h"
|
2016-09-19 11:50:08 +08:00
|
|
|
|
2017-01-06 04:47:08 +08:00
|
|
|
namespace vcpkg::Checks
|
2016-09-19 11:50:08 +08:00
|
|
|
{
|
2017-08-24 07:47:54 +08:00
|
|
|
[[noreturn]] static void cleanup_and_exit(const int exit_code)
|
|
|
|
{
|
2017-09-02 08:05:22 +08:00
|
|
|
const auto elapsed_us = GlobalState::timer.lock()->microseconds();
|
2017-08-26 07:03:57 +08:00
|
|
|
|
|
|
|
auto metrics = Metrics::g_metrics.lock();
|
|
|
|
metrics->track_metric("elapsed_us", elapsed_us);
|
2017-08-24 07:47:54 +08:00
|
|
|
GlobalState::debugging = false;
|
2017-08-26 07:03:57 +08:00
|
|
|
metrics->flush();
|
2017-08-24 07:47:54 +08:00
|
|
|
|
2017-08-26 07:55:14 +08:00
|
|
|
SetConsoleCP(GlobalState::g_init_console_cp);
|
|
|
|
SetConsoleOutputCP(GlobalState::g_init_console_output_cp);
|
|
|
|
|
2017-08-31 14:32:41 +08:00
|
|
|
fflush(nullptr);
|
|
|
|
|
|
|
|
::TerminateProcess(::GetCurrentProcess(), exit_code);
|
2017-08-24 07:47:54 +08:00
|
|
|
}
|
|
|
|
|
2017-09-02 08:05:22 +08:00
|
|
|
static BOOL ctrl_handler(DWORD fdw_ctrl_type)
|
2017-08-24 07:47:54 +08:00
|
|
|
{
|
2017-08-26 07:03:57 +08:00
|
|
|
{
|
|
|
|
auto locked_metrics = Metrics::g_metrics.lock();
|
2017-09-02 08:05:22 +08:00
|
|
|
locked_metrics->track_property("CtrlHandler", std::to_string(fdw_ctrl_type));
|
2017-08-26 07:03:57 +08:00
|
|
|
locked_metrics->track_property("error", "CtrlHandler was fired.");
|
|
|
|
}
|
2017-08-24 07:47:54 +08:00
|
|
|
cleanup_and_exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2017-09-02 08:05:22 +08:00
|
|
|
void register_console_ctrl_handler()
|
|
|
|
{
|
|
|
|
SetConsoleCtrlHandler(reinterpret_cast<PHANDLER_ROUTINE>(ctrl_handler), TRUE);
|
|
|
|
}
|
2017-08-24 07:47:54 +08:00
|
|
|
|
2017-04-28 09:08:52 +08:00
|
|
|
[[noreturn]] void unreachable(const LineInfo& line_info)
|
2016-09-19 11:50:08 +08:00
|
|
|
{
|
2017-04-04 07:31:00 +08:00
|
|
|
System::println(System::Color::error, "Error: Unreachable code was reached");
|
2017-04-04 07:47:58 +08:00
|
|
|
System::println(System::Color::error, line_info.to_string()); // Always print line_info here
|
2016-11-05 16:02:15 +08:00
|
|
|
#ifndef NDEBUG
|
|
|
|
std::abort();
|
2016-11-16 09:54:44 +08:00
|
|
|
#else
|
2017-08-24 07:47:54 +08:00
|
|
|
cleanup_and_exit(EXIT_FAILURE);
|
2016-11-16 09:54:44 +08:00
|
|
|
#endif
|
2016-09-19 11:50:08 +08:00
|
|
|
}
|
|
|
|
|
2017-04-28 09:08:52 +08:00
|
|
|
[[noreturn]] void exit_with_code(const LineInfo& line_info, const int exit_code)
|
2017-03-23 08:08:59 +08:00
|
|
|
{
|
2017-05-06 05:37:58 +08:00
|
|
|
Debug::println(System::Color::error, line_info.to_string());
|
2017-08-24 07:47:54 +08:00
|
|
|
cleanup_and_exit(exit_code);
|
2017-03-23 08:08:59 +08:00
|
|
|
}
|
|
|
|
|
2017-09-02 08:05:22 +08:00
|
|
|
[[noreturn]] void exit_with_message(const LineInfo& line_info, const CStringView error_message)
|
2016-09-19 11:50:08 +08:00
|
|
|
{
|
2017-09-02 08:05:22 +08:00
|
|
|
System::println(System::Color::error, error_message);
|
2017-03-23 08:34:17 +08:00
|
|
|
exit_fail(line_info);
|
2016-09-19 11:50:08 +08:00
|
|
|
}
|
|
|
|
|
2017-03-14 08:38:04 +08:00
|
|
|
void check_exit(const LineInfo& line_info, bool expression)
|
2017-02-11 08:51:36 +08:00
|
|
|
{
|
|
|
|
if (!expression)
|
|
|
|
{
|
2017-08-29 10:39:33 +08:00
|
|
|
exit_with_message(line_info, Strings::EMPTY);
|
2017-02-11 08:51:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-02 08:05:22 +08:00
|
|
|
void check_exit(const LineInfo& line_info, bool expression, const CStringView error_message)
|
2016-09-19 11:50:08 +08:00
|
|
|
{
|
|
|
|
if (!expression)
|
|
|
|
{
|
2017-09-02 08:05:22 +08:00
|
|
|
exit_with_message(line_info, error_message);
|
2016-09-19 11:50:08 +08:00
|
|
|
}
|
|
|
|
}
|
2017-01-06 04:47:08 +08:00
|
|
|
}
|