auto_print.cpp 1.49 KB
Newer Older
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
#include "auto_print.hpp"
#include <map>
#include <exception>

#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wglobal-constructors"
#endif

using handler_map = std::map<std::string, std::function<void()>>;

static handler_map create_handlers()
{
    handler_map m;
    for(const auto& name : migraphx::get_targets())
        m[name] = [] {};
    return m;
}

std::function<void()>& auto_print::get_handler(const std::string& name)
{
    // NOLINTNEXTLINE
    static handler_map handlers = create_handlers();
    return handlers.at(name);
}

void auto_print::set_terminate_handler(const std::string& name)
{
    // NOLINTNEXTLINE
    static std::string pname;
    pname = name;
    std::set_terminate(+[] {
        std::cout << "FAILED: " << pname << std::endl;
        try
        {
            std::rethrow_exception(std::current_exception());
        }
        catch(const std::exception& e)
        {
            std::cout << "    what(): " << e.what() << std::endl;
        }
        std::cout << std::endl;
        for(const auto& tname : migraphx::get_targets())
            get_handler(tname)();
    });
}
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66

static bool in_exception()
{
#if __cplusplus >= 201703L
    return std::uncaught_exceptions() > 0;
#else
    return std::uncaught_exception();
#endif
}

auto_print::~auto_print()
{
    if(in_exception())
    {
        std::cout << std::endl;
        for(const auto& tname : migraphx::get_targets())
            get_handler(tname)();
    }
    get_handler(name) = [] {};
}