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