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)
10
static char* logging_query_filename= NULL;
14
// copied from drizzled/sql_parse.cc
16
const LEX_STRING command_name[]={
17
{ C_STRING_WITH_LEN("Sleep") },
18
{ C_STRING_WITH_LEN("Quit") },
19
{ C_STRING_WITH_LEN("InitDB") },
20
{ C_STRING_WITH_LEN("Query") },
21
{ C_STRING_WITH_LEN("FieldList") },
22
{ C_STRING_WITH_LEN("CreateDB") },
23
{ C_STRING_WITH_LEN("DropDB") },
24
{ C_STRING_WITH_LEN("Refresh") },
25
{ C_STRING_WITH_LEN("Shutdown") },
26
{ C_STRING_WITH_LEN("Processlist") },
27
{ C_STRING_WITH_LEN("Connect") },
28
{ C_STRING_WITH_LEN("Kill") },
29
{ C_STRING_WITH_LEN("Ping") },
30
{ C_STRING_WITH_LEN("Time") },
31
{ C_STRING_WITH_LEN("ChangeUser") },
32
{ C_STRING_WITH_LEN("BinlogDump") },
33
{ C_STRING_WITH_LEN("ConnectOut") },
34
{ C_STRING_WITH_LEN("RegisterSlave") },
35
{ C_STRING_WITH_LEN("SetOption") },
36
{ C_STRING_WITH_LEN("Daemon") },
37
{ C_STRING_WITH_LEN("Error") }
41
bool logging_query_func_pre (THD *thd)
43
char msgbuf[MAX_MSG_LEN];
53
snprintf(msgbuf, MAX_MSG_LEN,
54
"log bgn thread_id=%ld query_id=%ld command=%.*s"
55
" db=\"%.*s\" query=\"%.*s\"\n",
56
(unsigned long) thd->thread_id,
57
(unsigned long) thd->query_id,
58
(uint32_t)command_name[thd->command].length, command_name[thd->command].str,
59
thd->db_length, thd->db,
60
thd->query_length, thd->query);
61
/* a single write has a OS level thread lock
62
so there is no need to have mutexes guarding this write,
64
wrv= write(fd, msgbuf, msgbuf_len);
65
assert(wrv == msgbuf_len);
70
bool logging_query_func_post (THD *thd)
72
char msgbuf[MAX_MSG_LEN];
76
if (fd < 0) return false;
81
snprintf(msgbuf, MAX_MSG_LEN,
82
"log end thread_id=%ld query_id=%ld command=%.*s"
83
" rows.sent=%ld rows.exam=%u\n",
84
(unsigned long) thd->thread_id,
85
(unsigned long) thd->query_id,
86
(uint32_t)command_name[thd->command].length, command_name[thd->command].str,
87
(unsigned long) thd->sent_row_count,
88
(uint32_t) thd->examined_row_count);
89
/* a single write has a OS level thread lock
90
so there is no need to have mutexes guarding this write,
92
wrv= write(fd, msgbuf, msgbuf_len);
93
assert(wrv == msgbuf_len);
99
static int logging_query_plugin_init(void *p)
101
logging_t *l= (logging_t *) p;
103
if (logging_query_filename == NULL)
105
/* no destination filename was specified via system variables
106
return now, dont set the callback pointers
111
fd= open(logging_query_filename, O_WRONLY | O_APPEND | O_CREAT);
114
fprintf(stderr, "fail open fn=%s er=%s\n",
115
logging_query_filename,
118
/* we should return an error here, so the plugin doesnt load
119
but this causes Drizzle to crash
120
so until that is fixed,
121
just return a success,
122
but leave the function pointers as NULL and the fd as -1
127
l->logging_pre= logging_query_func_pre;
128
l->logging_post= logging_query_func_post;
133
static int logging_query_plugin_deinit(void *p)
135
logging_st *l= (logging_st *) p;
143
l->logging_pre= NULL;
144
l->logging_post= NULL;
149
static DRIZZLE_SYSVAR_STR(filename, logging_query_filename,
151
"File to log queries to.",
154
static struct st_mysql_sys_var* logging_query_system_variables[]= {
155
DRIZZLE_SYSVAR(filename),
159
mysql_declare_plugin(logging_query)
161
DRIZZLE_LOGGER_PLUGIN,
164
"Mark Atwood <mark@fallenpegasus.com>",
167
logging_query_plugin_init,
168
logging_query_plugin_deinit,
169
NULL, /* status variables */
170
logging_query_system_variables,
173
mysql_declare_plugin_end;