21
21
#include <drizzled/plugin/logging_handler.h>
22
22
#include <drizzled/gettext.h>
23
23
#include <drizzled/session.h>
25
26
/* TODO make this dynamic as needed */
26
27
static const int MAX_MSG_LEN= 32*1024;
28
29
static bool sysvar_logging_query_enable= false;
29
30
static char* sysvar_logging_query_filename= NULL;
30
/* TODO fix these to not be unsigned long one we have sensible sys_var system */
31
static char* sysvar_logging_query_pcre= NULL;
32
/* TODO fix these to not be unsigned long once we have sensible sys_var system */
31
33
static unsigned long sysvar_logging_query_threshold_slow= 0;
32
34
static unsigned long sysvar_logging_query_threshold_big_resultset= 0;
33
35
static unsigned long sysvar_logging_query_threshold_big_examined= 0;
37
37
/* stolen from mysys/my_getsystime
38
38
until the Session has a good utime "now" we can use
39
39
will have to use this instead */
168
/* we could just not have a pre entrypoint at all,
169
and have logging_pre == NULL
170
but we have this here for the sake of being an example */
171
168
class Logging_query: public Logging_handler
174
Logging_query() : Logging_handler("Logging_query") {}
176
Logging_query() : Logging_handler("Logging_query"), fd(-1), re(NULL), pe(NULL)
179
/* if there is no destination filename, dont bother doing anything */
180
if (sysvar_logging_query_filename == NULL)
183
fd= open(sysvar_logging_query_filename,
184
O_WRONLY | O_APPEND | O_CREAT,
188
errmsg_printf(ERRMSG_LVL_ERROR, _("fail open() fn=%s er=%s\n"),
189
sysvar_logging_query_filename,
194
if (sysvar_logging_query_pcre != NULL)
196
const char *this_pcre_error;
197
int this_pcre_erroffset;
198
re= pcre_compile(sysvar_logging_query_pcre, 0, &this_pcre_error,
199
&this_pcre_erroffset, NULL);
200
pe= pcre_study(re, 0, &this_pcre_error);
201
/* TODO emit error messages if there is a problem */
176
224
virtual bool pre (Session *)
226
/* we could just not have a pre entrypoint at all,
227
and have logging_pre == NULL
228
but we have this here for the sake of being an example */
216
267
if ((t_mark - session->start_utime) < (sysvar_logging_query_threshold_slow))
273
this_pcre_rc = pcre_exec(re, pe, session->query, session->query_length, 0, 0, NULL, 0);
274
if (this_pcre_rc < 0)
219
278
// buffer to quotify the query
220
279
unsigned char qs[255];
229
288
snprintf(msgbuf, MAX_MSG_LEN,
230
289
"%"PRIu64",%"PRIu64",%"PRIu64",\"%.*s\",\"%s\",\"%.*s\","
231
"%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64"\n",
290
"%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64
291
"%"PRIu32",%"PRIu32"\n",
233
293
session->thread_id,
234
294
session->query_id,
246
306
(t_mark - session->start_utime),
247
307
(t_mark - session->utime_after_lock),
248
308
session->sent_row_count,
249
session->examined_row_count);
309
session->examined_row_count,
311
session->total_warn_count);
252
313
// a single write has a kernel thread lock, thus no need mutex guard this
253
314
wrv= write(fd, msgbuf, msgbuf_len);
262
323
static int logging_query_plugin_init(PluginRegistry ®istry)
265
if (sysvar_logging_query_filename == NULL)
267
/* no destination filename was specified via system variables
268
return now, dont set the callback pointers
273
fd= open(sysvar_logging_query_filename,
274
O_WRONLY | O_APPEND | O_CREAT,
278
errmsg_printf(ERRMSG_LVL_ERROR, _("fail open() fn=%s er=%s\n"),
279
sysvar_logging_query_filename,
283
we should return an error here, so the plugin doesnt load
284
but this causes Drizzle to crash
285
so until that is fixed,
286
just return a success,
287
but leave the function pointers as NULL and the fd as -1
292
325
handler= new Logging_query();
293
326
registry.add(handler);
328
354
NULL, /* update func*/
329
355
NULL /* default */);
357
static DRIZZLE_SYSVAR_STR(
359
sysvar_logging_query_pcre,
361
N_("PCRE to match the query against"),
362
NULL, /* check func */
363
NULL, /* update func*/
331
366
static DRIZZLE_SYSVAR_ULONG(
333
368
sysvar_logging_query_threshold_slow,
367
402
static struct st_mysql_sys_var* logging_query_system_variables[]= {
368
403
DRIZZLE_SYSVAR(enable),
369
404
DRIZZLE_SYSVAR(filename),
405
DRIZZLE_SYSVAR(pcre),
370
406
DRIZZLE_SYSVAR(threshold_slow),
371
407
DRIZZLE_SYSVAR(threshold_big_resultset),
372
408
DRIZZLE_SYSVAR(threshold_big_examined),