icedb  version 0.5.1
Snow particle scattering database API
Classes | Namespaces | Macros | Functions | Variables
error.cpp File Reference
#include "../icedb/error.hpp"
#include "../private/options.hpp"
#include "../icedb/error_context.h"
#include "../icedb/error.h"
#include "../icedb/util.h"
#include <cstdio>
#include <cstring>
#include <vector>
#include <string>
#include <cwchar>
#include <complex>
#include <list>
#include <sstream>
Include dependency graph for error.cpp:

Go to the source code of this file.

Classes

class  icedb::error::error_options_inner
 

Namespaces

 icedb
 
 icedb::error
 

Macros

#define DOTYPES(f)
 
#define IMPL_xError_ADD(T)   template xError& xError::add<T>(const std::string&, const T);
 

Functions

ICEDB_BEGIN_DECL_C ICEDB_error_code error_context_to_code (const struct ICEDB_error_context *err)
 
size_t error_context_to_message_size (const struct ICEDB_error_context *err)
 
size_t error_context_to_message (const struct ICEDB_error_context *err, size_t buf_size, char *buf)
 
size_t error_context_to_stream (const struct ICEDB_error_context *err, FILE *fp)
 
ICEDB_error_code error_test ()
 
size_t error_code_to_message_size (ICEDB_error_code err)
 
size_t error_code_to_message (ICEDB_error_code err, size_t buf_size, char *buf)
 
size_t error_code_to_stream (ICEDB_error_code err, FILE *fp)
 
struct ICEDB_error_contextget_error_context_thread_local_c ()
 
void error_context_deallocate (struct ICEDB_error_context *c)
 
const char * error_getOSname ()
 
 icedb::error::DOTYPES (IMPL_xError_ADD)
 
template<>
DL_ICEDB std::string icedb::error::stringify< std::string > (error_code_t err)
 
template<>
DL_ICEDB const char * icedb::error::stringify< const char * > (error_code_t err)
 
DL_ICEDB error_context_pt icedb::error::get_error_context_thread_local ()
 
DL_ICEDB void icedb::error::stringify (error_code_t err, std::string &res)
 
DL_ICEDB void icedb::error::stringify (error_code_t err, const char **res)
 
DL_ICEDB void icedb::error::stringify (const error_context_pt &err, std::string &res)
 
DL_ICEDB std::string icedb::error::stringify (const error_context_pt &err)
 
DL_ICEDB error_code_t icedb::error::error_context_to_code (const error_context_pt &err)
 

Variables

DL_ICEDB ICEDB_error_context_to_code_f ICEDB_error_context_to_code = error_context_to_code
 
DL_ICEDB ICEDB_error_context_to_message_size_f ICEDB_error_context_to_message_size = error_context_to_message_size
 
DL_ICEDB ICEDB_error_context_to_message_f ICEDB_error_context_to_message = error_context_to_message
 
DL_ICEDB ICEDB_error_context_to_stream_f ICEDB_error_context_to_stream = error_context_to_stream
 
DL_ICEDB ICEDB_error_test_f ICEDB_error_test = error_test
 
DL_ICEDB ICEDB_error_code_to_message_size_f ICEDB_error_code_to_message_size = error_code_to_message_size
 
DL_ICEDB ICEDB_error_code_to_message_f ICEDB_error_code_to_message = error_code_to_message
 
DL_ICEDB ICEDB_error_code_to_stream_f ICEDB_error_code_to_stream = error_code_to_stream
 
DL_ICEDB ICEDB_get_error_context_thread_local_f ICEDB_get_error_context_thread_local = get_error_context_thread_local_c
 
DL_ICEDB ICEDB_error_context_deallocate_f ICEDB_error_context_deallocate = error_context_deallocate
 
DL_ICEDB ICEDB_error_getOSname_f ICEDB_error_getOSname = error_getOSname
 
DL_ICEDB const struct ICEDB_error_container_ftable ICEDB_ct_error
 

Macro Definition Documentation

◆ DOTYPES

