forked from frc971/971-Robot-Code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdie.cc
84 lines (70 loc) · 2.1 KB
/
die.cc
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
77
78
79
80
81
82
83
84
#include "aos/die.h"
#include <sys/types.h>
#include <unistd.h>
#include <atomic>
#include <cerrno>
#include <csignal>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
namespace aos {
void Die(const char *format, ...) {
va_list args;
va_start(args, format);
VDie(format, args);
// va_end(args) // not because VDie never returns
}
namespace {
// Calculates the filename to dump the message into.
const std::string GetFilename() {
#ifdef __VXWORKS__
const char *name = taskName(0); // get the name of this task
if (name == NULL) name = "<unknown>";
const std::string first_part = "/aos_fatal_error.";
return first_part + std::string(name);
#else
char *filename;
if (asprintf(&filename, "/tmp/aos_fatal_error.%jd",
static_cast<intmax_t>(getpid())) > 0) {
std::string r(filename);
free(filename);
return r;
} else {
fprintf(stderr,
"aos fatal: asprintf(%p, \"thingie with %%jd\", %jd)"
" failed with %d\n",
&filename, static_cast<intmax_t>(getpid()), errno);
return std::string();
}
#endif
}
::std::atomic_bool test_mode(false);
} // namespace
void VDie(const char *format, va_list args_in) {
// We don't bother va_ending either of these because we're going nowhere and
// vxworks has some weird bugs that sometimes show up...
va_list args1, args2;
fputs("aos fatal: ERROR!! details following\n", stderr);
va_copy(args1, args_in);
vfprintf(stderr, format, args1);
if (!test_mode.load()) {
fputs("aos fatal: ERROR!! see stderr for details\n", stdout);
const std::string filename = GetFilename();
if (!filename.empty()) {
FILE *error_file = fopen(filename.c_str(), "w");
if (error_file != NULL) {
va_copy(args2, args_in);
vfprintf(error_file, format, args2);
fclose(error_file);
} else {
fprintf(stderr, "aos fatal: fopen('%s', \"w\") failed with %d\n",
filename.c_str(), errno);
}
}
}
abort();
}
void SetDieTestMode(bool new_test_mode) { test_mode.store(new_test_mode); }
} // namespace aos