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