drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
error_message.cc
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
22#include <drizzled/error.h>
23#include <drizzled/gettext.h>
24#include <drizzled/plugin/error_message.h>
25
26#include <cstdio>
27#include <algorithm>
28#include <vector>
29
30namespace drizzled
31{
32
33std::vector<plugin::ErrorMessage *> all_errmsg_handler;
34
35bool plugin::ErrorMessage::addPlugin(plugin::ErrorMessage &handler)
36{
37 all_errmsg_handler.push_back(&handler);
38 return false;
39}
40
41void plugin::ErrorMessage::removePlugin(plugin::ErrorMessage &)
42{
43 all_errmsg_handler.clear();
44}
45
46
47class Print
48{
49 error::priority_t priority;
50 const char *format;
51 va_list ap;
52
53public:
54 Print(error::priority_t priority_arg,
55 const char *format_arg, va_list ap_arg) :
56 priority(priority_arg), format(format_arg)
57 {
58 va_copy(ap, ap_arg);
59 }
60
61 ~Print() { va_end(ap); }
62
63 inline bool operator()(plugin::ErrorMessage *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
79 return true;
80 }
81 va_end(handler_ap);
82 return false;
83 }
84};
85
86
87bool plugin::ErrorMessage::vprintf(error::priority_t priority, char const *format, va_list ap)
88{
89 if (priority > error::verbosity())
90 {
91 return false;
92 }
93
94 /*
95 Check to see if any errmsg plugin has been loaded
96 if not, just fall back to emitting the message to stderr.
97 */
98 if (all_errmsg_handler.size() == 0)
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 */
105 vfprintf(stderr, format, ap);
106 fputc('\n', stderr);
107
108 return false;
109 }
110
111 /* Use find_if instead of foreach so that we can collect return codes */
112 std::vector<plugin::ErrorMessage *>::iterator iter=
113 std::find_if(all_errmsg_handler.begin(), all_errmsg_handler.end(),
114 Print(priority, format, ap));
115
116 /* If iter is == end() here, that means that all of the plugins returned
117 * false, which in this case means they all succeeded. Since we want to
118 * return false on success, we return the value of the two being !=
119 */
120 return iter != all_errmsg_handler.end();
121}
122
123} /* namespace drizzled */
TODO: Rename this file - func.h is stupid.