1 // Copyright 2012 Intel Corporation
2 //
3 // 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 // - Redistributions of source code must retain the above copyright notice, this
9 //   list of conditions and the following disclaimer.
10 //
11 // - Redistributions in binary form must reproduce the above copyright notice,
12 //   this list of conditions and the following disclaimer in the documentation
13 //   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 ARE
18 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 
26 #pragma once
27 
28 #include <stdbool.h>
29 
30 #include "waffle.h"
31 
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35 
36 /// @brief Thread-local info for the wcore_error module.
37 struct wcore_error_tinfo;
38 
39 struct wcore_error_tinfo*
40 wcore_error_tinfo_create(void);
41 
42 bool
43 wcore_error_tinfo_destroy(struct wcore_error_tinfo *self);
44 
45 /// @brief Reset the error state to WAFFLE_NO_ERROR.
46 void
47 wcore_error_reset(void);
48 
49 /// @brief Set error code for client.
50 ///
51 /// @param error is an `enum waffle_error`.
52 void
53 wcore_error(enum waffle_error error);
54 
55 /// @brief Set error code and message for client.
56 ///
57 /// @param error is an `enum waffle_error`.
58 /// @param format may be null.
59 void
60 wcore_errorf(enum waffle_error error, const char *format, ...);
61 
62 /// @brief Emit error for errno.
63 ///
64 /// Set error code to WAFFLE_ERROR_UNKNOWN and place the output of
65 /// strerror() in the error message. If \a format is not null,
66 /// then prepend the strerror() message with "${format}: ".
67 void
68 wcore_error_errno(const char *format, ...);
69 
70 /// @brief Emit WAFFLE_ERROR_BAD_ATTRIBUTE with a default error message.
71 void
72 wcore_error_bad_attribute(intptr_t attr);
73 
74 /// @brief Set error to WAFFLE_INTERNAL_ERROR with source location.
75 #define wcore_error_internal(format, ...) \
76     _wcore_error_internal(__FILE__, __LINE__, format, __VA_ARGS__)
77 
78 /// @brief Execute a statement with errors disabled.
79 #define WCORE_ERROR_DISABLED(statement) \
80     do { \
81         _wcore_error_disable(); \
82         statement \
83         _wcore_error_enable(); \
84     } while (0)
85 
86 /// @brief Get the last set error code.
87 enum waffle_error
88 wcore_error_get_code(void);
89 
90 /// @brief Get the user-visible portion of the error state.
91 const struct waffle_error_info*
92 wcore_error_get_info(void);
93 
94 void
95 _wcore_error_internal(const char *file, int line, const char *format, ...);
96 
97 void _wcore_error_enable(void);
98 void _wcore_error_disable(void);
99 
100 #ifdef __cplusplus
101 }
102 #endif
103