Drizzled Public API Documentation

module.cc
00001 /* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
00002  *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
00003  *
00004  *  Copyright 2011 Daniel Nichter
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, either version 3 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
00018  */
00019 
00020 #include <config.h>
00021 #include <string>
00022 #include <boost/program_options.hpp>
00023 #include <drizzled/item.h>
00024 #include <drizzled/data_home.h>
00025 #include <drizzled/module/option_map.h>
00026 #include <drizzled/session.h>
00027 #include <drizzled/plugin.h>
00028 #include "query_log.h"
00029 
00030 namespace po= boost::program_options;
00031 using namespace std;
00032 using namespace drizzled;
00033 
00034 namespace drizzle_plugin {
00035 
00045 bool update_file(Session *, set_var *var);
00046 void update_file_enabled(Session *, sql_var_t);
00047 
00054 static drizzled::plugin::QueryLog *query_log= NULL;
00055 static QueryLoggerFile *logger_file= NULL;
00056 
00060 const char *default_file= "drizzled-queries.log"; 
00061 
00077 bool update_file(Session *, set_var *var)
00078 {
00079   const char *new_file= var->value->str_value.ptr();
00080 
00081   if (not new_file)
00082   {
00083     errmsg_printf(error::ERROR, _("The query log file name must be defined."));
00084     return false;
00085   }
00086 
00087   if (not *new_file)
00088   {
00089     errmsg_printf(error::ERROR, _("The query log file name must have a value."));
00090     return false;
00091   }
00092 
00106   if (query_log->sysvar_file_enabled)
00107   {
00108     if (logger_file->openLogFile(new_file))
00109     {
00110       errmsg_printf(error::ERROR, "Cannot open the query log file %s", new_file);
00111       return true; // error
00112     }
00113   }
00114 
00115   // Update query_log_file in SHOW VARIABLES.
00116   query_log->sysvar_file= new_file;
00117 
00118   return false;  // success
00119 }
00120 
00132 void update_file_enabled(Session *, sql_var_t)
00133 {
00134   if (query_log->sysvar_file_enabled)
00135   {
00136     if (logger_file->openLogFile(query_log->sysvar_file.c_str()))
00137     {
00138       errmsg_printf(error::ERROR, "Cannot enable the query log file because the query log file %s cannot be opened.", query_log->sysvar_file.c_str());
00139       query_log->sysvar_file_enabled= false;
00140     }
00141   }
00142   else
00143     logger_file->closeLogFile();
00144 }
00145 
00157 static void init_options(drizzled::module::option_context &context)
00158 {
00159   logger_file= new QueryLoggerFile();
00160   query_log= new drizzled::plugin::QueryLog(true, logger_file);
00161 
00162   context(
00163     "file-enabled",
00164     po::value<bool>(&query_log->sysvar_file_enabled)->default_value(false)->zero_tokens(),
00165     N_("Enable query logging to file"));
00166 
00167   context(
00168     "file",
00169     po::value<string>(&query_log->sysvar_file)->default_value(default_file),
00170     N_("Query log file"));
00171 
00172   context(
00173     "threshold-execution-time",
00174     po::value<uint32_constraint>(&query_log->sysvar_threshold_execution_time)->default_value(0),
00175     _("Threshold for logging slow queries, in microseconds"));
00176   
00177   context(
00178     "threshold-lock-time",
00179     po::value<uint32_constraint>(&query_log->sysvar_threshold_lock_time)->default_value(0),
00180     _("Threshold for logging long locking queries, in microseconds"));
00181   
00182   context(
00183     "threshold-rows-examined",
00184     po::value<uint32_constraint>(&query_log->sysvar_threshold_rows_examined)->default_value(0),
00185     _("Threshold for logging queries that examine too many rows, integer"));
00186   
00187   context(
00188     "threshold-rows-sent",
00189     po::value<uint32_constraint>(&query_log->sysvar_threshold_rows_sent)->default_value(0),
00190     _("Threshold for logging queries that return too many rows, integer"));
00191   
00192   context(
00193     "threshold-tmp-tables",
00194     po::value<uint32_constraint>(&query_log->sysvar_threshold_tmp_tables)->default_value(0),
00195     _("Threshold for logging queries that use too many temporary tables, integer"));
00196 
00197   context(
00198     "threshold-warnings",
00199     po::value<uint32_constraint>(&query_log->sysvar_threshold_warnings)->default_value(0),
00200     _("Threshold for logging queries that cause too many warnings, integer"));
00201 
00202   context(
00203     "threshold-session-time",
00204     po::value<uint32_constraint>(&query_log->sysvar_threshold_session_time)->default_value(0),
00205     _("Threshold for logging queries that are active too long, in seconds"));
00206 
00207 }
00208 
00228 static int init(drizzled::module::Context &context)
00229 {
00230   const module::option_map &vm= context.getOptions();
00231 
00232   // Open the log file now that we have either an explicit value from the
00233   // command line (--query-log.file=FILE) or the default file.
00234   if (vm["file-enabled"].as<bool>())
00235     logger_file->openLogFile(vm["file"].as<string>().c_str());
00236 
00237   // Plug into Drizzle!
00238   context.add(query_log);
00239 
00240   // Register our system variables with Drizzle, e.g. query_log_enabled,
00241   // query_log_file, etc. in SHOW VARIABLES.
00242   context.registerVariable(
00243     new sys_var_bool_ptr(
00244       "enabled", &query_log->sysvar_enabled));
00245 
00246   context.registerVariable(
00247     new sys_var_bool_ptr(
00248       "file_enabled", &query_log->sysvar_file_enabled, &update_file_enabled));
00249 
00250   context.registerVariable(
00251     new sys_var_std_string(
00252       "file", query_log->sysvar_file, NULL, &update_file));
00253 
00254   context.registerVariable(
00255     new sys_var_constrained_value<uint32_t>(
00256       "threshold_execution_time", query_log->sysvar_threshold_execution_time));
00257   
00258   context.registerVariable(
00259     new sys_var_constrained_value<uint32_t>(
00260       "threshold_lock_time", query_log->sysvar_threshold_lock_time));
00261   
00262   context.registerVariable(
00263     new sys_var_constrained_value<uint32_t>(
00264       "threshold_rows_examined", query_log->sysvar_threshold_rows_examined));
00265   
00266   context.registerVariable(
00267     new sys_var_constrained_value<uint32_t>(
00268       "threshold_rows_sent", query_log->sysvar_threshold_rows_sent));
00269   
00270   context.registerVariable(
00271     new sys_var_constrained_value<uint32_t>(
00272       "threshold_tmp_tables", query_log->sysvar_threshold_tmp_tables));
00273   
00274   context.registerVariable(
00275     new sys_var_constrained_value<uint32_t>(
00276       "threshold_warnings", query_log->sysvar_threshold_warnings));
00277 
00278   context.registerVariable(
00279     new sys_var_constrained_value<uint32_t>(
00280       "threshold_session_time", query_log->sysvar_threshold_session_time));
00281 
00282   return 0; // success
00283 }
00284 
00285 } /* namespace drizzle_plugin */
00286 
00287 DRIZZLE_PLUGIN(drizzle_plugin::init, NULL, drizzle_plugin::init_options);