~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/errmsg.cc

  • Committer: Jay Pipes
  • Date: 2008-12-18 15:55:03 UTC
  • mto: This revision was merged to the branch mainline in revision 717.
  • Revision ID: jpipes@serialcoder-20081218155503-u45ygyunrdyyvquq
Fix for Bug#308457.  Gave UTF8 enclosure and escape character on LOAD DATA INFILE and changed the error message to be more descriptive

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
3
 *
4
 
 *  Copyright (C) 2008 Sun Microsystems, Inc.
 
4
 *  Copyright (C) 2008 Mark Atwood
5
5
 *
6
6
 *  This program is free software; you can redistribute it and/or modify
7
7
 *  it under the terms of the GNU General Public License as published by
17
17
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
18
 */
19
19
 
20
 
#include <config.h>
21
 
 
22
 
#include <drizzled/error.h>
 
20
#include <drizzled/server_includes.h>
 
21
#include <drizzled/errmsg.h>
23
22
#include <drizzled/gettext.h>
24
 
#include <drizzled/plugin/error_message.h>
25
 
 
26
 
#include <cstdio>
27
 
#include <algorithm>
28
 
#include <vector>
29
 
 
30
 
namespace drizzled
31
 
{
32
 
 
33
 
std::vector<plugin::ErrorMessage *> all_errmsg_handler;
34
 
 
35
 
bool plugin::ErrorMessage::addPlugin(plugin::ErrorMessage *handler)
36
 
{
37
 
  all_errmsg_handler.push_back(handler);
38
 
  return false;
39
 
}
40
 
 
41
 
void plugin::ErrorMessage::removePlugin(plugin::ErrorMessage *)
42
 
{
43
 
  all_errmsg_handler.clear();
44
 
}
45
 
 
46
 
 
47
 
class Print : public std::unary_function<plugin::ErrorMessage *, bool>
48
 
{
49
 
  error::level_t priority;
 
23
 
 
24
static bool errmsg_has= false;
 
25
 
 
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
    {
 
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);
 
48
      goto err;
 
49
    }
 
50
  }
 
51
 
 
52
  errmsg_has= true;
 
53
 
 
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
    {
 
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);
 
77
    }
 
78
  }
 
79
 
 
80
  if (p) free(p);
 
81
 
 
82
  return 0;
 
83
}
 
84
 
 
85
/* The plugin_foreach() iterator requires that we
 
86
   convert all the parameters of a plugin api entry point
 
87
   into just one single void ptr, plus the session.
 
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;
50
95
  const char *format;
51
96
  va_list ap;
52
 
 
53
 
public:
54
 
  Print(error::level_t priority_arg,
55
 
        const char *format_arg, va_list ap_arg) : 
56
 
    std::unary_function<plugin::ErrorMessage *, bool>(),
57
 
    priority(priority_arg), format(format_arg)
58
 
  {
59
 
    va_copy(ap, ap_arg);
60
 
  }
61
 
 
62
 
  ~Print()  { va_end(ap); }
63
 
 
64
 
  inline result_type operator()(argument_type handler)
65
 
  {
66
 
    va_list handler_ap;
67
 
    va_copy(handler_ap, ap);
68
 
    if (handler->errmsg(priority, format, handler_ap))
 
97
} errmsg_parms_t;
 
98
 
 
99
 
 
100
/* This gets called by plugin_foreach once for each loaded errmsg plugin */
 
101
static bool errmsg_iterate (Session *session, plugin_ref plugin, void *p)
 
102
{
 
103
  errmsg_t *l= plugin_data(plugin, errmsg_t *);
 
104
  errmsg_parms_t *parms= (errmsg_parms_t *) p;
 
105
 
 
106
  if (l && l->errmsg_func)
 
107
  {
 
108
    if (l->errmsg_func(session, parms->priority, parms->format, parms->ap))
69
109
    {
70
110
      /* we're doing the errmsg plugin api,
71
 
         so we can't trust the errmsg api to emit our error messages
72
 
         so we will emit error messages to stderr */
 
111
         so we can't trust the errmsg api to emit our error messages
 
112
         so we will emit error messages to stderr */
73
113
      /* TRANSLATORS: The leading word "errmsg" is the name
74
114
         of the plugin api, and so should not be translated. */
75
115
      fprintf(stderr,
76
 
              _("errmsg plugin '%s' errmsg() failed"),
77
 
              handler->getName().c_str());
78
 
      va_end(handler_ap);
 
116
              _("errmsg plugin '%s' errmsg_func() failed"),
 
117
              (char *)plugin_name(plugin));
79
118
      return true;
80
119
    }
81
 
    va_end(handler_ap);
82
 
    return false;
83
120
  }
84
 
}; 
85
 
 
86
 
 
87
 
bool plugin::ErrorMessage::vprintf(error::level_t priority, char const *format, va_list ap)
 
121
  return false;
 
122
}
 
123
 
 
124
bool errmsg_vprintf (Session *session, int priority, const char *format, va_list ap)
88
125
{
89
 
  if (not (priority >= error::verbosity()))
90
 
    return false;
 
126
  bool foreach_rv;
 
127
  errmsg_parms_t parms;
91
128
 
92
 
  /* 
93
 
    Check to see if any errmsg plugin has been loaded
94
 
    if not, just fall back to emitting the message to stderr.
95
 
  */
96
 
  if (not all_errmsg_handler.size())
 
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)
97
132
  {
98
133
    /* if it turns out that the vfprintf doesnt do one single write
99
134
       (single writes are atomic), then this needs to be rewritten to
100
135
       vsprintf into a char buffer, and then write() that char buffer
101
136
       to stderr */
102
 
      vfprintf(stderr, format, ap);
103
 
      fputc('\n', stderr);
 
137
    vfprintf(stderr, format, ap);
104
138
    return false;
105
139
  }
106
140
 
107
 
  /* Use find_if instead of foreach so that we can collect return codes */
108
 
  std::vector<plugin::ErrorMessage *>::iterator iter=
109
 
    std::find_if(all_errmsg_handler.begin(), all_errmsg_handler.end(),
110
 
                 Print(priority, format, ap)); 
111
 
 
112
 
  /* If iter is == end() here, that means that all of the plugins returned
113
 
   * false, which in this case means they all succeeded. Since we want to 
114
 
   * return false on success, we return the value of the two being != 
115
 
   */
116
 
  return iter != all_errmsg_handler.end();
117
 
}
118
 
 
119
 
} /* namespace drizzled */
 
141
  /* marshall the parameters so they will fit into the foreach */
 
142
  parms.priority= priority;
 
143
  parms.format= format;
 
144
  va_copy(parms.ap, ap);
 
145
 
 
146
  /* call errmsg_iterate
 
147
     once for each loaded errmsg plugin */
 
148
  foreach_rv= plugin_foreach(session, errmsg_iterate,
 
149
                             DRIZZLE_ERRMSG_PLUGIN, (void *) &parms);
 
150
  return foreach_rv;
 
151
}
 
152
 
 
153
bool errmsg_printf (Session *session, int priority, const char *format, ...)
 
154
{
 
155
  bool rv;
 
156
  va_list args;
 
157
  va_start(args, format);
 
158
  rv= errmsg_vprintf(session, priority, format, args);
 
159
  va_end(args);
 
160
  return rv;
 
161
}