Version 3.0.23.01.25
[clinfo] / src / error.h
1 /* OpenCL error handling */
2
3 #ifndef ERROR_H
4 #define ERROR_H
5
6 #include <stdio.h>
7
8 #include "ext.h"
9 #include "info_loc.h"
10 #include "fmtmacros.h"
11 #include "strbuf.h"
12
13 cl_int
14 check_ocl_error(cl_int err, const char *what, const char *func, int line)
15 {
16         if (err != CL_SUCCESS) {
17                 fflush(stdout);
18                 fflush(stderr);
19                 fprintf(stderr, "%s:%u: %s : error %d\n",
20                         func, line, what, err);
21                 fflush(stderr);
22         }
23         return err;
24 }
25
26 cl_int
27 report_ocl_error_basic(struct _strbuf *str, cl_int err, const char *what, const char *func, int line)
28 {
29         if (err != CL_SUCCESS) {
30                 snprintf(str->buf, str->sz, "<%s:%d: %s : error %d>",
31                         func, line, what, err);
32         }
33         return err;
34 }
35
36
37 cl_int
38 report_ocl_error_loc(struct _strbuf *str, cl_int err, const char *fmt,
39         const struct info_loc *loc)
40 {
41         static char full_fmt[1024];
42         if (err != CL_SUCCESS) {
43                 snprintf(full_fmt, 1024, "<%s:%" PRIuS ": %s : error %d>",
44                         loc->function, loc->line, fmt, err);
45                 snprintf(str->buf, str->sz, full_fmt, loc->sname);
46         }
47         return err != CL_SUCCESS;
48 }
49
50 void
51 report_size_mismatch(struct _strbuf *str, size_t req, size_t ours,
52         const struct info_loc *loc)
53 {
54         snprintf(str->buf, str->sz, "<%s:%" PRIuS ": %s : size mismatch "
55                 "(requested %" PRIuS ", we offer %" PRIuS ")>",
56                 loc->function, loc->line, loc->sname,
57                 req, ours);
58 }
59
60 #define CHECK_ERROR(error, what) if (check_ocl_error(error, what, __func__, __LINE__)) exit(1)
61
62 #define REPORT_ERROR(str, err, what) report_ocl_error_basic(str, err, what, __func__, __LINE__)
63 #define REPORT_ERROR_LOC(ret, err, loc, what) report_ocl_error_loc(&((ret)->err_str), err, what, loc)
64 #define REPORT_SIZE_MISMATCH(str, loc, req, ours) report_size_mismatch(str, req, ours, loc)
65
66 #endif