~drizzle-trunk/drizzle/development

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/* drizzle/plugin/logging_query/logging_query.cc */

/* need to define DRIZZLE_SERVER to get inside the THD */
#define DRIZZLE_SERVER 1
#include <drizzled/server_includes.h>
#include <drizzled/plugin_logging.h>

#define MAX_MSG_LEN (32*1024)

static char* logging_query_filename= NULL;

static int fd= -1;

// copied from drizzled/sql_parse.cc

const LEX_STRING command_name[]={
  { C_STRING_WITH_LEN("Sleep") },
  { C_STRING_WITH_LEN("Quit") },
  { C_STRING_WITH_LEN("InitDB") },
  { C_STRING_WITH_LEN("Query") },
  { C_STRING_WITH_LEN("FieldList") },
  { C_STRING_WITH_LEN("CreateDB") },
  { C_STRING_WITH_LEN("DropDB") },
  { C_STRING_WITH_LEN("Refresh") },
  { C_STRING_WITH_LEN("Shutdown") },
  { C_STRING_WITH_LEN("Processlist") },
  { C_STRING_WITH_LEN("Connect") },
  { C_STRING_WITH_LEN("Kill") },
  { C_STRING_WITH_LEN("Ping") },
  { C_STRING_WITH_LEN("Time") },
  { C_STRING_WITH_LEN("ChangeUser") },
  { C_STRING_WITH_LEN("BinlogDump") },
  { C_STRING_WITH_LEN("ConnectOut") },
  { C_STRING_WITH_LEN("RegisterSlave") },
  { C_STRING_WITH_LEN("SetOption") },
  { C_STRING_WITH_LEN("Daemon") },
  { C_STRING_WITH_LEN("Error") }
};


bool logging_query_func_pre (THD *thd)
{
  char msgbuf[MAX_MSG_LEN];
  int msgbuf_len= 0;
  int wrv;

  if (fd < 0) 
    return false;

  assert(thd != NULL);

  msgbuf_len=
    snprintf(msgbuf, MAX_MSG_LEN,
	     "log bgn thread_id=%ld query_id=%ld command=%.*s"
	     " db=\"%.*s\" query=\"%.*s\"\n",
	     (unsigned long) thd->thread_id,
	     (unsigned long) thd->query_id,
	     (uint32_t)command_name[thd->command].length, command_name[thd->command].str,
	     thd->db_length, thd->db,
	     thd->query_length, thd->query);
  /* a single write has a OS level thread lock
     so there is no need to have mutexes guarding this write,
  */
  wrv= write(fd, msgbuf, msgbuf_len);
  assert(wrv == msgbuf_len);

  return false;
}

bool logging_query_func_post (THD *thd)
{
  char msgbuf[MAX_MSG_LEN];
  int msgbuf_len= 0;
  int wrv;

  if (fd < 0) return false;

  assert(thd != NULL);

  msgbuf_len=
    snprintf(msgbuf, MAX_MSG_LEN,
	     "log end thread_id=%ld query_id=%ld command=%.*s"
	     " rows.sent=%ld rows.exam=%u\n",
	     (unsigned long) thd->thread_id, 
	     (unsigned long) thd->query_id,
	     (uint32_t)command_name[thd->command].length, command_name[thd->command].str,
	     (unsigned long) thd->sent_row_count,
	     (uint32_t) thd->examined_row_count);
  /* a single write has a OS level thread lock
     so there is no need to have mutexes guarding this write,
  */
  wrv= write(fd, msgbuf, msgbuf_len);
  assert(wrv == msgbuf_len);


  return false;
}

static int logging_query_plugin_init(void *p)
{
  logging_t *l= (logging_t *) p;

  if (logging_query_filename == NULL)
  {
    /* no destination filename was specified via system variables
       return now, dont set the callback pointers 
    */
    return 0;
  }

  fd= open(logging_query_filename, O_WRONLY | O_APPEND | O_CREAT,
           S_IRUSR|S_IWUSR);
  if (fd < 0) 
  {
    fprintf(stderr, "fail open fn=%s er=%s\n",
	    logging_query_filename,
	    strerror(errno));

    /* we should return an error here, so the plugin doesnt load
       but this causes Drizzle to crash
       so until that is fixed,
       just return a success,
       but leave the function pointers as NULL and the fd as -1
    */
    return 0;
  }

  l->logging_pre= logging_query_func_pre;
  l->logging_post= logging_query_func_post;

  return 0;
}

static int logging_query_plugin_deinit(void *p)
{
  logging_st *l= (logging_st *) p;

  if (fd >= 0) 
  {
    close(fd);
    fd= -1;
  }

  l->logging_pre= NULL;
  l->logging_post= NULL;

  return 0;
}

static DRIZZLE_SYSVAR_STR(filename, logging_query_filename,
  PLUGIN_VAR_READONLY,
  "File to log queries to.",
  NULL, NULL, NULL);

static struct st_mysql_sys_var* logging_query_system_variables[]= {
  DRIZZLE_SYSVAR(filename),
  NULL
};

mysql_declare_plugin(logging_query)
{
  DRIZZLE_LOGGER_PLUGIN,
  "logging_query",
  "0.1",
  "Mark Atwood <mark@fallenpegasus.com>",
  "Log queries",
  PLUGIN_LICENSE_GPL,
  logging_query_plugin_init,
  logging_query_plugin_deinit,
  NULL,   /* status variables */
  logging_query_system_variables,
  NULL
}
mysql_declare_plugin_end;