$darkmode
Libical API Documentation 4.0 STABLE VERSION Visit the v3.0 documentation
icalerror.c
Go to the documentation of this file.
1 /*======================================================================
2  FILE: icalerror.c
3  CREATOR: eric 16 May 1999
4 
5  SPDX-FileCopyrightText: 2000, Eric Busboom <eric@civicknowledge.com>
6  SPDX-License-Identifier: LGPL-2.1-only OR MPL-2.0
7 ======================================================================*/
8 
14 #ifdef HAVE_CONFIG_H
15 #include <config.h>
16 #endif
17 
18 #include "icalerror.h"
19 #include "icalerror_p.h"
20 #include "icalmemory.h"
21 
22 #include <stdbool.h>
23 #include <stdlib.h>
24 
25 #if defined(HAVE_BACKTRACE)
26 #include <execinfo.h>
27 #endif
28 
29 static ICAL_GLOBAL_VAR bool icalerror_errors_are_fatal = false;
30 
32 {
33  icalerror_errors_are_fatal = fatal;
34 }
35 
37 {
38  return icalerror_errors_are_fatal;
39 }
40 
41 #if ICAL_SYNC_MODE == ICAL_SYNC_MODE_PTHREAD
42 #include <pthread.h>
43 
44 static pthread_key_t icalerrno_key;
45 static pthread_once_t icalerrno_key_once = PTHREAD_ONCE_INIT;
46 
47 static void icalerrno_destroy(void *buf)
48 {
50  pthread_setspecific(icalerrno_key, NULL);
51 }
52 
53 static void icalerrno_key_alloc(void)
54 {
55  pthread_key_create(&icalerrno_key, icalerrno_destroy);
56 }
57 
59 {
60  icalerrorenum *_errno;
61 
62  pthread_once(&icalerrno_key_once, icalerrno_key_alloc);
63 
64  _errno = (icalerrorenum *)pthread_getspecific(icalerrno_key);
65 
66  if (!_errno) {
67  _errno = icalmemory_new_buffer(sizeof(icalerrorenum));
68  *_errno = ICAL_NO_ERROR;
69  pthread_setspecific(icalerrno_key, _errno);
70  }
71  return _errno;
72 }
73 
74 #else
75 
76 static ICAL_GLOBAL_VAR icalerrorenum icalerrno_storage = ICAL_NO_ERROR;
77 
79 {
80  return &icalerrno_storage;
81 }
82 
83 #endif
84 
86 {
88 }
89 
91 {
92  icalerrno = x;
94  (icalerror_get_error_state(x) == ICAL_ERROR_DEFAULT && icalerror_errors_are_fatal == 1)) {
95  icalerror_warn(icalerror_strerror(x));
97  icalassert(0);
98  }
99 }
100 
102 {
103 #if defined(HAVE_BACKTRACE)
104  void *stack_frames[50];
105  int i, num;
106  size_t size;
107  char **strings;
108 
109  size = sizeof(stack_frames) / sizeof(stack_frames[0]);
110  num = backtrace(stack_frames, (int)size);
111  strings = backtrace_symbols(stack_frames, num);
112  for (i = 0; i < num; i++) {
113  if (strings != NULL) {
114  icalerrprintf("%s\n", strings[i]);
115  } else {
116  icalerrprintf("%p\n", stack_frames[i]);
117  }
118  }
119  free((void *)strings); /* Not icalmemory_free_buffer(), allocated by backtrace_symbols() */
120 #endif
121 }
bool icalerror_get_errors_are_fatal(void)
Determine if errors are fatal.
Definition: icalerror.c:36
const char * icalerror_strerror(icalerrorenum e)
Finds the description string for error.
Common memory management routines.
void icalmemory_free_buffer(void *buf)
Releases a buffer.
Definition: icalmemory.c:355
void icalerror_backtrace(void)
Prints backtrace.
Definition: icalerror.c:101
void icalerror_set_errno(icalerrorenum x)
Sets the icalerrno to a given error.
Definition: icalerror.c:90
void icalerror_clear_errno(void)
Resets icalerrno to ICAL_NO_ERROR.
Definition: icalerror.c:85
icalerrorenum
Represents the different types of errors that can be triggered in libical.
Definition: icalerror.h:41
icalerrorstate icalerror_get_error_state(icalerrorenum error)
Gets the error state (severity) for a given error.
Error handling for libical.
void icalerror_set_errors_are_fatal(bool fatal)
Change if errors are fatal.
Definition: icalerror.c:31
void * icalmemory_new_buffer(size_t size)
Creates new buffer with the specified size.
Definition: icalmemory.c:315
icalerrorenum * icalerror_icalerrno(void)
Returns the current icalerrno value.
Definition: icalerror.c:58
#define icalerrno
Access the current icalerrno value.
Definition: icalerror.h:133