drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
drizzled/plugin/logging.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#include <drizzled/plugin/logging.h>
22#include <drizzled/gettext.h>
23#include <drizzled/errmsg_print.h>
24
25#include <vector>
26#include <algorithm>
27
28namespace drizzled {
29
30std::vector<plugin::Logging *> all_loggers;
31
32bool plugin::Logging::addPlugin(plugin::Logging &handler)
33{
34 all_loggers.push_back(&handler);
35 return false;
36}
37
38void plugin::Logging::removePlugin(plugin::Logging &handler)
39{
40 all_loggers.erase(std::find(all_loggers.begin(), all_loggers.end(), &handler));
41}
42
43
44class PreIterate
45{
46 Session *session;
47public:
48 PreIterate(Session *session_arg) :
49 session(session_arg) {}
50
51 inline bool operator()(plugin::Logging *handler)
52 {
53 if (handler->pre(session))
54 {
55 /* TRANSLATORS: The leading word "logging" is the name
56 of the plugin api, and so should not be translated. */
57 errmsg_printf(error::ERROR,
58 _("logging '%s' pre() failed"),
59 handler->getName().c_str());
60 return true;
61 }
62 return false;
63 }
64};
65
66
67class PostIterate
68{
69 Session *session;
70public:
71 PostIterate(Session *session_arg) :
72 session(session_arg) {}
73
74 /* This gets called once for each loaded logging plugin */
75 inline bool operator()(plugin::Logging *handler)
76 {
77 if (handler->post(session))
78 {
79 /* TRANSLATORS: The leading word "logging" is the name
80 of the plugin api, and so should not be translated. */
81 errmsg_printf(error::ERROR,
82 _("logging '%s' post() failed"),
83 handler->getName().c_str());
84 return true;
85 }
86 return false;
87 }
88};
89
90class PostEndIterate
91{
92 Session *session;
93public:
94 PostEndIterate(Session *session_arg) :
95 session(session_arg) {}
96
97 /* This gets called once for each loaded logging plugin */
98 inline bool operator()(plugin::Logging *handler)
99 {
100 if (handler->postEnd(session))
101 {
102 /* TRANSLATORS: The leading word "logging" is the name
103 of the plugin api, and so should not be translated. */
104 errmsg_printf(error::ERROR,
105 _("logging '%s' postEnd() failed"),
106 handler->getName().c_str());
107 return true;
108 }
109 return false;
110 }
111};
112
113class ResetIterate
114{
115 Session *session;
116public:
117 ResetIterate(Session *session_arg) :
118 session(session_arg) {}
119
120 inline bool operator()(plugin::Logging *handler)
121 {
122 if (handler->resetGlobalScoreboard())
123 {
124 /* TRANSLATORS: The leading word "logging" is the name
125 of the plugin api, and so should not be translated. */
126 errmsg_printf(error::ERROR,
127 _("logging '%s' resetCurrentScoreboard() failed"),
128 handler->getName().c_str());
129 return true;
130 }
131 return false;
132 }
133};
134
135
136/* This is the Logging::preDo entry point.
137 This gets called by the rest of the Drizzle server code */
138bool plugin::Logging::preDo(Session *session)
139{
140 /* Use find_if instead of foreach so that we can collect return codes */
141 std::vector<plugin::Logging *>::iterator iter=
142 std::find_if(all_loggers.begin(), all_loggers.end(),
143 PreIterate(session));
144 /* If iter is == end() here, that means that all of the plugins returned
145 * false, which in this case means they all succeeded. Since we want to
146 * return false on success, we return the value of the two being !=
147 */
148 return iter != all_loggers.end();
149}
150
151/* This is the Logging::postDo entry point.
152 This gets called by the rest of the Drizzle server code */
153bool plugin::Logging::postDo(Session *session)
154{
155 /* Use find_if instead of foreach so that we can collect return codes */
156 std::vector<plugin::Logging *>::iterator iter=
157 std::find_if(all_loggers.begin(), all_loggers.end(),
158 PostIterate(session));
159 /* If iter is == end() here, that means that all of the plugins returned
160 * false, which in this case means they all succeeded. Since we want to
161 * return false on success, we return the value of the two being !=
162 */
163 return iter != all_loggers.end();
164}
165
166/* This gets called in the session destructor */
167bool plugin::Logging::postEndDo(Session *session)
168{
169 /* Use find_if instead of foreach so that we can collect return codes */
170 std::vector<plugin::Logging *>::iterator iter=
171 std::find_if(all_loggers.begin(), all_loggers.end(),
172 PostEndIterate(session));
173 /* If iter is == end() here, that means that all of the plugins returned
174 * false, which in this case means they all succeeded. Since we want to
175 * return false on success, we return the value of the two being !=
176 */
177 return iter != all_loggers.end();
178}
179
180/* Resets global stats for logging plugin */
181bool plugin::Logging::resetStats(Session *session)
182{
183 std::vector<plugin::Logging *>::iterator iter=
184 std::find_if(all_loggers.begin(), all_loggers.end(),
185 ResetIterate(session));
186
187 return iter != all_loggers.end();
188}
189
190} /* namespace drizzled */
TODO: Rename this file - func.h is stupid.