Drizzled Public API Documentation

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