~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/errmsg.cc

Merged plugin-registration.

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
#include <drizzled/server_includes.h>
21
21
#include <drizzled/errmsg.h>
22
22
#include <drizzled/gettext.h>
 
23
#include <vector>
 
24
 
 
25
using namespace std;
 
26
 
 
27
static vector<Error_message_handler *> all_errmsg_handler;
23
28
 
24
29
static bool errmsg_has= false;
25
30
 
 
31
static void add_errmsg_handler(Error_message_handler *handler)
 
32
{
 
33
  all_errmsg_handler.push_back(handler);
 
34
}
 
35
 
 
36
static void remove_errmsg_handler(Error_message_handler *handler)
 
37
{
 
38
  all_errmsg_handler.erase(find(all_errmsg_handler.begin(),
 
39
                                all_errmsg_handler.end(), handler));
 
40
}
 
41
 
26
42
int errmsg_initializer(st_plugin_int *plugin)
27
43
{
28
44
  Error_message_handler *p;
43
59
    }
44
60
  }
45
61
 
46
 
  plugin->data= (void *)p;
 
62
  add_errmsg_handler(p);
 
63
  plugin->data= p;
47
64
  errmsg_has= true;
48
65
 
49
66
  return 0;
54
71
{
55
72
  Error_message_handler *p= static_cast<Error_message_handler *>(plugin->data);
56
73
 
 
74
  remove_errmsg_handler(p);
57
75
  if (plugin->plugin->deinit)
58
76
  {
59
77
    if (plugin->plugin->deinit(p))
72
90
  return 0;
73
91
}
74
92
 
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
 
93
 
 
94
class ErrorMessagePrint : public unary_function<Error_message_handler *, bool>
83
95
{
 
96
  Session *session;
84
97
  int priority;
85
98
  const char *format;
86
99
  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)
 
100
public:
 
101
  ErrorMessagePrint(Session *session_arg, int priority_arg,
 
102
                    const char *format_arg, va_list ap_arg) :
 
103
    unary_function<Error_message_handler *, bool>(), session(session_arg),
 
104
    priority(priority_arg), format(format_arg)
 
105
    {
 
106
      va_copy(ap, ap_arg);
 
107
    }
 
108
 
 
109
  ~ErrorMessagePrint()  { va_end(ap); }
 
110
 
 
111
  inline result_type operator()(argument_type handler)
97
112
  {
98
 
    if (handler->errmsg(session, parms->priority, parms->format, parms->ap))
 
113
    if (handler->errmsg(session, priority, format, ap))
99
114
    {
100
115
      /* 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 */
 
116
         so we can't trust the errmsg api to emit our error messages
 
117
         so we will emit error messages to stderr */
103
118
      /* TRANSLATORS: The leading word "errmsg" is the name
104
119
         of the plugin api, and so should not be translated. */
105
120
      fprintf(stderr,
106
121
              _("errmsg plugin '%s' errmsg() failed"),
107
 
              (char *)plugin_name(plugin));
 
122
              handler->getName().c_str());
108
123
      return true;
109
124
    }
 
125
    return false;
110
126
  }
111
 
  return false;
112
 
}
 
127
};
113
128
 
114
129
bool errmsg_vprintf (Session *session, int priority,
115
130
                     char const *format, va_list ap)
116
131
{
117
 
  bool foreach_rv;
118
 
  errmsg_parms_t parms;
119
132
 
120
133
  /* check to see if any errmsg plugin has been loaded
121
134
     if not, just fall back to emitting the message to stderr */
129
142
    return false;
130
143
  }
131
144
 
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;
 
145
  /* Use find_if instead of foreach so that we can collect return codes */
 
146
  vector<Error_message_handler *>::iterator iter=
 
147
    find_if(all_errmsg_handler.begin(), all_errmsg_handler.end(),
 
148
            ErrorMessagePrint(session, priority, format, ap)); 
 
149
  /* If iter is == end() here, that means that all of the plugins returned
 
150
   * false, which in this case means they all succeeded. Since we want to 
 
151
   * return false on success, we return the value of the two being != 
 
152
   */
 
153
  return iter != all_errmsg_handler.end();
142
154
}
143
155
 
144
156