1 /*****************************************************************************
2 * RRDtool 1.1.x Copyright Tobias Oetiker, 1997 - 2002
3 *****************************************************************************
4 * rrd_error.c Common Header File
5 *****************************************************************************
6 * $Id$
7 * $Log$
8 * Revision 1.3 2003/02/13 07:05:27 oetiker
9 * Find attached the patch I promised to send to you. Please note that there
10 * are three new source files (src/rrd_is_thread_safe.h, src/rrd_thread_safe.c
11 * and src/rrd_not_thread_safe.c) and the introduction of librrd_th. This
12 * library is identical to librrd, but it contains support code for per-thread
13 * global variables currently used for error information only. This is similar
14 * to how errno per-thread variables are implemented. librrd_th must be linked
15 * alongside of libpthred
16 *
17 * There is also a new file "THREADS", holding some documentation.
18 *
19 * -- Peter Stamfest <peter@stamfest.at>
20 *
21 * Revision 1.2 2002/02/01 20:34:49 oetiker
22 * fixed version number and date/time
23 *
24 * Revision 1.1.1.1 2001/02/25 22:25:05 oetiker
25 * checkin
26 *
27 *************************************************************************** */
29 #include "rrd_tool.h"
30 #include <stdarg.h>
32 #define MAXLEN 4096
33 #define ERRBUFLEN 256
34 #define CTX (rrd_get_context())
36 void
37 rrd_set_error(char *fmt, ...)
38 {
39 va_list argp;
40 rrd_clear_error();
41 va_start(argp, fmt);
42 #ifdef HAVE_VSNPRINTF
43 vsnprintf(CTX->rrd_error, CTX->len, fmt, argp);
44 #else
45 vsprintf(CTX->rrd_error, fmt, argp);
46 #endif
47 va_end(argp);
48 }
50 int
51 rrd_test_error(void) {
52 return CTX->rrd_error[0] != '\0';
53 }
55 void
56 rrd_clear_error(void){
57 CTX->rrd_error[0] = '\0';
58 }
60 char *
61 rrd_get_error(void){
62 return CTX->rrd_error;
63 }
65 #if 0
66 /* PS: Keep this stuff around, maybe we want it again if we use
67 rrd_contexts to really associate them with single RRD files and
68 operations on them... Then a single thread may use more than one
69 context. Using these functions would require to change each and
70 every function containing any of the non _r versions... */
71 void
72 rrd_set_error_r(struct rrd_context *rrd_ctx, char *fmt, ...)
73 {
74 va_list argp;
75 rrd_clear_error_r(rrd_ctx);
76 va_start(argp, fmt);
77 #ifdef HAVE_VSNPRINTF
78 vsnprintf((char *)rrd_ctx->rrd_error, rrd_ctx->len, fmt, argp);
79 #else
80 vsprintf((char *)rrd_ctx->rrd_error, fmt, argp);
81 #endif
82 va_end(argp);
83 }
85 int
86 rrd_test_error_r(struct rrd_context *rrd_ctx) {
87 return rrd_ctx->rrd_error[0] != '\0';
88 }
90 void
91 rrd_clear_error_r(struct rrd_context *rrd_ctx) {
92 rrd_ctx->rrd_error[0] = '\0';
93 }
95 char *
96 rrd_get_error_r(struct rrd_context *rrd_ctx) {
97 return (char *)rrd_ctx->rrd_error;
98 }
99 #endif
101 /* PS: Should we move this to some other file? It is not really error
102 related. */
103 struct rrd_context *
104 rrd_new_context(void) {
105 struct rrd_context *rrd_ctx =
106 (struct rrd_context *) malloc(sizeof(struct rrd_context));
108 if (rrd_ctx) {
109 rrd_ctx->len = 0;
110 rrd_ctx->rrd_error = malloc(MAXLEN);
111 rrd_ctx->lib_errstr = malloc(ERRBUFLEN);
112 if (rrd_ctx->rrd_error && rrd_ctx->lib_errstr) {
113 *rrd_ctx->rrd_error = 0;
114 *rrd_ctx->lib_errstr = 0;
115 rrd_ctx->len = MAXLEN;
116 rrd_ctx->errlen = ERRBUFLEN;
117 return rrd_ctx;
118 }
119 if (rrd_ctx->rrd_error) free(rrd_ctx->rrd_error);
120 if (rrd_ctx->lib_errstr) free(rrd_ctx->lib_errstr);
121 free(rrd_ctx);
122 }
123 return NULL;
124 }
126 void
127 rrd_free_context(struct rrd_context *rrd_ctx) {
128 if (rrd_ctx) {
129 if (rrd_ctx->rrd_error) free(rrd_ctx->rrd_error);
130 free(rrd_ctx);
131 }
132 }
134 #if 0
135 void rrd_globalize_error(struct rrd_context *rrd_ctx) {
136 if (rrd_ctx) {
137 rrd_set_error(rrd_ctx->rrd_error);
138 }
139 }
140 #endif