#define DOTYPES (   f)
Value:
f(int); f(float); f(double); f(long); f(long long); \
f(unsigned int); f(unsigned long); f(unsigned long long); f(std::string); f(bool); f(std::complex<double>);

Definition at line 278 of file error.cpp.

◆ IMPL_xError_ADD

#define IMPL_xError_ADD (   T)    template xError& xError::add<T>(const std::string&, const T);

Definition at line 281 of file error.cpp.

Function Documentation

◆ error_code_to_message()

size_t error_code_to_message ( ICEDB_error_code  err,
size_t  buf_size,
char *  buf 
)

Definition at line 143 of file error.cpp.

References ICEDB_COMPAT_strncpy_s(), ICEDB_COMPAT_strnlen_s, ICEDB_DEBUG_RAISE_EXCEPTION, ICEDB_ERRORCODES_MAP, and ICEDB_ERRORCODES_TOTAL.

144 {
145  if (!buf) ICEDB_DEBUG_RAISE_EXCEPTION();
147  uint16_t cnt = (uint16_t)ICEDB_COMPAT_strnlen_s(ICEDB_ERRORCODES_MAP[err],
148  SIZE_MAX - 1) + 1; // -1,+1 because message_size includes the null character.
149  size_t res = ICEDB_COMPAT_strncpy_s(buf, buf_size, ICEDB_ERRORCODES_MAP[err], cnt);
150  return size_t((res>SIZE_MAX) ? SIZE_MAX : res);
151 }
#define ICEDB_DEBUG_RAISE_EXCEPTION()
Definition: defs.h:151
Not really an error, but used for counting.
Definition: errorCodes.h:23
ICEDB_BEGIN_DECL_C ICEDB_SYMBOL_SHARED size_t ICEDB_COMPAT_strncpy_s(char *dest, size_t destSz, const char *src, size_t srcSz)
Definition: util.cpp:14
ICEDB_SYMBOL_PRIVATE const char * ICEDB_ERRORCODES_MAP[ICEDB_ERRORCODES_TOTAL]
Definition: errorCodes.cpp:3
#define ICEDB_COMPAT_strnlen_s
Definition: util.h:44
Here is the call graph for this function:

◆ error_code_to_message_size()

size_t error_code_to_message_size ( ICEDB_error_code  err)

Definition at line 134 of file error.cpp.

References ICEDB_COMPAT_strnlen_s, ICEDB_DEBUG_RAISE_EXCEPTION, ICEDB_ERRORCODES_MAP, and ICEDB_ERRORCODES_TOTAL.

135 {
137  size_t res = (size_t)ICEDB_COMPAT_strnlen_s(ICEDB_ERRORCODES_MAP[err],
138  1000) + 1; // -1,+1 because message_size includes the null character.
139  return res;
140 }
#define ICEDB_DEBUG_RAISE_EXCEPTION()
Definition: defs.h:151
Not really an error, but used for counting.
Definition: errorCodes.h:23
ICEDB_SYMBOL_PRIVATE const char * ICEDB_ERRORCODES_MAP[ICEDB_ERRORCODES_TOTAL]
Definition: errorCodes.cpp:3
#define ICEDB_COMPAT_strnlen_s
Definition: util.h:44

◆ error_code_to_stream()

size_t error_code_to_stream ( ICEDB_error_code  err,
FILE *  fp 
)

Definition at line 154 of file error.cpp.

References ICEDB_COMPAT_fputs_s, ICEDB_DEBUG_RAISE_EXCEPTION, ICEDB_ERRORCODES_MAP, and ICEDB_ERRORCODES_TOTAL.

155 {
156  if (!fp) ICEDB_DEBUG_RAISE_EXCEPTION();
158  size_t res = ICEDB_COMPAT_fputs_s(ICEDB_ERRORCODES_MAP[err], fp);
159 
160  return size_t(res);
161 }
#define ICEDB_DEBUG_RAISE_EXCEPTION()
Definition: defs.h:151
Not really an error, but used for counting.
Definition: errorCodes.h:23
ICEDB_SYMBOL_PRIVATE const char * ICEDB_ERRORCODES_MAP[ICEDB_ERRORCODES_TOTAL]
Definition: errorCodes.cpp:3
#define ICEDB_COMPAT_fputs_s
Definition: util.h:35

