-
Notifications
You must be signed in to change notification settings - Fork 3
/
error.c
89 lines (77 loc) · 2.27 KB
/
error.c
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
85
86
87
88
89
/**
* Copyright 2018
*
* \author <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#define _GNU_SOURCE
#include "error.h"
#include "hdf5.h"
#include "mpi.h"
#include "netcdf.h"
#include <execinfo.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
void print_backtrace(void) {
void *stack[10];
size_t size = backtrace(stack, 10);
backtrace_symbols_fd(stack, size, STDERR_FILENO);
}
// NetCDF error handler
void handle_nc_error(int err, const char *file, int line) {
if (err != 0) {
const char *message = nc_strerror(err);
log_message(LOG_ERROR, "ERROR in NetCDF %s:%d %d %s\n", file, line, err,
message);
print_backtrace();
MPI_Abort(MPI_COMM_WORLD, err);
}
}
// HDF5 error handler
void handle_h5_error(int err, const char *file, int line) {
if (err < 0) {
log_message(LOG_ERROR, "ERROR in HDF5 %s:%d\n", file, line);
H5Eprint1(stderr);
print_backtrace();
MPI_Abort(MPI_COMM_WORLD, err);
}
}
void handle_c_error(int err, const char *message, const char *file, int line) {
if (err != 0) {
log_message(LOG_ERROR, "ERROR %s:%d %s\n", file, line, message);
print_backtrace();
MPI_Abort(MPI_COMM_WORLD, err);
}
}
static int log_level;
void set_log_level(int level) { log_level = level; }
void log_message(int level, const char *message, ...) {
if (level <= log_level) {
int rank;
va_list vargs;
va_start(vargs, message);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
char *rendered;
int err = vasprintf(&rendered, message, vargs);
if (err > 0) {
fprintf(stderr, "[rank %03d] %s\n", rank, rendered);
free(rendered);
} else {
fprintf(stderr, "[rank %03d] %s\n", rank, "Unknown error");
}
va_end(vargs);
}
}