~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/errmsg.cc

Moved the last of the libdrizzleclient calls into Protocol.

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