~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/plugin/error_message.cc

  • Committer: Monty Taylor
  • Date: 2008-09-16 00:00:48 UTC
  • mto: This revision was merged to the branch mainline in revision 391.
  • Revision ID: monty@inaugust.com-20080916000048-3rvrv3gv9l0ad3gs
Fixed copyright headers in drizzled/

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
 
 
34
 
bool plugin::ErrorMessage::addPlugin(plugin::ErrorMessage *handler)
35
 
{
36
 
  all_errmsg_handler.push_back(handler);
37
 
  return false;
38
 
}
39
 
 
40
 
void plugin::ErrorMessage::removePlugin(plugin::ErrorMessage *)
41
 
{
42
 
  all_errmsg_handler.clear();
43
 
}
44
 
 
45
 
 
46
 
class Print : public std::unary_function<plugin::ErrorMessage *, bool>
47
 
{
48
 
  error::level_t priority;
49
 
  const char *format;
50
 
  va_list ap;
51
 
 
52
 
public:
53
 
  Print(error::level_t priority_arg,
54
 
        const char *format_arg, va_list ap_arg) : 
55
 
    std::unary_function<plugin::ErrorMessage *, bool>(),
56
 
    priority(priority_arg), format(format_arg)
57
 
  {
58
 
    va_copy(ap, ap_arg);
59
 
  }
60
 
 
61
 
  ~Print()  { va_end(ap); }
62
 
 
63
 
  inline result_type operator()(argument_type handler)
64
 
  {
65
 
    va_list handler_ap;
66
 
    va_copy(handler_ap, ap);
67
 
    if (handler->errmsg(priority, format, handler_ap))
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' errmsg() failed"),
76
 
              handler->getName().c_str());
77
 
      va_end(handler_ap);
78
 
      return true;
79
 
    }
80
 
    va_end(handler_ap);
81
 
    return false;
82
 
  }
83
 
}; 
84
 
 
85
 
 
86
 
bool plugin::ErrorMessage::vprintf(error::level_t priority, char const *format, va_list ap)
87
 
{
88
 
 
89
 
  /* 
90
 
    Check to see if any errmsg plugin has been loaded
91
 
    if not, just fall back to emitting the message to stderr.
92
 
  */
93
 
  if (not all_errmsg_handler.size())
94
 
  {
95
 
    /* if it turns out that the vfprintf doesnt do one single write
96
 
       (single writes are atomic), then this needs to be rewritten to
97
 
       vsprintf into a char buffer, and then write() that char buffer
98
 
       to stderr */
99
 
    vfprintf(stderr, format, ap);
100
 
    fputc('\n', stderr);
101
 
    return false;
102
 
  }
103
 
 
104
 
  /* Use find_if instead of foreach so that we can collect return codes */
105
 
  std::vector<plugin::ErrorMessage *>::iterator iter=
106
 
    std::find_if(all_errmsg_handler.begin(), all_errmsg_handler.end(),
107
 
                 Print(priority, format, ap)); 
108
 
 
109
 
  /* If iter is == end() here, that means that all of the plugins returned
110
 
   * false, which in this case means they all succeeded. Since we want to 
111
 
   * return false on success, we return the value of the two being != 
112
 
   */
113
 
  return iter != all_errmsg_handler.end();
114
 
}
115
 
 
116
 
} /* namespace drizzled */