~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mysys/my_error.cc

  • Committer: Monty Taylor
  • Date: 2009-08-12 06:25:19 UTC
  • mto: (1114.1.1 innodb-plugin-merge)
  • mto: This revision was merged to the branch mainline in revision 1183.
  • Revision ID: mordred@inaugust.com-20090812062519-cij02mrrunvnxblt
Tags: innodb-plugin-1.0.4
InnoDB Plugin 1.0.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2000 MySQL AB
 
2
 
 
3
   This program is free software; you can redistribute it and/or modify
 
4
   it under the terms of the GNU General Public License as published by
 
5
   the Free Software Foundation; version 2 of the License.
 
6
 
 
7
   This program is distributed in the hope that it will be useful,
 
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
10
   GNU General Public License for more details.
 
11
 
 
12
   You should have received a copy of the GNU General Public License
 
13
   along with this program; if not, write to the Free Software
 
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
 
15
 
 
16
#include "mysys_priv.h"
 
17
#include "mysys_err.h"
 
18
#include <mystrings/m_string.h>
 
19
#include <mystrings/m_ctype.h>
 
20
#include <drizzled/gettext.h>
 
21
#include <stdio.h>
 
22
#include <stdarg.h>
 
23
 
 
24
/* Define some external variables for error handling */
 
25
 
 
26
/*
 
27
  WARNING!
 
28
  my_error family functions have to be used according following rules:
 
29
  - if message have not parameters use my_message(ER_CODE, ER(ER_CODE), MYF(N))
 
30
  - if message registered use my_error(ER_CODE, MYF(N), ...).
 
31
  - With some special text of errror message use:
 
32
  my_printf_error(ER_CODE, format, MYF(N), ...)
 
33
*/
 
34
 
 
35
char errbuff[NRERRBUFFS][ERRMSGSIZE];
 
36
 
 
37
/*
 
38
  Message texts are registered into a linked list of 'my_err_head' structs.
 
39
  Each struct contains (1.) an array of pointers to C character strings with
 
40
  '\0' termination, (2.) the error number for the first message in the array
 
41
  (array index 0) and (3.) the error number for the last message in the array
 
42
  (array index (last - first)).
 
43
  The array may contain gaps with NULL pointers and pointers to empty strings.
 
44
  Both kinds of gaps will be translated to "Unknown error %d.", if my_error()
 
45
  is called with a respective error number.
 
46
  The list of header structs is sorted in increasing order of error numbers.
 
47
  Negative error numbers are allowed. Overlap of error numbers is not allowed.
 
48
  Not registered error numbers will be translated to "Unknown error %d.".
 
49
*/
 
50
static struct my_err_head
 
51
{
 
52
  struct my_err_head    *meh_next;      /* chain link */
 
53
  const char            **meh_errmsgs;  /* error messages array */
 
54
  int                   meh_first;      /* error number matching array slot 0 */
 
55
  int                   meh_last;       /* error number matching last slot */
 
56
  bool                  is_globerrs;
 
57
} my_errmsgs_globerrs = {NULL, globerrs, EE_ERROR_FIRST, EE_ERROR_LAST, true};
 
58
 
 
59
static struct my_err_head *my_errmsgs_list= &my_errmsgs_globerrs;
 
60
 
 
61
 
 
62
/*
 
63
   Error message to user
 
64
 
 
65
   SYNOPSIS
 
66
     my_error()
 
67
       nr       Errno
 
68
       MyFlags  Flags
 
69
       ...      variable list
 
70
*/
 
71
 
 
72
void my_error(int nr, myf MyFlags, ...)
 
73
{
 
74
  const char *format;
 
75
  struct my_err_head *meh_p;
 
76
  va_list args;
 
77
  char ebuff[ERRMSGSIZE + 20];
 
78
 
 
79
  /* Search for the error messages array, which could contain the message. */
 
80
  for (meh_p= my_errmsgs_list; meh_p; meh_p= meh_p->meh_next)
 
81
    if (nr <= meh_p->meh_last)
 
82
      break;
 
83
 
 
84
  /* get the error message string. Default, if NULL or empty string (""). */
 
85
  if (! (format= (meh_p && (nr >= meh_p->meh_first)) ?
 
86
         _(meh_p->meh_errmsgs[nr - meh_p->meh_first]) : NULL) || ! *format)
 
87
    (void) snprintf (ebuff, sizeof(ebuff), _("Unknown error %d"), nr);
 
88
  else
 
89
  {
 
90
    va_start(args,MyFlags);
 
91
    (void) vsnprintf (ebuff, sizeof(ebuff), format, args);
 
92
    va_end(args);
 
93
  }
 
94
  (*error_handler_hook)(nr, ebuff, MyFlags);
 
95
  return;
 
96
}
 
97
 
 
98
 
 
99
/*
 
100
  Error as printf
 
101
 
 
102
  SYNOPSIS
 
103
    my_printf_error()
 
104
      error     Errno
 
105
      format    Format string
 
106
      MyFlags   Flags
 
107
      ...       variable list
 
108
*/
 
109
 
 
110
void my_printf_error(uint32_t error, const char *format, myf MyFlags, ...)
 
111
{
 
112
  va_list args;
 
113
  char ebuff[ERRMSGSIZE+20];
 
114
 
 
115
  va_start(args,MyFlags);
 
116
  (void) vsnprintf (ebuff, sizeof(ebuff), format, args);
 
117
  va_end(args);
 
118
  (*error_handler_hook)(error, ebuff, MyFlags);
 
119
  return;
 
120
}
 
