17
17
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
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
27
#include <sys/types.h>
32
using namespace drizzled;
34
26
/* TODO make this dynamic as needed */
35
27
static const int MAX_MSG_LEN= 32*1024;
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)
188
179
/* if there is no destination filename, dont bother doing anything */
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)
285
276
unsigned char qs[255];
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 : "";
282
dbl= session->db_length;
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",
296
290
session->thread_id,
297
session->getQueryId(),
298
292
// dont need to quote the db name, always CSV safe
299
(int)session->db.length(), 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(),
308
session->total_warn_count);
319
310
// a single write has a kernel thread lock, thus no need mutex guard this
320
311
wrv= write(fd, msgbuf, msgbuf_len);
327
318
static Logging_query *handler= NULL;
329
static int logging_query_plugin_init(drizzled::plugin::Context &context)
320
static int logging_query_plugin_init(drizzled::plugin::Registry ®istry)
331
322
handler= new Logging_query();
332
context.add(handler);
323
registry.add(handler);
328
static int logging_query_plugin_deinit(drizzled::plugin::Registry ®istry)
330
registry.remove(handler);
410
DRIZZLE_DECLARE_PLUGIN
409
drizzle_declare_plugin(logging_query)
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,
422
DRIZZLE_DECLARE_PLUGIN_END;
422
drizzle_declare_plugin_end;