◆ error_context_deallocate()

void error_context_deallocate ( struct ICEDB_error_context c)

Definition at line 170 of file error.cpp.

References ICEDB_DEBUG_RAISE_EXCEPTION, ICEDB_free(), ICEDB_error_context::message_text, ICEDB_error_context::num_var_fields, ICEDB_error_context_var_val::val, ICEDB_error_context::var_vals, and ICEDB_error_context_var_val::varname.

Referenced by icedb::error::get_error_context_thread_local().

171 {
172  if (!c) ICEDB_DEBUG_RAISE_EXCEPTION();
174  if (c->var_vals) { // Should always exist, as the construction function automatically widens / allocates.
175  for (int i = 0; i < c->num_var_fields; ++i) {
176  ICEDB_free(c->var_vals[i].val);
177  ICEDB_free(c->var_vals[i].varname);
178  }
179  ICEDB_free(c->var_vals);
180  }
181  ICEDB_free(c);
182 }
#define ICEDB_DEBUG_RAISE_EXCEPTION()
Definition: defs.h:151
ICEDB_SYMBOL_SHARED void ICEDB_free(void *obj)
Free memory region. Should not be double-freed.
Definition: util.cpp:97
ICEDB_error_context_var_val * var_vals
Definition: error_context.h:26
Here is the call graph for this function:
Here is the caller graph for this function:

◆ error_context_to_code()

ICEDB_BEGIN_DECL_C ICEDB_error_code error_context_to_code ( const struct ICEDB_error_context err)

Definition at line 17 of file error.cpp.

References ICEDB_error_context::code, and ICEDB_DEBUG_RAISE_EXCEPTION.

17  {
18  if (!err) ICEDB_DEBUG_RAISE_EXCEPTION();
19  return err->code;
20 }
#define ICEDB_DEBUG_RAISE_EXCEPTION()
Definition: defs.h:151
ICEDB_error_code code
Definition: error_context.h:20

◆ error_context_to_message()

size_t error_context_to_message ( const struct ICEDB_error_context err,
size_t  buf_size,
char *  buf 
)

Definition at line 63 of file error.cpp.

References ICEDB_COMPAT_strncpy_s(), ICEDB_DEBUG_RAISE_EXCEPTION, ICEDB_free(), ICEDB_malloc(), ICEDB_error_context::message_size, ICEDB_error_context::message_text, ICEDB_error_context::num_var_fields, ICEDB_error_context_var_val::val, ICEDB_error_context::var_vals, and ICEDB_error_context_var_val::varname.

