forked from aristanetworks/EosSdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
panic.cpp
57 lines (46 loc) · 1.13 KB
/
panic.cpp
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
// Copyright (c) 2013 Arista Networks, Inc. All rights reserved.
// Arista Networks, Inc. Confidential and Proprietary.
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include "eos/panic.h"
#include "eos/exception.h"
namespace eos {
static void throw_exception(error const & exception) {
exception.raise();
}
auto exception_handler = throw_exception;
void panic(char const * fmt, ...) {
va_list ap;
va_start(ap, fmt);
vpanic(fmt, ap);
// not reached
}
void panic(error const & error) {
if(exception_handler) {
exception_handler(error);
}
fprintf(stderr, "%s\n", error.what());
for(;;) {
abort();
}
}
struct legacy_exception : public error {
virtual ~legacy_exception() noexcept {
}
explicit legacy_exception(std::string const & msg) noexcept : error(msg) {
}
virtual void raise() const {
throw *this;
}
};
void vpanic(char const * fmt, va_list ap) {
char buf[1024];
vsnprintf(buf, 1024, fmt, ap);
panic(legacy_exception(buf));
}
void exception_handler_is(exception_handler_t h) {
assert(h && "exception handler must not be null");
exception_handler = h;
}
}