src/error.c

changeset 95
622887f7e954
parent 94
24bd333be668
equal deleted inserted replaced
94:24bd333be668 95:622887f7e954
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 * Copyright 2023 Mike Becker. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 * POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include "ascension/context.h"
29 #include "ascension/error.h"
30 #include "ascension/utils.h"
31
32 #include <cx/buffer.h>
33 #include <cx/printf.h>
34 #include <GL/gl.h>
35
36 void asc_error_impl(const char* file, unsigned line, char const* fmt, ...) {
37 asc_set_flag(asc_context.flags, ASC_FLAG_HAS_ERROR);
38
39 // write to error buffer
40 va_list args;
41 va_start(args, fmt);
42 CxBuffer* buf = &asc_context.error_buffer;
43 size_t bufpos = buf->pos;
44 int written = cx_vfprintf(buf, cxBufferWriteFunc, fmt, args);
45 cxBufferPut(buf, '\n');
46 va_end(args);
47
48 // also print to stderr
49 // avoid double-formatting, get it directly from the buffer
50 fprintf(stderr, "[ERROR %s %u] %.*s\n",
51 file, line, written, buf->space+bufpos);
52 }
53
54 bool asc_has_error(void) {
55 return asc_test_flag(asc_context.flags, ASC_FLAG_HAS_ERROR);
56 }
57
58 char const* asc_get_error(void) {
59 // we zero-terminate the current buffer contents before providing them
60 cxBufferPut(&asc_context.error_buffer, 0);
61 --asc_context.error_buffer.pos;
62 --asc_context.error_buffer.size;
63 return asc_context.error_buffer.space;
64 }
65
66 void asc_clear_error(void) {
67 cxBufferClear(&asc_context.error_buffer);
68 asc_clear_flag(asc_context.flags, ASC_FLAG_HAS_ERROR);
69 }
70
71 void asc_error_gl(unsigned code, cxstring message) {
72 const char *glerr;
73 switch(code) {
74 case GL_NO_ERROR:
75 return;
76 case GL_INVALID_ENUM:
77 glerr = "invalid enum";
78 break;
79 case GL_INVALID_VALUE:
80 glerr = "invalid value";
81 break;
82 case GL_INVALID_OPERATION:
83 glerr = "invalid operation";
84 break;
85 case GL_INVALID_FRAMEBUFFER_OPERATION:
86 glerr = "invalid framebuffer operation";
87 break;
88 case GL_OUT_OF_MEMORY:
89 glerr = "out of memory";
90 break;
91 case GL_STACK_UNDERFLOW:
92 glerr = "stack underflow";
93 break;
94 case GL_STACK_OVERFLOW:
95 glerr = "stack overflow";
96 break;
97 default:
98 glerr = "unknown GL error";
99 }
100 asc_error("%s\nGL Error: %s", message, glerr);
101 }
102
103 int asc_error_catch_all_gl(void) {
104 GLenum error;
105 int ret = 0;
106 while ((error = glGetError()) != GL_NO_ERROR) {
107 asc_error_gl(error, CX_STR("Uncaught"));
108 ret = 1;
109 }
110 return ret;
111 }

mercurial