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 class Session;
00029 
00030 namespace drizzled
00031 {
00032 
00033 std::vector<plugin::Logging *> all_loggers;
00034 
00035 
00036 bool plugin::Logging::addPlugin(plugin::Logging *handler)
00037 {
00038   if (handler != NULL)
00039     all_loggers.push_back(handler);
00040   return false;
00041 }
00042 
00043 void plugin::Logging::removePlugin(plugin::Logging *handler)
00044 {
00045   if (handler != NULL)
00046     all_loggers.erase(std::find(all_loggers.begin(), all_loggers.end(), handler));
00047 }
00048 
00049 
00050 class PreIterate : public std::unary_function<plugin::Logging *, bool>
00051 {
00052   Session *session;
00053 public:
00054   PreIterate(Session *session_arg) :
00055     std::unary_function<plugin::Logging *, bool>(),
00056     session(session_arg) {}
00057 
00058   inline result_type operator()(argument_type handler)
00059   {
00060     if (handler->pre(session))
00061     {
00062       /* TRANSLATORS: The leading word "logging" is the name
00063          of the plugin api, and so should not be translated. */
00064       errmsg_printf(error::ERROR,
00065                     _("logging '%s' pre() failed"),
00066                     handler->getName().c_str());
00067       return true;
00068     }
00069     return false;
00070   }
00071 };
00072 
00073 
00074 class PostIterate : public std::unary_function<plugin::Logging *, bool>
00075 {
00076   Session *session;
00077 public:
00078   PostIterate(Session *session_arg) :
00079     std::unary_function<plugin::Logging *, bool>(),
00080     session(session_arg) {}
00081 
00082   /* This gets called once for each loaded logging plugin */
00083   inline result_type operator()(argument_type handler)
00084   {
00085     if (handler->post(session))
00086     {
00087       /* TRANSLATORS: The leading word "logging" is the name
00088          of the plugin api, and so should not be translated. */
00089       errmsg_printf(error::ERROR,
00090                     _("logging '%s' post() failed"),
00091                     handler->getName().c_str());
00092       return true;
00093     }
00094     return false;
00095   }
00096 };
00097 
00098 class PostEndIterate : public std::unary_function<plugin::Logging *, bool>
00099 {
00100   Session *session;
00101 public:
00102   PostEndIterate(Session *session_arg) :
00103     std::unary_function<plugin::Logging *, bool>(),
00104     session(session_arg) {}
00105 
00106   /* This gets called once for each loaded logging plugin */
00107   inline result_type operator()(argument_type handler)
00108   {
00109     if (handler->postEnd(session))
00110     {
00111       /* TRANSLATORS: The leading word "logging" is the name
00112          of the plugin api, and so should not be translated. */
00113       errmsg_printf(error::ERROR,
00114                     _("logging '%s' postEnd() failed"),
00115                     handler->getName().c_str());
00116       return true;
00117     }
00118     return false;
00119   }
00120 };
00121 
00122 class ResetIterate : public std::unary_function<plugin::Logging *, bool>
00123 {
00124   Session *session;
00125 public:
00126   ResetIterate(Session *session_arg) :
00127     std::unary_function<plugin::Logging *, bool>(),
00128     session(session_arg) {}
00129 
00130   inline result_type operator()(argument_type handler)
00131   {
00132     if (handler->resetGlobalScoreboard())
00133     {
00134       /* TRANSLATORS: The leading word "logging" is the name
00135          of the plugin api, and so should not be translated. */
00136       errmsg_printf(error::ERROR,
00137                     _("logging '%s' resetCurrentScoreboard() failed"),
00138                     handler->getName().c_str());
00139       return true;
00140     }
00141     return false;
00142   }
00143 };
00144 
00145 
00146 /* This is the Logging::preDo entry point.
00147    This gets called by the rest of the Drizzle server code */
00148 bool plugin::Logging::preDo(Session *session)
00149 {
00150   /* Use find_if instead of foreach so that we can collect return codes */
00151   std::vector<plugin::Logging *>::iterator iter=
00152     std::find_if(all_loggers.begin(), all_loggers.end(),
00153                  PreIterate(session)); 
00154   /* If iter is == end() here, that means that all of the plugins returned
00155    * false, which in this case means they all succeeded. Since we want to 
00156    * return false on success, we return the value of the two being != 
00157    */
00158   return iter != all_loggers.end();
00159 }
00160 
00161 /* This is the Logging::postDo entry point.
00162    This gets called by the rest of the Drizzle server code */
00163 bool plugin::Logging::postDo(Session *session)
00164 {
00165   /* Use find_if instead of foreach so that we can collect return codes */
00166   std::vector<plugin::Logging *>::iterator iter=
00167     std::find_if(all_loggers.begin(), all_loggers.end(),
00168                  PostIterate(session)); 
00169   /* If iter is == end() here, that means that all of the plugins returned
00170    * false, which in this case means they all succeeded. Since we want to 
00171    * return false on success, we return the value of the two being != 
00172    */
00173   return iter != all_loggers.end();
00174 }
00175 
00176 /* This gets called in the session destructor */
00177 bool plugin::Logging::postEndDo(Session *session)
00178 {
00179   /* Use find_if instead of foreach so that we can collect return codes */
00180   std::vector<plugin::Logging *>::iterator iter=
00181     std::find_if(all_loggers.begin(), all_loggers.end(),
00182                  PostEndIterate(session));
00183   /* If iter is == end() here, that means that all of the plugins returned
00184    * false, which in this case means they all succeeded. Since we want to
00185    * return false on success, we return the value of the two being !=
00186    */
00187   return iter != all_loggers.end();
00188 }
00189 
00190 /* Resets global stats for logging plugin */
00191 bool plugin::Logging::resetStats(Session *session)
00192 {
00193   std::vector<plugin::Logging *>::iterator iter=
00194     std::find_if(all_loggers.begin(), all_loggers.end(),
00195                  ResetIterate(session));
00196 
00197   return iter != all_loggers.end();
00198 }
00199 
00200 } /* namespace drizzled */