Drizzled Public API Documentation

module.cc
1 /* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3  *
4  * Copyright 2011 Daniel Nichter
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, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include <config.h>
21 #include <string>
22 #include <boost/program_options.hpp>
23 #include <drizzled/item.h>
24 #include <drizzled/data_home.h>
26 #include <drizzled/session.h>
27 #include <drizzled/plugin.h>
28 #include "query_log.h"
29 
30 namespace po= boost::program_options;
31 using namespace std;
32 using namespace drizzled;
33 
34 namespace drizzle_plugin {
35 
45 bool update_file(Session *, set_var *var);
46 void update_file_enabled(Session *, sql_var_t);
47 
55 static QueryLoggerFile *logger_file= NULL;
56 
60 const char *default_file= "drizzled-queries.log";
61 
78 {
79  const char *new_file= var->value->str_value.ptr();
80 
81  if (not new_file)
82  {
83  errmsg_printf(error::ERROR, _("The query log file name must be defined."));
84  return false;
85  }
86 
87  if (not *new_file)
88  {
89  errmsg_printf(error::ERROR, _("The query log file name must have a value."));
90  return false;
91  }
92 
106  if (query_log->sysvar_file_enabled)
107  {
108  if (logger_file->openLogFile(new_file))
109  {
110  errmsg_printf(error::ERROR, "Cannot open the query log file %s", new_file);
111  return true; // error
112  }
113  }
114 
115  // Update query_log_file in SHOW VARIABLES.
116  query_log->sysvar_file= new_file;
117 
118  return false; // success
119 }
120 
132 void update_file_enabled(Session *, sql_var_t)
133 {
134  if (query_log->sysvar_file_enabled)
135  {
136  if (logger_file->openLogFile(query_log->sysvar_file.c_str()))
137  {
138  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());
139  query_log->sysvar_file_enabled= false;
140  }
141  }
142  else
143  logger_file->closeLogFile();
144 }
145 
158 {
159  logger_file= new QueryLoggerFile();
160  query_log= new drizzled::plugin::QueryLog(true, logger_file);
161 
162  context(
163  "file-enabled",
164  po::value<bool>(&query_log->sysvar_file_enabled)->default_value(false)->zero_tokens(),
165  N_("Enable query logging to file"));
166 
167  context(
168  "file",
169  po::value<string>(&query_log->sysvar_file)->default_value(default_file),
170  N_("Query log file"));
171 
172  context(
173  "threshold-execution-time",
174  po::value<uint32_constraint>(&query_log->sysvar_threshold_execution_time)->default_value(0),
175  _("Threshold for logging slow queries, in microseconds"));
176 
177  context(
178  "threshold-lock-time",
179  po::value<uint32_constraint>(&query_log->sysvar_threshold_lock_time)->default_value(0),
180  _("Threshold for logging long locking queries, in microseconds"));
181 
182  context(
183  "threshold-rows-examined",
184  po::value<uint32_constraint>(&query_log->sysvar_threshold_rows_examined)->default_value(0),
185  _("Threshold for logging queries that examine too many rows, integer"));
186 
187  context(
188  "threshold-rows-sent",
189  po::value<uint32_constraint>(&query_log->sysvar_threshold_rows_sent)->default_value(0),
190  _("Threshold for logging queries that return too many rows, integer"));
191 
192  context(
193  "threshold-tmp-tables",
194  po::value<uint32_constraint>(&query_log->sysvar_threshold_tmp_tables)->default_value(0),
195  _("Threshold for logging queries that use too many temporary tables, integer"));
196 
197  context(
198  "threshold-warnings",
199  po::value<uint32_constraint>(&query_log->sysvar_threshold_warnings)->default_value(0),
200  _("Threshold for logging queries that cause too many warnings, integer"));
201 
202  context(
203  "threshold-session-time",
204  po::value<uint32_constraint>(&query_log->sysvar_threshold_session_time)->default_value(0),
205  _("Threshold for logging queries that are active too long, in seconds"));
206 
207 }
208 
228 static int init(drizzled::module::Context &context)
229 {
230  const module::option_map &vm= context.getOptions();
231 
232  // Open the log file now that we have either an explicit value from the
233  // command line (--query-log.file=FILE) or the default file.
234  if (vm["file-enabled"].as<bool>())
235  logger_file->openLogFile(vm["file"].as<string>().c_str());
236 
237  // Plug into Drizzle!
238  context.add(query_log);
239 
240  // Register our system variables with Drizzle, e.g. query_log_enabled,
241  // query_log_file, etc. in SHOW VARIABLES.
242  context.registerVariable(
243  new sys_var_bool_ptr(
244  "enabled", &query_log->sysvar_enabled));
245 
246  context.registerVariable(
247  new sys_var_bool_ptr(
248  "file_enabled", &query_log->sysvar_file_enabled, &update_file_enabled));
249 
250  context.registerVariable(
251  new sys_var_std_string(
252  "file", query_log->sysvar_file, NULL, &update_file));
253 
254  context.registerVariable(
256  "threshold_execution_time", query_log->sysvar_threshold_execution_time));
257 
258  context.registerVariable(
260  "threshold_lock_time", query_log->sysvar_threshold_lock_time));
261 
262  context.registerVariable(
264  "threshold_rows_examined", query_log->sysvar_threshold_rows_examined));
265 
266  context.registerVariable(
268  "threshold_rows_sent", query_log->sysvar_threshold_rows_sent));
269 
270  context.registerVariable(
272  "threshold_tmp_tables", query_log->sysvar_threshold_tmp_tables));
273 
274  context.registerVariable(
276  "threshold_warnings", query_log->sysvar_threshold_warnings));
277 
278  context.registerVariable(
280  "threshold_session_time", query_log->sysvar_threshold_session_time));
281 
282  return 0; // success
283 }
284 
285 } /* namespace drizzle_plugin */
286 
287 DRIZZLE_DECLARE_PLUGIN
288 {
289  DRIZZLE_VERSION_ID,
290  "query_log",
291  "1.0",
292  "Daniel Nichter",
293  N_("Logs queries to a file"),
294  PLUGIN_LICENSE_GPL,
296  NULL,
298 }
299 DRIZZLE_DECLARE_PLUGIN_END;
bool closeLogFile()
Close the log file.
Definition: file.cc:72
const char * default_file
Default query log file.
Definition: module.cc:60
bool openLogFile(const char *file)
Open new log file, close old log file if successful.
Definition: file.cc:61
TODO: Rename this file - func.h is stupid.
static void init_options(drizzled::module::option_context &context)
Initialize query-log command line options.
Definition: module.cc:157
An Proxy Wrapper around boost::program_options::variables_map.
static drizzled::plugin::QueryLog * query_log
Definition: module.cc:54
static int init(drizzled::module::Context &context)
Add query_log plugin to Drizzle and initalize query_log system variables.
Definition: module.cc:228
QueryLog implements the query_log plugin.
Definition: query_log.h:43
QueryLoggerFile implements logging to a file for the QueryLog class.
Definition: file.h:34
bool update_file(Session *, set_var *var)
Update query_log_file (query_log->sysvar_file).
Definition: module.cc:77
void update_file_enabled(Session *, sql_var_t)
Update query_log_file_enabled (query_log->sysvar_file_enabled).
Definition: module.cc:132
String str_value
Definition: item.h:107