aboutsummaryrefslogtreecommitdiff
path: root/toolsrc/src/vcpkg_Checks.cpp
blob: b6a88a0112df5b8c47dc29e2a2a4ed996f887929 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include "pch.h"

#include "metrics.h"
#include "vcpkg_Checks.h"
#include "vcpkg_GlobalState.h"
#include "vcpkg_System.h"
#include "vcpkglib.h"

namespace vcpkg::Checks
{
    [[noreturn]] static void cleanup_and_exit(const int exit_code)
    {
        auto elapsed_us = GlobalState::timer.lock()->microseconds();

        auto metrics = Metrics::g_metrics.lock();
        metrics->track_metric("elapsed_us", elapsed_us);
        GlobalState::debugging = false;
        metrics->flush();

        SetConsoleCP(GlobalState::g_init_console_cp);
        SetConsoleOutputCP(GlobalState::g_init_console_output_cp);

        ::exit(exit_code);
    }

    static BOOL CtrlHandler(DWORD fdwCtrlType)
    {
        {
            auto locked_metrics = Metrics::g_metrics.lock();
            locked_metrics->track_property("CtrlHandler", std::to_string(fdwCtrlType));
            locked_metrics->track_property("error", "CtrlHandler was fired.");
        }
        cleanup_and_exit(EXIT_FAILURE);
    }

    void register_console_ctrl_handler() { SetConsoleCtrlHandler((PHANDLER_ROUTINE)CtrlHandler, TRUE); }

    [[noreturn]] void unreachable(const LineInfo& line_info)
    {
        System::println(System::Color::error, "Error: Unreachable code was reached");
        System::println(System::Color::error, line_info.to_string()); // Always print line_info here
#ifndef NDEBUG
        std::abort();
#else
        cleanup_and_exit(EXIT_FAILURE);
#endif
    }

    [[noreturn]] void exit_with_code(const LineInfo& line_info, const int exit_code)
    {
        Debug::println(System::Color::error, line_info.to_string());
        cleanup_and_exit(exit_code);
    }

    [[noreturn]] void exit_with_message(const LineInfo& line_info, const CStringView errorMessage)
    {
        System::println(System::Color::error, errorMessage);
        exit_fail(line_info);
    }

    void check_exit(const LineInfo& line_info, bool expression)
    {
        if (!expression)
        {
            exit_with_message(line_info, Strings::EMPTY);
        }
    }

    void check_exit(const LineInfo& line_info, bool expression, const CStringView errorMessage)
    {
        if (!expression)
        {
            exit_with_message(line_info, errorMessage);
        }
    }
}