~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/plugin/error_message.cc

  • Committer: Brian Aker
  • Date: 2010-12-18 18:24:57 UTC
  • mfrom: (1999.6.3 trunk)
  • Revision ID: brian@tangent.org-20101218182457-yi1wd0so2hml1k1w
Merge in Lee's copyright header fix

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