64 {
65  if (!err) ICEDB_DEBUG_RAISE_EXCEPTION();
66  if (!buf) ICEDB_DEBUG_RAISE_EXCEPTION();
67  size_t total = 0;
68  if (err->message_text) {
69  total = ICEDB_COMPAT_strncpy_s(buf, buf_size, err->message_text, err->message_size); // Reports the number of characters copied. Note lack of count of null.
70  }
71  // Write the error variables
72  const char* sep = "\t%s\t=\t%s\n";
73  const size_t seplen = strlen(sep);
74 
75 #ifdef ICEDB_USING_SECURE_STRINGS
76  if (err->num_var_fields)
77  for (int i = 0; i < err->num_var_fields; ++i)
78  total += sprintf_s(buf + total, buf_size - total, sep, err->var_vals[i].varname, err->var_vals[i].val);
79 #else
80  size_t expectedSize = 0;
81  if (err->num_var_fields) {
82  for (int i = 0; i < err->num_var_fields; ++i) {
83  // Manually size a temporary buffer, and then copy the data.
84  expectedSize += seplen + strlen(err->var_vals[i].val) + strlen(err->var_vals[i].varname);
85  }
86  }
87  char *tempbuf = (char*)ICEDB_malloc(sizeof(char)*expectedSize);
88  size_t j = 0;
89  if (err->num_var_fields) {
90  for (int i = 0; i < err->num_var_fields; ++i) {
91  j += sprintf(tempbuf + j, sep, err->var_vals[i].varname, err->var_vals[i].val);
92  }
93  }
94  ICEDB_COMPAT_strncpy_s(buf + total, buf_size - total, tempbuf, j);
95  total += j;
96  ICEDB_free(tempbuf);
97 #endif
98 
99  return size_t(total);
100 }
#define ICEDB_DEBUG_RAISE_EXCEPTION()
Definition: defs.h:151
ICEDB_SYMBOL_SHARED void ICEDB_free(void *obj)
Free memory region. Should not be double-freed.
Definition: util.cpp:97
ICEDB_error_context_var_val * var_vals
Definition: error_context.h:26
ICEDB_BEGIN_DECL_C ICEDB_SYMBOL_SHARED size_t ICEDB_COMPAT_strncpy_s(char *dest, size_t destSz, const char *src, size_t srcSz)
Definition: util.cpp:14
ICEDB_SYMBOL_SHARED void * ICEDB_malloc(size_t numBytes)
Allocate memory in bytes. Generally this is just malloced, but a custom allocator may be substituted...
Definition: util.cpp:90
Here is the call graph for this function:

◆ error_context_to_message_size()

size_t error_context_to_message_size ( const struct ICEDB_error_context err)

Definition at line 23 of file error.cpp.

References ICEDB_COMPAT_strncpy_s(), ICEDB_DEBUG_RAISE_EXCEPTION, ICEDB_free(), ICEDB_malloc(), ICEDB_error_context::message_size, ICEDB_error_context::message_text, ICEDB_error_context::num_var_fields, ICEDB_error_context_var_val::val, ICEDB_error_context::var_vals, and ICEDB_error_context_var_val::varname.

23  {
24  if (!err) ICEDB_DEBUG_RAISE_EXCEPTION();
25  size_t total = 1;
26  const size_t buf_size = 1000;
27  char buf[buf_size];
28  if (err->message_text) {
29  total += ICEDB_COMPAT_strncpy_s(buf, buf_size, err->message_text, err->message_size); // Reports the number of characters copied. Note lack of count of null.
30  }
31  // Write the error variables
32  const char* sep = "\t%s\t=\t%s\n";
33  const size_t seplen = strlen(sep);
34 
35 #ifdef ICEDB_USING_SECURE_STRINGS
36  if (err->num_var_fields)
37  for (int i = 0; i < err->num_var_fields; ++i)
38  total += sprintf_s(buf + total, buf_size - total, sep, err->var_vals[i].varname, err->var_vals[i].val);
39 #else
40  size_t expectedSize = 0;
41  if (err->num_var_fields) {
42  for (int i = 0; i < err->num_var_fields; ++i) {
43  // Manually size a temporary buffer, and then copy the data.
44  expectedSize += seplen + strlen(err->var_vals[i].val) + strlen(err->var_vals[i].varname);
45  }
46  }
47  char *tempbuf = (char*)ICEDB_malloc(sizeof(char)*expectedSize);
48  size_t j = 0;
49  if (err->num_var_fields) {
50  for (int i = 0; i < err->num_var_fields; ++i) {
51  j += sprintf(tempbuf + j, sep, err->var_vals[i].varname, err->var_vals[i].val);
52  }
53  }
54  ICEDB_COMPAT_strncpy_s(buf + total, buf_size - total, tempbuf, j);
55  total += j;
56  ICEDB_free(tempbuf);
57 #endif
58 
59  return size_t(total);
60 }
#define ICEDB_DEBUG_RAISE_EXCEPTION()
Definition: defs.h:151
ICEDB_SYMBOL_SHARED void ICEDB_free(void *obj)
Free memory region. Should not be double-freed.
Definition: util.cpp:97
ICEDB_error_context_var_val * var_vals
Definition: error_context.h:26
ICEDB_BEGIN_DECL_C ICEDB_SYMBOL_SHARED size_t ICEDB_COMPAT_strncpy_s(char *dest, size_t destSz, const char *src, size_t srcSz)
Definition: util.cpp:14
ICEDB_SYMBOL_SHARED void * ICEDB_malloc(size_t numBytes)
Allocate memory in bytes. Generally this is just malloced, but a custom allocator may be substituted...
Definition: util.cpp:90
Here is the call graph for this function:

