Realloc bufsz only if no error
[clinfo] / src / error.h
1 /* OpenCL error handling */
2
3 #include <stdio.h>
4
5 #include "ext.h"
6 #include "fmtmacros.h"
7
8 cl_int error;
9
10 int
11 check_ocl_error(cl_int err, const char *what, const char *func, int line)
12 {
13         if (err != CL_SUCCESS) {
14                 fflush(stdout);
15                 fflush(stderr);
16                 fprintf(stderr, "%s:%u: %s : error %d\n",
17                         func, line, what, err);
18                 fflush(stderr);
19         }
20         return err != CL_SUCCESS;
21 }
22
23 const char *current_function;
24 size_t current_line;
25 const char *current_param;
26
27 int
28 report_ocl_error(char *dstbuf, size_t sz, cl_int err, const char *fmt)
29 {
30         static char full_fmt[1024];
31         if (err != CL_SUCCESS) {
32                 snprintf(full_fmt, 1024, "<%s:%" PRIuS ": %s : error %d>",
33                         current_function, current_line, fmt, err);
34                 snprintf(dstbuf, sz, full_fmt, current_param);
35         }
36         return err != CL_SUCCESS;
37 }
38
39 int
40 report_ocl_error_old(char *where, size_t sz, cl_int err, const char *what, const char *func, int line)
41 {
42         if (err != CL_SUCCESS) {
43                 snprintf(where, sz, "<%s:%d: %s : error %d>",
44                         func, line, what, err);
45         }
46         return err != CL_SUCCESS;
47 }
48
49 #define CHECK_ERROR(what) if (check_ocl_error(error, what, __func__, __LINE__)) exit(1)
50
51 #define REPORT_ERROR(what) report_ocl_error_old(strbuf, bufsz, error, what, __func__, __LINE__)
52 #define REPORT_ERROR2(what) report_ocl_error(strbuf, bufsz, error, what)
53