~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-08-15 00:59:30 UTC
  • mfrom: (1115.1.7 merge)
  • Revision ID: brian@gaz-20090815005930-q47yenjrq1esiwsz
Merge of Trond + Brian

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
18
 */
19
19
 
20
 
#include "config.h"
21
 
#include <drizzled/plugin/logging.h>
 
20
#include <drizzled/server_includes.h>
 
21
#include <drizzled/plugin/logging_handler.h>
22
22
#include <drizzled/gettext.h>
23
23
#include <drizzled/session.h>
24
24
#include PCRE_HEADER
25
 
#include <limits.h>
26
 
#include <sys/time.h>
27
 
#include <sys/types.h>
28
 
#include <sys/stat.h>
29
 
#include <fcntl.h>
30
 
 
31
 
 
32
 
using namespace drizzled;
33
25
 
34
26
/* TODO make this dynamic as needed */
35
27
static const int MAX_MSG_LEN= 32*1024;
46
38
   until the Session has a good utime "now" we can use
47
39
   will have to use this instead */
48
40
 
 
41
#include <sys/time.h>
49
42
static uint64_t get_microtime()
50
43
{
51
44
#if defined(HAVE_GETHRTIME)
172
165
}
173
166
 
174
167
 
175
 
class Logging_query: public drizzled::plugin::Logging
 
168
class Logging_query: public Logging_handler
176
169
{
177
170
  int fd;
178
171
  pcre *re;
180
173
 
181
174
public:
182
175
 
183
 
  Logging_query()
184
 
    : drizzled::plugin::Logging("Logging_query"),
185
 
      fd(-1), re(NULL), pe(NULL)
 
176
  Logging_query() : Logging_handler("Logging_query"), fd(-1), re(NULL), pe(NULL)
186
177
  {
187
178
 
188
179
    /* if there is no destination filename, dont bother doing anything */
276
267
    if (re)
277
268
    {
278
269
      int this_pcre_rc;
279
 
      this_pcre_rc = pcre_exec(re, pe, session->query.c_str(), session->query.length(), 0, 0, NULL, 0);
 
270
      this_pcre_rc = pcre_exec(re, pe, session->query, session->query_length, 0, 0, NULL, 0);
280
271
      if (this_pcre_rc < 0)
281
272
        return false;
282
273
    }
285
276
    unsigned char qs[255];
286
277
  
287
278
    // to avoid trying to printf %s something that is potentially NULL
288
 
    const char *dbs= session->db.empty() ? "" : session->db.c_str();
 
279
    const char *dbs= (session->db) ? session->db : "";
 
280
    int dbl= 0;
 
281
    if (dbs != NULL)
 
282
      dbl= session->db_length;
289
283
  
290
284
    msgbuf_len=
291
285
      snprintf(msgbuf, MAX_MSG_LEN,
292
286
               "%"PRIu64",%"PRIu64",%"PRIu64",\"%.*s\",\"%s\",\"%.*s\","
293
287
               "%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64","
294
 
               "%"PRIu32",%"PRIu32",%"PRIu32",\"%s\"\n",
 
288
               "%"PRIu32",%"PRIu32"\n",
295
289
               t_mark,
296
290
               session->thread_id,
297
 
               session->getQueryId(),
 
291
               session->query_id,
298
292
               // dont need to quote the db name, always CSV safe
299
 
               (int)session->db.length(), dbs,
 
293
               dbl, dbs,
300
294
               // do need to quote the query
301
 
               quotify((unsigned char *)session->getQueryString().c_str(),
302
 
                       session->getQueryLength(), qs, sizeof(qs)),
 
295
               quotify((unsigned char *)session->query,
 
296
                       session->query_length, qs, sizeof(qs)),
303
297
               // command_name is defined in drizzled/sql_parse.cc
304
298
               // dont need to quote the command name, always CSV safe
305
299
               (int)command_name[session->command].length,
311
305
               session->sent_row_count,
312
306
               session->examined_row_count,
313
307
               session->tmp_table,
314
 
               session->total_warn_count,
315
 
               session->getServerId(),
316
 
               glob_hostname
317
 
               );
 
308
               session->total_warn_count);
318
309
  
319
310
    // a single write has a kernel thread lock, thus no need mutex guard this
320
311
    wrv= write(fd, msgbuf, msgbuf_len);
326
317
 
327
318
static Logging_query *handler= NULL;
328
319
 
329
 
static int logging_query_plugin_init(drizzled::plugin::Context &context)
 
320
static int logging_query_plugin_init(drizzled::plugin::Registry &registry)
330
321
{
331
322
  handler= new Logging_query();
332
 
  context.add(handler);
 
323
  registry.add(handler);
 
324
 
 
325
  return 0;
 
326
}
 
327
 
 
328
static int logging_query_plugin_deinit(drizzled::plugin::Registry &registry)
 
329
{
 
330
  registry.remove(handler);
 
331
  delete handler;
333
332
 
334
333
  return 0;
335
334
}
397
396
  UINT32_MAX, /* max */
398
397
  0 /* blksiz */);
399
398
 
400
 
static drizzle_sys_var* logging_query_system_variables[]= {
 
399
static struct st_mysql_sys_var* logging_query_system_variables[]= {
401
400
  DRIZZLE_SYSVAR(enable),
402
401
  DRIZZLE_SYSVAR(filename),
403
402
  DRIZZLE_SYSVAR(pcre),
407
406
  NULL
408
407
};
409
408
 
410
 
DRIZZLE_DECLARE_PLUGIN
 
409
drizzle_declare_plugin(logging_query)
411
410
{
412
 
  DRIZZLE_VERSION_ID,
413
411
  "logging_query",
414
412
  "0.2",
415
413
  "Mark Atwood <mark@fallenpegasus.com>",
416
414
  N_("Log queries to a CSV file"),
417
415
  PLUGIN_LICENSE_GPL,
418
416
  logging_query_plugin_init,
 
417
  logging_query_plugin_deinit,
 
418
  NULL,   /* status variables */
419
419
  logging_query_system_variables,
420
420
  NULL
421
421
}
422
 
DRIZZLE_DECLARE_PLUGIN_END;
 
422
drizzle_declare_plugin_end;