Drizzled Public API Documentation

logging.cc
00001 /* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
00002  *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
00003  *
00004  *  Copyright (C) 2008 Sun Microsystems, Inc.
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; version 2 of the License.
00009  *
00010  *  This program is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *  GNU General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU General Public License
00016  *  along with this program; if not, write to the Free Software
00017  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00018  */
00019 
00020 #include <config.h>
00021 #include <drizzled/plugin/logging.h>
00022 #include <drizzled/gettext.h>
00023 #include <drizzled/errmsg_print.h>
00024 
00025 #include <vector>
00026 #include <algorithm>
00027 
00028 namespace drizzled {
00029 
00030 std::vector<plugin::Logging *> all_loggers;
00031 
00032 bool plugin::Logging::addPlugin(plugin::Logging *handler)
00033 {
00034   if (handler != NULL)
00035     all_loggers.push_back(handler);
00036   return false;
00037 }
00038 
00039 void plugin::Logging::removePlugin(plugin::Logging *handler)
00040 {
00041   if (handler != NULL)
00042     all_loggers.erase(std::find(all_loggers.begin(), all_loggers.end(), handler));
00043 }
00044 
00045 
00046 class PreIterate : public std::unary_function<plugin::Logging *, bool>
00047 {
00048   Session *session;
00049 public:
00050   PreIterate(Session *session_arg) :
00051     std::unary_function<plugin::Logging *, bool>(),
00052     session(session_arg) {}
00053 
00054   inline result_type operator()(argument_type handler)
00055   {
00056     if (handler->pre(session))
00057     {
00058       /* TRANSLATORS: The leading word "logging" is the name
00059          of the plugin api, and so should not be translated. */
00060       errmsg_printf(error::ERROR,
00061                     _("logging '%s' pre() failed"),
00062                     handler->getName().c_str());
00063       return true;
00064     }
00065     return false;
00066   }
00067 };
00068 
00069 
00070 class PostIterate : public std::unary_function<plugin::Logging *, bool>
00071 {
00072   Session *session;
00073 public:
00074   PostIterate(Session *session_arg) :
00075     std::unary_function<plugin::Logging *, bool>(),
00076     session(session_arg) {}
00077 
00078   /* This gets called once for each loaded logging plugin */
00079   inline result_type operator()(argument_type handler)
00080   {
00081     if (handler->post(session))
00082     {
00083       /* TRANSLATORS: The leading word "logging" is the name
00084          of the plugin api, and so should not be translated. */
00085       errmsg_printf(error::ERROR,
00086                     _("logging '%s' post() failed"),
00087                     handler->getName().c_str());
00088       return true;
00089     }
00090     return false;
00091   }
00092 };
00093 
00094 class PostEndIterate : public std::unary_function<plugin::Logging *, bool>
00095 {
00096   Session *session;
00097 public:
00098   PostEndIterate(Session *session_arg) :
00099     std::unary_function<plugin::Logging *, bool>(),
00100     session(session_arg) {}
00101 
00102   /* This gets called once for each loaded logging plugin */
00103   inline result_type operator()(argument_type handler)
00104   {
00105     if (handler->postEnd(session))
00106     {
00107       /* TRANSLATORS: The leading word "logging" is the name
00108          of the plugin api, and so should not be translated. */
00109       errmsg_printf(error::ERROR,
00110                     _("logging '%s' postEnd() failed"),
00111                     handler->getName().c_str());
00112       return true;
00113     }
00114     return false;
00115   }
00116 };
00117 
00118 class ResetIterate : public std::unary_function<plugin::Logging *, bool>
00119 {
00120   Session *session;
00121 public:
00122   ResetIterate(Session *session_arg) :
00123     std::unary_function<plugin::Logging *, bool>(),
00124     session(session_arg) {}
00125 
00126   inline result_type operator()(argument_type handler)
00127   {
00128     if (handler->resetGlobalScoreboard())
00129     {
00130       /* TRANSLATORS: The leading word "logging" is the name
00131          of the plugin api, and so should not be translated. */
00132       errmsg_printf(error::ERROR,
00133                     _("logging '%s' resetCurrentScoreboard() failed"),
00134                     handler->getName().c_str());
00135       return true;
00136     }
00137     return false;
00138   }
00139 };
00140 
00141 
00142 /* This is the Logging::preDo entry point.
00143    This gets called by the rest of the Drizzle server code */
00144 bool plugin::Logging::preDo(Session *session)
00145 {
00146   /* Use find_if instead of foreach so that we can collect return codes */
00147   std::vector<plugin::Logging *>::iterator iter=
00148     std::find_if(all_loggers.begin(), all_loggers.end(),
00149                  PreIterate(session)); 
00150   /* If iter is == end() here, that means that all of the plugins returned
00151    * false, which in this case means they all succeeded. Since we want to 
00152    * return false on success, we return the value of the two being != 
00153    */
00154   return iter != all_loggers.end();
00155 }
00156 
00157 /* This is the Logging::postDo entry point.
00158    This gets called by the rest of the Drizzle server code */
00159 bool plugin::Logging::postDo(Session *session)
00160 {
00161   /* Use find_if instead of foreach so that we can collect return codes */
00162   std::vector<plugin::Logging *>::iterator iter=
00163     std::find_if(all_loggers.begin(), all_loggers.end(),
00164                  PostIterate(session)); 
00165   /* If iter is == end() here, that means that all of the plugins returned
00166    * false, which in this case means they all succeeded. Since we want to 
00167    * return false on success, we return the value of the two being != 
00168    */
00169   return iter != all_loggers.end();
00170 }
00171 
00172 /* This gets called in the session destructor */
00173 bool plugin::Logging::postEndDo(Session *session)
00174 {
00175   /* Use find_if instead of foreach so that we can collect return codes */
00176   std::vector<plugin::Logging *>::iterator iter=
00177     std::find_if(all_loggers.begin(), all_loggers.end(),
00178                  PostEndIterate(session));
00179   /* If iter is == end() here, that means that all of the plugins returned
00180    * false, which in this case means they all succeeded. Since we want to
00181    * return false on success, we return the value of the two being !=
00182    */
00183   return iter != all_loggers.end();
00184 }
00185 
00186 /* Resets global stats for logging plugin */
00187 bool plugin::Logging::resetStats(Session *session)
00188 {
00189   std::vector<plugin::Logging *>::iterator iter=
00190     std::find_if(all_loggers.begin(), all_loggers.end(),
00191                  ResetIterate(session));
00192 
00193   return iter != all_loggers.end();
00194 }
00195 
00196 } /* namespace drizzled */