CUBRID Engine  latest
error_manager.h
Go to the documentation of this file.
1 /*
2  * Copyright 2008 Search Solution Corporation
3  * Copyright 2016 CUBRID Corporation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 
20 /*
21  * error_manager.h - Error module (both client & server)
22  */
23 
24 #ifndef _ERROR_MANAGER_H_
25 #define _ERROR_MANAGER_H_
26 
27 #ident "$Id$"
28 
29 #include "config.h"
30 
31 #include <stdarg.h>
32 #include <assert.h>
33 #if defined (SERVER_MODE)
34 #if defined (WINDOWS)
35 #include <winsock2.h>
36 #include <windows.h>
37 #else /* WINDOWS */
38 #include <pthread.h>
39 #endif /* WINDOWS */
40 #endif /* SERVER_MODE */
41 
42 #include "error_code.h"
43 
44 #define ARG_FILE_LINE __FILE__, __LINE__
45 #define NULL_LEVEL 0
46 
47 /* Shorthand for simple warnings and errors */
48 #define ERROR0(error, code) \
49  do \
50  { \
51  error = code; \
52  er_set (ER_WARNING_SEVERITY, ARG_FILE_LINE, code, 0); \
53  } \
54  while (0)
55 
56 #define ERROR1(error, code, arg1) \
57  do \
58  { \
59  error = code; \
60  er_set (ER_WARNING_SEVERITY, ARG_FILE_LINE, code, 1, arg1); \
61  } \
62  while (0)
63 
64 #define ERROR2(error, code, arg1, arg2) \
65  do \
66  { \
67  error = code; \
68  er_set (ER_WARNING_SEVERITY, ARG_FILE_LINE, code, 2, arg1, arg2); \
69  } \
70  while (0)
71 
72 #define ERROR3(error, code, arg1, arg2, arg3) \
73  do \
74  { \
75  error = code; \
76  er_set (ER_WARNING_SEVERITY, ARG_FILE_LINE, code, 3, arg1, arg2, arg3); \
77  } \
78  while (0)
79 
80 #define ERROR4(error, code, arg1, arg2, arg3, arg4) \
81  do \
82  { \
83  error = code; \
84  er_set (ER_WARNING_SEVERITY, ARG_FILE_LINE, code, 4, arg1, arg2, arg3, arg4); \
85  } \
86  while (0)
87 
88 /*
89  * custom assert macro for release mode
90  */
91 #if defined(NDEBUG)
92 #define STRINGIZE(s) #s
93 #define assert_release(e) \
94  ((e) ? (void) 0 : er_set (ER_NOTIFICATION_SEVERITY, ARG_FILE_LINE, ER_FAILED_ASSERTION, 1, STRINGIZE (e)))
95 #else
96 #define assert_release(e) assert(e)
97 #endif
98 
100 {
104  ER_ABORT = 3,
106 };
107 
109 {
112 };
113 
114 /* do not change the order of this enumeration; see er_Fnlog[] */
116 {
121 };
122 
124 {
126 };
127 
128 typedef enum er_final_code
129 {
132 } ER_FINAL_CODE;
133 
134 typedef void (*PTR_FNERLOG) (int err_id);
135 
136 #define ER_IS_LOCK_TIMEOUT_ERROR(err) \
137  ((err) == ER_LK_UNILATERALLY_ABORTED \
138  || (err) == ER_LK_OBJECT_TIMEOUT_SIMPLE_MSG \
139  || (err) == ER_LK_OBJECT_TIMEOUT_CLASS_MSG \
140  || (err) == ER_LK_OBJECT_TIMEOUT_CLASSOF_MSG \
141  || (err) == ER_LK_OBJECT_DL_TIMEOUT_SIMPLE_MSG \
142  || (err) == ER_LK_OBJECT_DL_TIMEOUT_CLASS_MSG \
143  || (err) == ER_LK_OBJECT_DL_TIMEOUT_CLASSOF_MSG)
144 
145 #define ER_IS_ABORTED_DUE_TO_DEADLOCK(err) \
146  ((err) == ER_LK_UNILATERALLY_ABORTED \
147  || (err) == ER_TM_SERVER_DOWN_UNILATERALLY_ABORTED)
148 
149 #define ER_IS_SERVER_DOWN_ERROR(err) \
150  ((err) == ER_TM_SERVER_DOWN_UNILATERALLY_ABORTED \
151  || (err) == ER_NET_SERVER_CRASHED \
152  || (err) == ER_OBJ_NO_CONNECT \
153  || (err) == ER_BO_CONNECT_FAILED)
154 
155 /* Macros to assert that error is set. */
156 #define ASSERT_ERROR() \
157  assert (er_errid () != NO_ERROR)
158 /* This macro will also make sure the error_code to be returned is not
159  * NO_ERROR.
160  */
161 #define ASSERT_ERROR_AND_SET(error_code) \
162  do \
163  { \
164  error_code = er_errid (); \
165  if (error_code == NO_ERROR) \
166  { \
167  /* Error should be set */ \
168  assert (false); \
169  error_code = ER_FAILED; \
170  } \
171  } while (false)
172 /* Macro that checks no error was set. */
173 #define ASSERT_NO_ERROR() \
174  assert (er_errid () == NO_ERROR);
175 
176 #ifdef __cplusplus
177 extern "C"
178 {
179 #endif
180 
181  extern const char *er_get_msglog_filename (void);
182  extern int er_init (const char *msglog_filename, int exit_ask);
183  extern bool er_is_initialized (void);
184  extern void er_set_print_property (int print_console);
185  extern void er_final (ER_FINAL_CODE do_global_final);
186  extern void er_clear (void);
187  extern void er_set (int severity, const char *file_name, const int line_no, int err_id, int num_args, ...);
188  extern void er_set_with_file (int severity, const char *file_name, const int line_no, int err_id, FILE * fp,
189  int num_args, ...);
190  extern void er_set_with_oserror (int severity, const char *file_name, const int line_no, int err_id, int num_args,
191  ...);
192  typedef void (*er_log_handler_t) (unsigned int);
194 
195 #if !defined (WINDOWS) && defined (SERVER_MODE)
196  extern void er_file_create_link_to_current_log_file (const char *er_file_path, const char *suffix);
197 #endif /* !WINDOWS && SERVER_MODE */
198 
199  extern int er_errid (void);
200  extern int er_errid_if_has_error (void);
201  extern int er_get_severity (void);
202  extern const char *er_msg (void);
203  extern void er_all (int *err_id, int *severity, int *nlevels, int *line_no, const char **file_name, const char **msg);
204 
205  extern void _er_log_debug (const char *file_name, const int line_no, const char *fmt, ...);
206 #define er_log_debug(...) if (prm_get_bool_value (PRM_ID_ER_LOG_DEBUG)) _er_log_debug(__VA_ARGS__)
207 
208  extern char *er_get_ermsg_from_area_error (char *buffer);
209  extern char *er_get_area_error (char *buffer, int *length);
210  extern int er_set_area_error (char *server_area);
211  extern void er_stack_push (void);
212  extern void er_stack_push_if_exists (void);
213  extern void er_stack_pop (void);
214  extern void er_stack_pop_and_keep_error (void);
215  extern void er_restore_last_error (void);
216  extern void er_stack_clearall (void);
217  extern void *db_default_malloc_handler (void *arg, const char *filename, int line_no, size_t size);
218  extern int er_event_restart (void);
219  extern void er_clearid (void);
220  extern void er_setid (int err_id);
221 
222  extern bool er_has_error (void);
223  extern void er_print_callstack (const char *file_name, const int line_no, const char *fmt, ...);
224 #if defined (CS_MODE)
225  extern void er_set_ignore_uninit (bool ignore);
226 #endif
227 
228 #ifdef __cplusplus
229 }
230 #endif
231 
232 #ifdef __cplusplus
233 
234 #if defined (SERVER_MODE) || !defined (WINDOWS)
235 // not dll linkage
236 #define CUBERR_MANAGER_DLL
237 #elif defined (CS_MODE) || defined (SA_MODE)
238 // Windows CS_MODE or SA_MODE - export
239 #define CUBERR_MANAGER_DLL __declspec( dllexport )
240 #else // Windows, not CS_MODE and not SA_MODE
241 // import
242 #define CUBERR_MANAGER_DLL __declspec( dllimport )
243 #endif // Windows, not CS_MODE and not SA_MODE
244 
245 /* *INDENT-OFF* */
246 namespace cuberr
247 {
248  class CUBERR_MANAGER_DLL manager
249  {
250  public:
251  manager (const char * msg_file, er_exit_ask exit_arg);
252  ~manager (void);
253  };
254 } // namespace cuberr
255 /* *INDENT-ON* */
256 
257 // to use in C units instead of er_init; makes sure that er_final is called before exiting scope
258 // NOTE - cuberr_manager variable is created. it may cause naming conflicts
259 // NOTE - if used after jumps, expect "crosses initialization" errors
260 #define ER_SAFE_INIT(msg_file, exit_arg) cuberr::manager cuberr_manager (msg_file, exit_arg)
261 #endif // c++
262 
263 #endif /* _ERROR_MANAGER_H_ */
er_print_option
bool er_has_error(void)
er_final_code
int er_errid_if_has_error(void)
void er_set(int severity, const char *file_name, const int line_no, int err_id, int num_args,...)
char * er_get_area_error(char *buffer, int *length)
er_exit_ask
Definition: error_manager.h:99
const char * er_get_msglog_filename(void)
void er_clear(void)
int er_get_severity(void)
void er_stack_clearall(void)
er_log_handler_t er_register_log_handler(er_log_handler_t f)
int er_errid(void)
void er_clearid(void)
er_severity
char * er_get_ermsg_from_area_error(char *buffer)
int er_event_restart(void)
int er_set_area_error(char *server_area)
void er_set_with_file(int severity, const char *file_name, const int line_no, int err_id, FILE *fp, int num_args,...)
const char * er_msg(void)
void er_setid(int err_id)
er_level
void er_all(int *err_id, int *severity, int *nlevels, int *line_no, const char **file_name, const char **msg)
void er_set_with_oserror(int severity, const char *file_name, const int line_no, int err_id, int num_args,...)
void er_stack_pop_and_keep_error(void)
void er_stack_pop(void)
void er_stack_push(void)
void er_print_callstack(const char *file_name, const int line_no, const char *fmt,...)
void * db_default_malloc_handler(void *arg, const char *filename, int line_no, size_t size)
void(* er_log_handler_t)(unsigned int)
void _er_log_debug(const char *file_name, const int line_no, const char *fmt,...)
int er_init(const char *msglog_filename, int exit_ask)
void er_set_print_property(int print_console)
void(* PTR_FNERLOG)(int err_id)
bool er_is_initialized(void)
void er_final(ER_FINAL_CODE do_global_final)
void er_stack_push_if_exists(void)
void er_restore_last_error(void)
enum er_final_code ER_FINAL_CODE