~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/errmsg.cc

  • Committer: Brian Aker
  • Date: 2008-11-04 15:39:09 UTC
  • mfrom: (575.1.2 devel)
  • Revision ID: brian@tangent.org-20081104153909-c72hn65udxs1ccal
Merge of Monty's work

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