~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/logging_query/logging_query.cc

  • Committer: Brian Aker
  • Date: 2009-06-05 19:40:08 UTC
  • mfrom: (1039.4.10 drizzle_log)
  • Revision ID: brian@gaz-20090605194008-mjz1s0mnwv9117nk
Merge Mark

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
#include <drizzled/plugin/logging_handler.h>
22
22
#include <drizzled/gettext.h>
23
23
#include <drizzled/session.h>
 
24
#include <pcre.h>
24
25
 
25
26
/* TODO make this dynamic as needed */
26
27
static const int MAX_MSG_LEN= 32*1024;
27
28
 
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;
34
36
 
35
 
static int fd= -1;
36
 
 
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 */
165
165
}
166
166
 
167
167
 
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
172
169
{
 
170
  int fd;
 
171
  pcre *re;
 
172
  pcre_extra *pe;
 
173
 
173
174
public:
174
 
  Logging_query() : Logging_handler("Logging_query") {}
 
175
 
 
176
  Logging_query() : Logging_handler("Logging_query"), fd(-1), re(NULL), pe(NULL)
 
177
  {
 
178
 
 
179
    /* if there is no destination filename, dont bother doing anything */
 
180
    if (sysvar_logging_query_filename == NULL)
 
181
      return;
 
182
 
 
183
    fd= open(sysvar_logging_query_filename,
 
184
             O_WRONLY | O_APPEND | O_CREAT,
 
185
             S_IRUSR|S_IWUSR);
 
186
    if (fd < 0)
 
187
    {
 
188
      errmsg_printf(ERRMSG_LVL_ERROR, _("fail open() fn=%s er=%s\n"),
 
189
                    sysvar_logging_query_filename,
 
190
                    strerror(errno));
 
191
      return;
 
192
    }
 
193
 
 
194
    if (sysvar_logging_query_pcre != NULL)
 
195
    {
 
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 */
 
202
    }
 
203
  }
 
204
 
 
205
  ~Logging_query()
 
206
  {
 
207
    if (fd >= 0)
 
208
    {
 
209
      close(fd);
 
210
    }
 
211
 
 
212
    if (pe != NULL)
 
213
    {
 
214
      pcre_free(pe);
 
215
    }
 
216
 
 
217
    if (re != NULL)
 
218
    {
 
219
      pcre_free(re);
 
220
    }
 
221
  }
 
222
 
175
223
 
176
224
  virtual bool pre (Session *)
177
225
  {
 
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 */
178
229
    return false;
179
230
  }
180
231
 
215
266
  
216
267
    if ((t_mark - session->start_utime) < (sysvar_logging_query_threshold_slow))
217
268
      return false;
218
 
  
 
269
 
 
270
    if (re)
 
271
    {
 
272
      int this_pcre_rc;
 
273
      this_pcre_rc = pcre_exec(re, pe, session->query, session->query_length, 0, 0, NULL, 0);
 
274
      if (this_pcre_rc < 0)
 
275
        return false;
 
276
    }
 
277
 
219
278
    // buffer to quotify the query
220
279
    unsigned char qs[255];
221
280
  
228
287
    msgbuf_len=
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",
232
292
               t_mark,
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);
250
 
  
 
309
               session->examined_row_count,
 
310
               session->tmp_table,
 
311
               session->total_warn_count);
251
312
  
252
313
    // a single write has a kernel thread lock, thus no need mutex guard this
253
314
    wrv= write(fd, msgbuf, msgbuf_len);
261
322
 
262
323
static int logging_query_plugin_init(PluginRegistry &registry)
263
324
{
264
 
 
265
 
  if (sysvar_logging_query_filename == NULL)
266
 
  {
267
 
    /* no destination filename was specified via system variables
268
 
       return now, dont set the callback pointers
269
 
    */
270
 
    return 0;
271
 
  }
272
 
 
273
 
  fd= open(sysvar_logging_query_filename,
274
 
           O_WRONLY | O_APPEND | O_CREAT,
275
 
           S_IRUSR|S_IWUSR);
276
 
  if (fd < 0)
277
 
  {
278
 
    errmsg_printf(ERRMSG_LVL_ERROR, _("fail open() fn=%s er=%s\n"),
279
 
                  sysvar_logging_query_filename,
280
 
                  strerror(errno));
281
 
 
282
 
    /* TODO
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
288
 
    */
289
 
    return 0;
290
 
  }
291
 
 
292
325
  handler= new Logging_query();
293
326
  registry.add(handler);
294
327
 
297
330
 
298
331
static int logging_query_plugin_deinit(PluginRegistry &registry)
299
332
{
300
 
 
301
 
  if (fd >= 0)
302
 
  {
303
 
    close(fd);
304
 
    fd= -1;
305
 
  }
306
 
 
307
333
  registry.remove(handler);
308
334
  delete handler;
309
335
 
328
354
  NULL, /* update func*/
329
355
  NULL /* default */);
330
356
 
 
357
static DRIZZLE_SYSVAR_STR(
 
358
  pcre,
 
359
  sysvar_logging_query_pcre,
 
360
  PLUGIN_VAR_READONLY,
 
361
  N_("PCRE to match the query against"),
 
362
  NULL, /* check func */
 
363
  NULL, /* update func*/
 
364
  NULL /* default */);
 
365
 
331
366
static DRIZZLE_SYSVAR_ULONG(
332
367
  threshold_slow,
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),