121
 
 
122
/*
 
123
  Give message using error_handler_hook
 
124
 
 
125
  SYNOPSIS
 
126
    my_message()
 
127
      error     Errno
 
128
      str       Error message
 
129
      MyFlags   Flags
 
130
*/
 
131
 
 
132
void my_message(uint32_t error, const char *str, register myf MyFlags)
 
133
{
 
134
  (*error_handler_hook)(error, str, MyFlags);
 
135
}
 
136
 
 
137
 
 
138
/*
 
139
  Register error messages for use with my_error().
 
140
 
 
141
  SYNOPSIS
 
142
    my_error_register()
 
143
    errmsgs                     array of pointers to error messages
 
144
    first                       error number of first message in the array
 
145
    last                        error number of last message in the array
 
146
 
 
147
  DESCRIPTION
 
148
    The pointer array is expected to contain addresses to NUL-terminated
 
149
    C character strings. The array contains (last - first + 1) pointers.
 
150
    NULL pointers and empty strings ("") are allowed. These will be mapped to
 
151
    "Unknown error" when my_error() is called with a matching error number.
 
152
    This function registers the error numbers 'first' to 'last'.
 
153
    No overlapping with previously registered error numbers is allowed.
 
154
 
 
155
  RETURN
 
156
    0           OK
 
157
    != 0        Error
 
158
*/
 
159
 
 
160
int my_error_register(const char **errmsgs, int first, int last)
 
161
{
 
162
  struct my_err_head *meh_p;
 
163
  struct my_err_head **search_meh_pp;
 
164
 
 
165
  /* Allocate a new header structure. */
 
166
  if (! (meh_p= (struct my_err_head*) my_malloc(sizeof(struct my_err_head),
 
167
                                                MYF(MY_WME))))
 
168
    return 1;
 
169
  meh_p->meh_errmsgs= errmsgs;
 
170
  meh_p->meh_first= first;
 
171
  meh_p->meh_last= last;
 
172
  meh_p->is_globerrs= false;
 
173
 
 
174
  /* Search for the right position in the list. */
 
175
  for (search_meh_pp= &my_errmsgs_list;
 
176
       *search_meh_pp;
 
177
       search_meh_pp= &(*search_meh_pp)->meh_next)
 
178
  {
 
179
    if ((*search_meh_pp)->meh_last > first)
 
180
      break;
 
181
  }
 
182
 
 
183
  /* Error numbers must be unique. No overlapping is allowed. */
 
184
  if (*search_meh_pp && ((*search_meh_pp)->meh_first <= last))
 
185
  {
 
186
    free((unsigned char*)meh_p);
 
187
    return 1;
 
188
  }
 
189
 
 
190
  /* Insert header into the chain. */
 
191
  meh_p->meh_next= *search_meh_pp;
 
192
  *search_meh_pp= meh_p;
 
193
  return 0;
 
194
}
 
195
 
 
196
 
 
197
/*
 
198
  Unregister formerly registered error messages.
 
199
 
 
200
  SYNOPSIS
 
201
    my_error_unregister()
 
202
    first                       error number of first message
 
203
    last                        error number of last message
 
204
 
 
205
  DESCRIPTION
 
206
    This function unregisters the error numbers 'first' to 'last'.
 
207
    These must have been previously registered by my_error_register().
 
208
    'first' and 'last' must exactly match the registration.
 
209
    If a matching registration is present, the header is removed from the
 
210
    list and the pointer to the error messages pointers array is returned.
 
211
    Otherwise, NULL is returned.
 
212
 
 
213
  RETURN
 
214
    non-NULL    OK, returns address of error messages pointers array.
 
215
    NULL        Error, no such number range registered.
 
216
*/
 
217
 
 
218
const char **my_error_unregister(int first, int last)
 
219
{
 
220
  struct my_err_head    *meh_p;
 
221
  struct my_err_head    **search_meh_pp;
 
222
  const char            **errmsgs;
 
223
 
 
224
  /* Search for the registration in the list. */
 
225
  for (search_meh_pp= &my_errmsgs_list;
 
226
       *search_meh_pp;
 
227
       search_meh_pp= &(*search_meh_pp)->meh_next)
 
228
  {
 
229
    if (((*search_meh_pp)->meh_first == first) &&
 
230
        ((*search_meh_pp)->meh_last == last))
 
231
      break;
 
232
  }
 
233
  if (! *search_meh_pp)
 
234
    return NULL;
 
235
 
 
236
  /* Remove header from the chain. */
 
237
  meh_p= *search_meh_pp;
 
238
  *search_meh_pp= meh_p->meh_next;
 
239
 
 
240
  /* Save the return value and free the header. */
 
241
  errmsgs= meh_p->meh_errmsgs;
 
242
  bool is_globerrs= meh_p->is_globerrs;
 
243
 
 
244
  free((unsigned char*) meh_p);
 
245
 
 
246
  if (is_globerrs)
 
247
    return NULL;
 
248
  
 
249
  return errmsgs;
 
250
}
 
251
 
 
252
 
 
253
void my_error_unregister_all(void)
 
254
{
 
255
  struct my_err_head    *list, *next;
 
256
  for (list= my_errmsgs_globerrs.meh_next; list; list= next)
 
257
  {
 
258
    next= list->meh_next;
 
259
    free((unsigned char*) list);
 
260
  }
 
261
  my_errmsgs_list= &my_errmsgs_globerrs;
 
262
}