1
/* drizzle/plugin/logging_query/logging_query.cc */
3
/* need to define DRIZZLE_SERVER to get inside the THD */
4
#define DRIZZLE_SERVER 1
5
#include <drizzled/server_includes.h>
6
#include <drizzled/plugin_logging.h>
8
#define MAX_MSG_LEN (32*1024)
12
// copied from drizzled/sql_parse.cc
14
const LEX_STRING command_name[]={
15
{ C_STRING_WITH_LEN("Sleep") },
16
{ C_STRING_WITH_LEN("Quit") },
17
{ C_STRING_WITH_LEN("InitDB") },
18
{ C_STRING_WITH_LEN("Query") },
19
{ C_STRING_WITH_LEN("FieldList") },
20
{ C_STRING_WITH_LEN("CreateDB") },
21
{ C_STRING_WITH_LEN("DropDB") },
22
{ C_STRING_WITH_LEN("Refresh") },
23
{ C_STRING_WITH_LEN("Shutdown") },
24
{ C_STRING_WITH_LEN("Processlist") },
25
{ C_STRING_WITH_LEN("Connect") },
26
{ C_STRING_WITH_LEN("Kill") },
27
{ C_STRING_WITH_LEN("Ping") },
28
{ C_STRING_WITH_LEN("Time") },
29
{ C_STRING_WITH_LEN("ChangeUser") },
30
{ C_STRING_WITH_LEN("BinlogDump") },
31
{ C_STRING_WITH_LEN("ConnectOut") },
32
{ C_STRING_WITH_LEN("RegisterSlave") },
33
{ C_STRING_WITH_LEN("SetOption") },
34
{ C_STRING_WITH_LEN("Daemon") },
35
{ C_STRING_WITH_LEN("Error") }
39
bool logging_query_func_pre (THD *thd)
41
char msgbuf[MAX_MSG_LEN];
49
snprintf(msgbuf, MAX_MSG_LEN,
50
"log bgn thread_id=%ld query_id=%ld command=%.*s"
51
" db=\"%.*s\" query=\"%.*s\"\n",
52
(unsigned long) thd->thread_id,
53
(unsigned long) thd->query_id,
54
(uint32_t)command_name[thd->command].length, command_name[thd->command].str,
55
thd->db_length, thd->db,
56
thd->query_length, thd->query);
57
wrv= write(fd, msgbuf, msgbuf_len);
58
assert(wrv == msgbuf_len);
63
bool logging_query_func_post (THD *thd)
65
char msgbuf[MAX_MSG_LEN];
73
snprintf(msgbuf, MAX_MSG_LEN,
74
"log end thread_id=%ld query_id=%ld command=%.*s"
75
" utime=%u rows.sent=%ld rows.exam=%u\n",
76
(unsigned long) thd->thread_id,
77
(unsigned long) thd->query_id,
78
(uint32_t)command_name[thd->command].length, command_name[thd->command].str,
79
(uint32_t)(thd->current_utime() - thd->start_utime),
80
(unsigned long) thd->sent_row_count,
81
(uint32_t) thd->examined_row_count);
82
wrv= write(fd, msgbuf, msgbuf_len);
83
assert(wrv == msgbuf_len);
85
// some other interesting things in the THD
86
// thd->enable_slow_log
91
static int logging_query_plugin_init(void *p)
93
logging_t *l= (logging_t *) p;
95
l->logging_pre= logging_query_func_pre;
96
l->logging_post= logging_query_func_post;
98
fd= open("/tmp/drizzle.log", O_WRONLY | O_APPEND);
101
"MRA fail open /tmp/drizzle.log fd=%d er=%s\n",
102
fd, strerror(errno));
106
/* need to do something better with the fd */
111
static int logging_query_plugin_deinit(void *p)
113
logging_st *l= (logging_st *) p;
117
l->logging_pre= NULL;
118
l->logging_post= NULL;
123
mysql_declare_plugin(logging_query)
125
DRIZZLE_LOGGER_PLUGIN,
128
"Mark Atwood <mark@fallenpegasus.com>",
131
logging_query_plugin_init,
132
logging_query_plugin_deinit,
133
NULL, /* status variables */
134
NULL, /* system variables */
135
NULL /* config options */
137
mysql_declare_plugin_end;