~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
/* 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 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;

  assert(thd != NULL);
  assert(fd > 0);

  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);
  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;

  assert(thd != NULL);
  assert(fd > 0);

  msgbuf_len=
    snprintf(msgbuf, MAX_MSG_LEN,
	     "log end thread_id=%ld query_id=%ld command=%.*s"
	     " utime=%u 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,
	     (uint32_t)(thd->current_utime() - thd->start_utime),
	     (unsigned long) thd->sent_row_count,
	     (uint32_t) thd->examined_row_count);
  wrv= write(fd, msgbuf, msgbuf_len);
  assert(wrv == msgbuf_len);

  // some other interesting things in the THD
  // thd->enable_slow_log

  return false;
}

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

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

  fd= open("/tmp/drizzle.log", O_WRONLY | O_APPEND);
  if (fd < 0) {
    fprintf(stderr,
	    "MRA fail open /tmp/drizzle.log fd=%d er=%s\n",
	    fd, strerror(errno));
    return fd;
  }

  /* need to do something better with the fd */

  return 0;
}

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

  close(fd);

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

  return 0;
}

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 */
  NULL,   /* system variables */
  NULL    /* config options */
}
mysql_declare_plugin_end;