~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/errmsg.cc

update to latest from trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 
3
 *
 
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
 
 
20
#include <drizzled/server_includes.h>
 
21
#include <drizzled/errmsg.h>
 
22
 
 
23
int errmsg_initializer(st_plugin_int *plugin)
 
24
{
 
25
  errmsg_t *p;
 
26
 
 
27
  p= (errmsg_t *) malloc(sizeof(errmsg_t));
 
28
  if (p == NULL) return 1;
 
29
  memset(p, 0, sizeof(errmsg_t));
 
30
 
 
31
  plugin->data= (void *)p;
 
32
 
 
33
  if (plugin->plugin->init)
 
34
  {
 
35
    if (plugin->plugin->init((void *)p))
 
36
    {
 
37
      /* we're doing the errmsg plugin api,
 
38
         so we can't trust the errmsg api to emit our error messages
 
39
         so we will emit error messages to stderr */
 
40
      /* TRANSLATORS: The leading word "errmsg" is the name
 
41
         of the plugin api, and so should not be translated. */
 
42
      fprintf(stderr,
 
43
              _("errmsg plugin '%s' init() failed."),
 
44
              plugin->name.str);
 
45
      goto err;
 
46
    }
 
47
  }
 
48
  return 0;
 
49
 
 
50
err:
 
51
  free(p);
 
52
  return 1;
 
53
}
 
54
 
 
55
int errmsg_finalizer(st_plugin_int *plugin)
 
56
 
57
  errmsg_t *p = (errmsg_t *) plugin->data;
 
58
 
 
59
  if (plugin->plugin->deinit)
 
60
  {
 
61
    if (plugin->plugin->deinit((void *)p))
 
62
    {
 
63
      /* we're doing the errmsg plugin api,
 
64
         so we can't trust the errmsg api to emit our error messages
 
65
         so we will emit error messages to stderr */
 
66
      /* TRANSLATORS: The leading word "errmsg" is the name
 
67
         of the plugin api, and so should not be translated. */
 
68
      fprintf(stderr,
 
69
              _("errmsg plugin '%s' deinit() failed."),
 
70
              plugin->name.str);
 
71
    }
 
72
  }
 
73
 
 
74
  if (p) free(p);
 
75
 
 
76
  return 0;
 
77
}
 
78
 
 
79
/* The plugin_foreach() iterator requires that we
 
80
   convert all the parameters of a plugin api entry point
 
81
   into just one single void ptr, plus the session.
 
82
   So we will take all the additional paramters of errmsg_vprintf,
 
83
   and marshall them into a struct of this type, and
 
84
   then just pass in a pointer to it.
 
85
*/
 
86
typedef struct errmsg_parms_st
 
87
{
 
88
  int priority;
 
89
  const char *format;
 
90
  va_list ap;
 
91
} errmsg_parms_t;
 
92
 
 
93
 
 
94
/* This gets called by plugin_foreach once for each loaded errmsg plugin */
 
95
static bool errmsg_iterate (Session *session, plugin_ref plugin, void *p)
 
96
{
 
97
  errmsg_t *l= plugin_data(plugin, errmsg_t *);
 
98
  errmsg_parms_t *parms= (errmsg_parms_t *) p;
 
99
 
 
100
  if (l && l->errmsg_func)
 
101
  {
 
102
    if (l->errmsg_func(session, parms->priority, parms->format, parms->ap))
 
103
    {
 
104
      /* we're doing the errmsg plugin api,
 
105
         so we can't trust the errmsg api to emit our error messages
 
106
         so we will emit error messages to stderr */
 
107
      /* TRANSLATORS: The leading word "errmsg" is the name
 
108
         of the plugin api, and so should not be translated. */
 
109
      fprintf(stderr,
 
110
              _("errmsg plugin '%s' errmsg_func() failed"),
 
111
              (char *)plugin_name(plugin));
 
112
      return true;
 
113
    }
 
114
  }
 
115
  return false;
 
116
}
 
117
 
 
118
bool errmsg_vprintf (Session *session, int priority, const char *format, va_list ap)
 
119
{
 
120
  bool foreach_rv;
 
121
  errmsg_parms_t parms;
 
122
 
 
123
  /* marshall the parameters so they will fit into the foreach */
 
124
  parms.priority= priority;
 
125
  parms.format= format;
 
126
  va_copy(parms.ap, ap);
 
127
 
 
128
  /* call errmsg_iterate
 
129
     once for each loaded errmsg plugin */
 
130
  foreach_rv= plugin_foreach(session,
 
131
                             errmsg_iterate,
 
132
                             DRIZZLE_ERRMSG_PLUGIN,
 
133
                             (void *) &parms);
 
134
  return foreach_rv;
 
135
}
 
136
 
 
137
bool errmsg_printf (Session *session, int priority, const char *format, ...)
 
138
{
 
139
  bool rv;
 
140
  va_list args;
 
141
  va_start(args, format);
 
142
  rv= errmsg_vprintf(session, priority, format, args);
 
143
  va_end(args);
 
144
  return rv;
 
145
}