◆ error_context_to_stream()

size_t error_context_to_stream ( const struct ICEDB_error_context err,
FILE *  fp 
)

Definition at line 103 of file error.cpp.

References ICEDB_COMPAT_fprintf_s, ICEDB_COMPAT_fputs_s, ICEDB_DEBUG_RAISE_EXCEPTION, ICEDB_error_context::message_text, ICEDB_error_context::num_var_fields, ICEDB_error_context_var_val::val, ICEDB_error_context::var_vals, and ICEDB_error_context_var_val::varname.

104 {
105  if (!err) ICEDB_DEBUG_RAISE_EXCEPTION();
106  if (!fp) ICEDB_DEBUG_RAISE_EXCEPTION();
107  int res = 0;
108  if (err->message_text) {
109  res = ICEDB_COMPAT_fputs_s(err->message_text, fp);
110  }
111  // Error validation
112  if (res == EOF) {
113  printf("Error in writing error context to stream. Error code is %d. Context text is: %s.\n",
114  ferror(fp), err->message_text);
115  }
116 
117  // Write the error variables
118  if (err->num_var_fields) {
119  for (int i = 0; i < err->num_var_fields; ++i)
120  ICEDB_COMPAT_fprintf_s(fp, "\t%s\t=\t%s\n", err->var_vals[i].varname, err->var_vals[i].val);
121  }
122  return uint16_t(res);
123 }
#define ICEDB_DEBUG_RAISE_EXCEPTION()
Definition: defs.h:151
ICEDB_error_context_var_val * var_vals
Definition: error_context.h:26
#define ICEDB_COMPAT_fputs_s
Definition: util.h:35
#define ICEDB_COMPAT_fprintf_s
Definition: util.h:26

◆ error_getOSname()

const char* error_getOSname ( )

Definition at line 185 of file error.cpp.

185  {
186  const char* name =
187 #if defined(__FreeBSD__)
188  "FreeBSD";
189 #elif defined(__NetBSD__)
190  "NetBSD";
191 #elif defined(__OpenBSD__)
192  "OpenBSD";
193 #elif defined(__bsdi__)
194  "bsdi";
195 #elif defined(__DragonFly__)
196  "DragonFly BSD";
197 #elif defined (__APPLE__)
198  "Apple";
199 #elif defined(__linux__)
200  "Linux";
201 #elif defined(_WIN32)
202  "Windows";
203 #elif defined(__unix__)
204  "Generic Unix";
205 #else
206  "UNKNOWN";
207 #endif
208  return name;
209 }

◆ error_test()

ICEDB_error_code error_test ( )

Definition at line 126 of file error.cpp.

References ICEDB_error_context_append_str, ICEDB_error_context_create, and ICEDB_ERRORCODES_TODO.

127 {
129  ICEDB_error_context_append_str(cxt, "This is a test.\n");
130  return ICEDB_ERRORCODES_TODO;
131 }
#define ICEDB_error_context_append_str
Definition: error_context.h:61
Placeholder error value.
Definition: errorCodes.h:11
#define ICEDB_error_context_create(x)
Definition: error_context.h:35

◆ get_error_context_thread_local_c()

struct ICEDB_error_context* get_error_context_thread_local_c ( )

Definition at line 164 of file error.cpp.

References __ICEDB_LOCAL_THREAD_error_context.

Referenced by icedb::error::get_error_context_thread_local().

165 {
167 }
ICEDB_SYMBOL_PRIVATE ICEDB_THREAD_LOCAL ICEDB_error_context * __ICEDB_LOCAL_THREAD_error_context
Here is the caller graph for this function: