This patch introduces a feature whereby rrdcached will disallow updates
[rrdtool.git] / src / rrd_error.c
1 /*****************************************************************************
2  * RRDtool 1.3.2  Copyright by Tobi Oetiker, 1997-2008
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  *************************************************************************** */
32
33 #include "rrd_tool.h"
34 #include <stdarg.h>
35
36 #define MAXLEN 4096
37 #define ERRBUFLEN 256
38 #define CTX (rrd_get_context())
39
40 void rrd_set_error(
41     char *fmt,
42     ...)
43 {
44     va_list   argp;
45
46     rrd_clear_error();
47     va_start(argp, fmt);
48 #ifdef HAVE_VSNPRINTF
49     vsnprintf(CTX->rrd_error, sizeof(CTX->rrd_error), fmt, argp);
50 #else
51     vsprintf(CTX->rrd_error, fmt, argp);
52 #endif
53     va_end(argp);
54 }
55
56 int rrd_test_error(
57     void)
58 {
59     return CTX->rrd_error[0] != '\0';
60 }
61
62 void rrd_clear_error(
63     void)
64 {
65     CTX->rrd_error[0] = '\0';
66 }
67
68 char     *rrd_get_error(
69     void)
70 {
71     return CTX->rrd_error;
72 }
73
74 #if 0
75 /* PS: Keep this stuff around, maybe we want it again if we use
76    rrd_contexts to really associate them with single RRD files and
77    operations on them... Then a single thread may use more than one
78    context. Using these functions would require to change each and
79    every function containing any of the non _r versions... */
80 void rrd_set_error_r(
81     rrd_context_t * rrd_ctx,
82     char *fmt,
83     ...)
84 {
85     va_list   argp;
86
87     rrd_clear_error_r(rrd_ctx);
88     va_start(argp, fmt);
89 #ifdef HAVE_VSNPRINTF
90     vsnprintf(rrd_ctx->rrd_error, sizeof(rrd_ctx->rrd_error), fmt, argp);
91     rrd_ctx->rrd_error[sizeof(rrd_ctx->rrd_error) - 1] = '\0';
92 #else
93     vsprintf(rrd_ctx->rrd_error, fmt, argp);
94 #endif
95     va_end(argp);
96 }
97
98 int rrd_test_error_r(
99     rrd_context_t * rrd_ctx)
100 {
101     return rrd_ctx->rrd_error[0] != '\0';
102 }
103
104 void rrd_clear_error_r(
105     rrd_context_t * rrd_ctx)
106 {
107     rrd_ctx->rrd_error[0] = '\0';
108 }
109
110 char     *rrd_get_error_r(
111     rrd_context_t * rrd_ctx)
112 {
113     return rrd_ctx->rrd_error;
114 }
115 #endif
116
117 /* PS: Should we move this to some other file? It is not really error
118    related. */
119 rrd_context_t *rrd_new_context(
120     void)
121 {
122     rrd_context_t *rrd_ctx = (rrd_context_t *) malloc(sizeof(rrd_context_t));
123
124     if (!rrd_ctx) {
125         return NULL;
126     }
127
128     rrd_ctx->rrd_error[0] = '\0';
129     rrd_ctx->lib_errstr[0] = '\0';
130     return rrd_ctx;
131 }
132
133 void rrd_free_context(
134     rrd_context_t * rrd_ctx)
135 {
136     if (rrd_ctx) {
137         free(rrd_ctx);
138     }
139 }
140
141 #if 0
142 void rrd_globalize_error(
143     rrd_context_t * rrd_ctx)
144 {
145     if (rrd_ctx) {
146         rrd_set_error(rrd_ctx->rrd_error);
147     }
148 }
149 #endif