~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/errmsg.cc

Merge of Jay

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 Sun Microsystems
 
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
#include "drizzled/plugin/registry.h"
 
24
 
 
25
#include <vector>
 
26
 
 
27
using namespace std;
 
28
 
 
29
static vector<Error_message_handler *> all_errmsg_handler;
 
30
 
 
31
static bool errmsg_has= false;
 
32
 
 
33
void add_errmsg_handler(Error_message_handler *handler)
 
34
{
 
35
  all_errmsg_handler.push_back(handler);
 
36
  errmsg_has= true;
 
37
}
 
38
 
 
39
void remove_errmsg_handler(Error_message_handler *handler)
 
40
{
 
41
  all_errmsg_handler.erase(find(all_errmsg_handler.begin(),
 
42
                                all_errmsg_handler.end(), handler));
 
43
}
 
44
 
 
45
 
 
46
class ErrorMessagePrint : public unary_function<Error_message_handler *, bool>
 
47
{
 
48
  Session *session;
 
49
  int priority;
 
50
  const char *format;
 
51
  va_list ap;
 
52
public:
 
53
  ErrorMessagePrint(Session *session_arg, int priority_arg,
 
54
                    const char *format_arg, va_list ap_arg) :
 
55
    unary_function<Error_message_handler *, bool>(), session(session_arg),
 
56
    priority(priority_arg), format(format_arg)
 
57
    {
 
58
      va_copy(ap, ap_arg);
 
59
    }
 
60
 
 
61
  ~ErrorMessagePrint()  { va_end(ap); }
 
62
 
 
63
  inline result_type operator()(argument_type handler)
 
64
  {
 
65
    if (handler->errmsg(session, priority, format, ap))
 
66
    {
 
67
      /* we're doing the errmsg plugin api,
 
68
         so we can't trust the errmsg api to emit our error messages
 
69
         so we will emit error messages to stderr */
 
70
      /* TRANSLATORS: The leading word "errmsg" is the name
 
71
         of the plugin api, and so should not be translated. */
 
72
      fprintf(stderr,
 
73
              _("errmsg plugin '%s' errmsg() failed"),
 
74
              handler->getName().c_str());
 
75
      return true;
 
76
    }
 
77
    return false;
 
78
  }
 
79
};
 
80
 
 
81
bool errmsg_vprintf (Session *session, int priority,
 
82
                     char const *format, va_list ap)
 
83
{
 
84
 
 
85
  /* check to see if any errmsg plugin has been loaded
 
86
     if not, just fall back to emitting the message to stderr */
 
87
  if (!errmsg_has)
 
88
  {
 
89
    /* if it turns out that the vfprintf doesnt do one single write
 
90
       (single writes are atomic), then this needs to be rewritten to
 
91
       vsprintf into a char buffer, and then write() that char buffer
 
92
       to stderr */
 
93
    vfprintf(stderr, format, ap);
 
94
    return false;
 
95
  }
 
96
 
 
97
  /* Use find_if instead of foreach so that we can collect return codes */
 
98
  vector<Error_message_handler *>::iterator iter=
 
99
    find_if(all_errmsg_handler.begin(), all_errmsg_handler.end(),
 
100
            ErrorMessagePrint(session, priority, format, ap)); 
 
101
  /* If iter is == end() here, that means that all of the plugins returned
 
102
   * false, which in this case means they all succeeded. Since we want to 
 
103
   * return false on success, we return the value of the two being != 
 
104
   */
 
105
  return iter != all_errmsg_handler.end();
 
106
}
 
107
 
 
108