~drizzle-trunk/drizzle/development

499.2.14 by Mark Atwood
fixes as per MontyT's comments, prep for internationalization
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
499.2.10 by Mark Atwood
add editor format hints, and other useful metadata comments
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
499.2.14 by Mark Atwood
fixes as per MontyT's comments, prep for internationalization
3
 *
499.2.10 by Mark Atwood
add editor format hints, and other useful metadata comments
4
 *  Copyright (C) 2008 Mark Atwood
5
 *
6
 *  This program is free software; you can redistribute it and/or modify
7
 *  it under the terms of the GNU General Public License as published by
8
 *  the Free Software Foundation; version 2 of the License.
9
 *
10
 *  This program is distributed in the hope that it will be useful,
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 *  GNU General Public License for more details.
14
 *
15
 *  You should have received a copy of the GNU General Public License
16
 *  along with this program; if not, write to the Free Software
17
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
 */
19
499.2.1 by Mark Atwood
add hooks for errmsg plugin type
20
#include <drizzled/server_includes.h>
21
#include <drizzled/errmsg.h>
549 by Monty Taylor
Took gettext.h out of header files.
22
#include <drizzled/gettext.h>
499.2.1 by Mark Atwood
add hooks for errmsg plugin type
23
602.1.1 by Mark Atwood
hook up the errmsg plugin to the sql_print_error function
24
static bool errmsg_has= false;
25
499.2.1 by Mark Atwood
add hooks for errmsg plugin type
26
int errmsg_initializer(st_plugin_int *plugin)
27
{
28
  errmsg_t *p;
29
30
  p= (errmsg_t *) malloc(sizeof(errmsg_t));
31
  if (p == NULL) return 1;
32
  memset(p, 0, sizeof(errmsg_t));
33
34
  plugin->data= (void *)p;
35
36
  if (plugin->plugin->init)
37
  {
38
    if (plugin->plugin->init((void *)p))
39
    {
499.2.14 by Mark Atwood
fixes as per MontyT's comments, prep for internationalization
40
      /* we're doing the errmsg plugin api,
41
	 so we can't trust the errmsg api to emit our error messages
42
	 so we will emit error messages to stderr */
43
      /* TRANSLATORS: The leading word "errmsg" is the name
44
         of the plugin api, and so should not be translated. */
45
      fprintf(stderr,
46
	      _("errmsg plugin '%s' init() failed."),
47
	      plugin->name.str);
499.2.1 by Mark Atwood
add hooks for errmsg plugin type
48
      goto err;
49
    }
50
  }
602.1.1 by Mark Atwood
hook up the errmsg plugin to the sql_print_error function
51
52
  errmsg_has= true;
53
499.2.1 by Mark Atwood
add hooks for errmsg plugin type
54
  return 0;
55
56
err:
57
  free(p);
58
  return 1;
59
}
60
61
int errmsg_finalizer(st_plugin_int *plugin)
62
{ 
63
  errmsg_t *p = (errmsg_t *) plugin->data;
64
65
  if (plugin->plugin->deinit)
66
  {
67
    if (plugin->plugin->deinit((void *)p))
68
    {
499.2.14 by Mark Atwood
fixes as per MontyT's comments, prep for internationalization
69
      /* we're doing the errmsg plugin api,
70
	 so we can't trust the errmsg api to emit our error messages
71
	 so we will emit error messages to stderr */
72
      /* TRANSLATORS: The leading word "errmsg" is the name
73
         of the plugin api, and so should not be translated. */
74
      fprintf(stderr,
75
	      _("errmsg plugin '%s' deinit() failed."),
76
	      plugin->name.str);
499.2.1 by Mark Atwood
add hooks for errmsg plugin type
77
    }
78
  }
79
80
  if (p) free(p);
81
82
  return 0;
83
}
84
499.2.14 by Mark Atwood
fixes as per MontyT's comments, prep for internationalization
85
/* The plugin_foreach() iterator requires that we
86
   convert all the parameters of a plugin api entry point
520.1.22 by Brian Aker
Second pass of thd cleanup
87
   into just one single void ptr, plus the session.
499.2.14 by Mark Atwood
fixes as per MontyT's comments, prep for internationalization
88
   So we will take all the additional paramters of errmsg_vprintf,
89
   and marshall them into a struct of this type, and
90
   then just pass in a pointer to it.
91
*/
92
typedef struct errmsg_parms_st
93
{
94
  int priority;
95
  const char *format;
96
  va_list ap;
97
} errmsg_parms_t;
98
99
100
/* This gets called by plugin_foreach once for each loaded errmsg plugin */
520.1.22 by Brian Aker
Second pass of thd cleanup
101
static bool errmsg_iterate (Session *session, plugin_ref plugin, void *p)
499.2.1 by Mark Atwood
add hooks for errmsg plugin type
102
{
103
  errmsg_t *l= plugin_data(plugin, errmsg_t *);
499.2.2 by Mark Atwood
va_list handing and other fixes for errmsg plugin
104
  errmsg_parms_t *parms= (errmsg_parms_t *) p;
499.2.1 by Mark Atwood
add hooks for errmsg plugin type
105
499.2.2 by Mark Atwood
va_list handing and other fixes for errmsg plugin
106
  if (l && l->errmsg_func)
499.2.1 by Mark Atwood
add hooks for errmsg plugin type
107
  {
520.1.22 by Brian Aker
Second pass of thd cleanup
108
    if (l->errmsg_func(session, parms->priority, parms->format, parms->ap))
499.2.14 by Mark Atwood
fixes as per MontyT's comments, prep for internationalization
109
    {
110
      /* we're doing the errmsg plugin api,
111
	 so we can't trust the errmsg api to emit our error messages
112
	 so we will emit error messages to stderr */
113
      /* TRANSLATORS: The leading word "errmsg" is the name
114
         of the plugin api, and so should not be translated. */
115
      fprintf(stderr,
116
	      _("errmsg plugin '%s' errmsg_func() failed"),
117
	      (char *)plugin_name(plugin));
499.2.1 by Mark Atwood
add hooks for errmsg plugin type
118
      return true;
499.2.14 by Mark Atwood
fixes as per MontyT's comments, prep for internationalization
119
    }
499.2.1 by Mark Atwood
add hooks for errmsg plugin type
120
  }
121
  return false;
122
}
123
520.1.22 by Brian Aker
Second pass of thd cleanup
124
bool errmsg_vprintf (Session *session, int priority, const char *format, va_list ap)
499.2.1 by Mark Atwood
add hooks for errmsg plugin type
125
{
499.2.14 by Mark Atwood
fixes as per MontyT's comments, prep for internationalization
126
  bool foreach_rv;
499.2.2 by Mark Atwood
va_list handing and other fixes for errmsg plugin
127
  errmsg_parms_t parms;
128
602.1.1 by Mark Atwood
hook up the errmsg plugin to the sql_print_error function
129
  /* check to see if any errmsg plugin has been loaded
130
     if not, just fall back to emitting the message to stderr */
131
  if (!errmsg_has)
132
  {
133
    /* if it turns out that the vfprintf doesnt do one single write
134
       (single writes are atomic), then this needs to be rewritten to
135
       vsprintf into a char buffer, and then write() that char buffer
136
       to stderr */
137
    vfprintf(stderr, format, ap);
138
    return false;
139
  }
140
499.2.14 by Mark Atwood
fixes as per MontyT's comments, prep for internationalization
141
  /* marshall the parameters so they will fit into the foreach */
499.2.2 by Mark Atwood
va_list handing and other fixes for errmsg plugin
142
  parms.priority= priority;
143
  parms.format= format;
499.2.14 by Mark Atwood
fixes as per MontyT's comments, prep for internationalization
144
  va_copy(parms.ap, ap);
499.2.2 by Mark Atwood
va_list handing and other fixes for errmsg plugin
145
499.2.14 by Mark Atwood
fixes as per MontyT's comments, prep for internationalization
146
  /* call errmsg_iterate
147
     once for each loaded errmsg plugin */
520.1.22 by Brian Aker
Second pass of thd cleanup
148
  foreach_rv= plugin_foreach(session,
499.2.14 by Mark Atwood
fixes as per MontyT's comments, prep for internationalization
149
			     errmsg_iterate,
150
			     DRIZZLE_ERRMSG_PLUGIN,
151
			     (void *) &parms);
152
  return foreach_rv;
499.2.1 by Mark Atwood
add hooks for errmsg plugin type
153
}
499.2.2 by Mark Atwood
va_list handing and other fixes for errmsg plugin
154
520.1.22 by Brian Aker
Second pass of thd cleanup
155
bool errmsg_printf (Session *session, int priority, const char *format, ...)
499.2.2 by Mark Atwood
va_list handing and other fixes for errmsg plugin
156
{
499.2.14 by Mark Atwood
fixes as per MontyT's comments, prep for internationalization
157
  bool rv;
499.2.2 by Mark Atwood
va_list handing and other fixes for errmsg plugin
158
  va_list args;
159
  va_start(args, format);
520.1.22 by Brian Aker
Second pass of thd cleanup
160
  rv= errmsg_vprintf(session, priority, format, args);
499.2.2 by Mark Atwood
va_list handing and other fixes for errmsg plugin
161
  va_end(args);
499.2.14 by Mark Atwood
fixes as per MontyT's comments, prep for internationalization
162
  return rv;
499.2.2 by Mark Atwood
va_list handing and other fixes for errmsg plugin
163
}